dispatch_async块未被调用

时间:2022-05-15 20:59:42

MySynchManager class is having a shared instance.

MySynchManager类有一个共享实例。

One of the function in MySynchManager class is

MySynchManager类中的一个功能是

- (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.

uploadSession:从视图控制器调用@“session”。

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.

我面临的问题有时是调用dispatchSession中的代码,但有时不调用block。打印块后,我只观察日志打印语句。

Can any one of you explain the reason behind this?

你们中的任何一个人都可以解释这背后的原因吗?

1 个解决方案

#1


1  

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];
    });

 }

#1


1  

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];
    });

 }