Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSOperationQueue currentQueue不工作_Ios_Objective C_Afnetworking_Nsoperationqueue - Fatal编程技术网

Ios NSOperationQueue 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:

我正试图在currentQueue上运行下面所示的
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];

在队列上添加操作后,如果操作最终没有启动,则有两种方法来执行它们

  • 使用等待块,例如在使用XCTest框架进行单元测试期间,使用

    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);
    }
    }];
    
  • 调用CFRunLoopRun(),它将在当前队列中成功执行当前操作


  • 因此,如果我创建了自己的NSOperationQueue,但它不会在主线程上运行?我不确定您有什么问题,操作是异步的,它不会冻结主线程。