PerformSelector withObject afterDelay does not run in the sub-thread call. performselector
For example, this is a problem found recently when I modified a Data Synchronization module. The entire data synchronization task is placed in a background thread after the App is started. After a single data synchronization task is successfully executed
Objective-c code
- [Self defined mselector :( nonnull SEL) withObject :( nullable id) afterDelay :( NSTimeInterval)];
To execute the next single data synchronization task. Through debugging, it is found that the SEL method is not called when this line of code is executed. After determining the existence of this method, I never thought of the reason, So Google. The answer is that the performSelector withObject afterDelay method does not call the SEL method in the Child thread, and the performSelector withObject method is called directly. The reason is:
1. afterDelay uses the timer of the current thread to call SEL after a certain time, and NO AfterDelay directly calls SEL.
2. There is no timer in the Child thread by default.
Therefore, there are two solutions to the problem:
1. Enable the thread Timer
Objective-c code
- [[Nsunloop currentRunLoop] run];
2. Run the scheduled task with dispatch_after.
Objective-c code
- -(Void) functionToDelay :( SEL) function param :( nullable id) param afterDelay :( NSTimeInterval) delay {
- Dispatch_time_t time = dispatch_time (DISPATCH_TIME_NOW, delay * NSEC_PER_SEC );
- Dispatch_after (time, dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
- If ([self respondsToSelector: function]) {
- # Pragma clang diagnostic push
- # Pragma clang diagnostic ignored "-Warc-starter mselector-leaks"
- [Self defined mselector: function withObject: param];
- # Pragma clang diagnostic pop
- }
- });
- }
QQ technology exchange group 290551701 http://cxy.liuzhihengseo.com/556.html