ios - How to work with NSOperationQueue and NSBlockOperation with dispatch gcd? -


here code

@interface viewcontroller () @property (nonatomic, strong) nsoperationqueue *queue; @end   @implementation viewcontroller - (void)viewdidload {     [super viewdidload];      _queue = [[nsoperationqueue alloc] init];      nsblockoperation *ablockoperation = [[nsblockoperation alloc] init];     __weak nsblockoperation* aweakblockoperation = ablockoperation;      [ablockoperation addexecutionblock:^{         nslog(@"queue should still have operation. , does. yay!: %@", [_queue operations]); // should print correctly. show nsblock operation correctly residing inside nsoperationqueue         dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(3.0 * nsec_per_sec)), dispatch_get_main_queue(), ^{             nslog(@"now queue empty??: %@", [_queue operations]); // should print being empty             nslog(@"and weak block nil???: %@", aweakblockoperation); // should print out **nil**             if (![aweakblockoperation iscancelled]) {                 // have no way check operation             }          });     }];      [_queue addoperation:ablockoperation]; @end 

[edit] goal have user interaction this: there tableview on screen 5 or more cells. when ever user click cell, background process perform background process take while. app will, @ 3 second intervals, check see if user clicked on cell. if user clicked on cell, should cancel current operation queue, , begin processing new 1 user clicked on.

from code above have 2 problems cant solve.

  1. how make weak reference isnt deallocated in dispatch_after block? goal of putting there pause app 3 seconds. if dispatch_after incorrect, code use there prevent becoming nil?

  2. why nsoperationqueue become empty after call dispatch_after? there way make not become empty?

dispatch_after schedules block , returns immediately. so, nsblockoperation's executionblock has no work — finishes , removed queue. @ time, operation released , weak reference becomes nil before dispatch_after block called later.

if dispatch_after first , schedule operation inside block, might suit needs. use sleep, wouldn't recommend since unnecessarily blocking thread. see this question more discussion on nsoperation , delays.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -