简体   繁体   中英

dispatch_async block not getting invoked

MySynchManager class is having a shared instance.

One of the function in MySynchManager class is

- (void)uploadSession:(NSString *)sessionId {
    // run the upload process on a separate thread to not to block the main thread for user interaction
    // process upload data in serial Queue
    NSLog(@"Inside uploadSession");
    if (!_serialQueue) {
        NSLog(@"uploadSession, _serialQueue is NOT ACTIVE");

        [self setRunLoopStarted:FALSE];
        _serialQueue = dispatch_queue_create("sessionUploadQueue", NULL);

        dispatch_async(_serialQueue, ^{
            [[MySyncManager sharedInstance] dispatchSession:sessionId];
        });
    }
    else {
        //[self setRunLoopStarted:FALSE];
        dispatch_async(_serialQueue, ^{
            [self dispatchSession:sessionId];
        });
        NSLog(@"Adding block to the dispatch queue is complete");
    }
}

uploadSession:@"session" is being called from view controllers.

The problem that I am facing is sometimes the code present in dispatchSession is called, but sometimes block is not called. I only observe the log print statement after the block is printed.

Can any one of you explain the reason behind this?

This is weird code. Try this instead

-(void)uploadSession:(NSString *)sessionId
 {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       _serialQueue = dispatch_queue_create("sessionUploadQueue", NULL);
    });

    dispatch_async(_serialQueue, ^{
      [self dispatchSession:sessionId];
    });

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM