ios - NSOperationQueue currentQueue 不工作

标签 ios objective-c afnetworking nsoperationqueue

我正在尝试在 currentQueue 上运行 AFURLConnectionOperation,因为我想让我的主线程空闲以供用户交互,但是当我调用 mainQeue 时没有任何反应。

但是,如果我在 mainQueue 上调用相同的 AFURLConnectionOperation,它会完美运行。

请看下面的代码

// Send Batch
        NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
            NSLog(@"%lu of %lu complete", (unsigned long)numberOfFinishedOperations, (unsigned long)totalNumberOfOperations);
        } completionBlock:^(NSArray *operations) {
            // check batch response
            NSError *error;
            for (AFHTTPRequestOperation *op in operations) {
                if (op.isCancelled){
                    return ;
                }
                if (op.responseObject){
                    // Current JSON Batch complete
                    NSMutableArray *jsonObject = [NSJSONSerialization JSONObjectWithData:op.request.HTTPBody options:kNilOptions error:&error];
                    // Update sent_flag using current chunk
                    [[SyncModel sharedInstance] updateSentFlag:jsonObject];
                }
                if (op.error){
                    error = op.error;
                    NSLog(@"Error == %@", error);
                }
            }
        }];

然后最后我调用以下代码之一

[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO]; // this works
[[NSOperationQueue currentQueue] addOperations:operations waitUntilFinished:NO]; // this dose not work

最佳答案

原因是

You can use this method from within a running operation object to get a reference to the operation queue that started it. Calling this method from outside the context of a running operation typically results in nil being returned.

所以,我想,如果你记录 [NSOperationQueue currentQueue],它是 nil

如果你想要一个新队列,使用

[[NSOperationQueue alloc] init];

关于ios - NSOperationQueue currentQueue 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33642590/

相关文章:

iphone - AFNetworking 监控所有下载过程

ios - 等待 block 完成吗?

iphone - 单击选项卡时返回 UINavigationController 堆栈的顶部?

iphone - UIViewController:我可以在哪里放置自定义初始化

ios - 使用 AFNetworking 保存/缓存文件并将其用于在线和离线访问

ios - 没有匹配函数调用 'MatToUIImage'

ios - UITableView - 带背景图像的 UIRefreshControl

ios - 捕获 iPhone 游戏音频

ios - Objective C - IOS - 在类型对象上找不到属性 - 访问对象变量时

ios - 将谷歌分析跟踪添加到我的 iOS Today 小部件