Ios NSOperationQueue currentQueue不工作
我正试图在currentQueue上运行下面所示的Ios NSOperationQueue currentQueue不工作,ios,objective-c,afnetworking,nsoperationqueue,Ios,Objective C,Afnetworking,Nsoperationqueue,我正试图在currentQueue上运行下面所示的AFURLConnectionOperation,因为我想让我的主线程在用户交互时保持空闲,但是调用MainQUE时什么也没有发生 但是,如果我在mainQueue上调用相同的AFURLConnectionOperation,它就可以正常工作 请看下面的代码 // Send Batch NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:
AFURLConnectionOperation
,因为我想让我的主线程在用户交互时保持空闲,但是调用MainQUE时什么也没有发生
但是,如果我在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
最后,我调用以下代码中的一个或另一个
// 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
原因是
您可以从正在运行的操作对象中使用此方法来获取对启动它的操作队列的引用。从运行操作的上下文外部调用此方法通常会导致返回nil
所以,我想,如果您记录[NSOperationQueue currentQueue]
,它是零
如果需要新队列,请使用
[[NSOperationQueue alloc] init];
在队列上添加操作后,如果操作最终没有启动,则有两种方法来执行它们
XCTestExpectation *expectation1 = [self expectationWithDescription:@"ExtractColorsInternal function call on NSOperationQueue"];
dispatch_async(dispatch_get_main_queue(), ^{
[expectation1 fulfill];
});
[self waitForExpectationsWithTimeout:1000 handler:^(NSError *error) {
if (error != nil) {
NSLog(@"Error: %@", error.localizedDescription);
}
}];
因此,如果我创建了自己的NSOperationQueue,但它不会在主线程上运行?我不确定您有什么问题,操作是异步的,它不会冻结主线程。