Run Selector: withObject: afterDelay: as part of NSOperation

I am executing some code inside some NSOperation objects managed by NSOperationQueue . The code also contains a call delay call using performSelector:withObject:afterDelay:

The problem is that the corresponding selector, which should be called deferred, is not called at all .

After reading https://stackoverflow.com/a/1681616/... , I think this is due to the fact that NSOperation already completed, and its stream no longer exists, "forgetting" about the scheduled call to the selector.

How can I get around this? How to delay a method call in NSOperation ?

+4
source share
1 answer

One possibility is to use Grand Central Dispatch, namely dispatch_after() :

 double delayInSeconds = 2.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_after(popTime, queue, ^{ ... }); 

Instead of dispatch_get_global_queue() you can also create your own dispatch queue or use the main queue with dispatch_get_main_queue() .

+4
source

Source: https://habr.com/ru/post/1388614/


All Articles