我正在尝试在 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/