ASSetworkQueue里面有NSOperation阻塞主线程

时间:2011-12-19 10:30:25

标签: objective-c ios asihttprequest nsoperation

我正在使用NSOperation来收集应下载的数据(需要2-5秒),之后我会下载它。我在这个NSOperation中放了一个ASINetworkQueue来开始下载以前收集的数据。

一切正常但是当我在cancelAllOperations上调用ASINetworkQueue时,主线程阻塞和UI冻结。为什么会这样?其他一切都很好。

这是我的代码:

- (void)main {
    //ManagedObjectContext for operations
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    self.managedObjectContext = [[NSManagedObjectContext alloc] init];


    [self.managedObjectContext setPersistentStoreCoordinator: [appDelegate persistentStoreCoordinator]];

    // Register context with the notification center
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 
    [nc addObserver:self
           selector:@selector(mergeChanges:)
               name:NSManagedObjectContextDidSaveNotification
             object:self.managedObjectContext];


    [self startDownload];


    if (!self.downloadDidFail) {
        [self moveFiles];

        [self.managedObjectContext save:nil];
    }
}

- (void)startDownload {    
    self.downloadQueue = [ASINetworkQueue queue];
    self.downloadQueue.delegate = self;
    [self.downloadQueue setRequestDidFailSelector:@selector(dataRequestFailed:)];
    [self.downloadQueue setRequestDidFinishSelector:@selector(dataRequestFinished:)];
    [self.downloadQueue setQueueDidFinishSelector:@selector(dataQueueFinished:)];
    [self.downloadQueue setShouldCancelAllRequestsOnFailure:YES];
    [self.downloadQueue setDownloadProgressDelegate:self.progressView];

    for (File *dataFile in self.dataFiles) {
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:dataFile.url]];
            [request setDownloadDestinationPath:dataFile.path];

            [self.downloadQueue addOperation:request];
        }
    }

    [self.downloadQueue go];
    [self.downloadQueue waitUntilAllOperationsAreFinished];
}

- (void)dataRequestFinished:(ASIHTTPRequest *)request {
    NSLog(@"DL finished");
}

- (void)dataRequestFailed:(ASIHTTPRequest *)request {
    DLog(@"Download failed");

    self.downloadDidFail = YES;
}

- (void)dataQueueFinished:(ASINetworkQueue *)queue {
    DLog(@"Finished Data Queue");
}

- (void)cancelDownload {
    self.canceledDownload = YES;

    [self.downloadQueue cancelAllOperations];
}

3 个答案:

答案 0 :(得分:2)

我有同样的问题,并通过致电解决:

[queue setShouldCancelAllRequestsOnFailure:NO]

致电之前:

[queue cancelAllOperations].

答案 1 :(得分:0)

你的失败委托方法有什么作用?默认情况下,ASIHTTPRequest将在主线程上运行,所以如果它进行了大量处理(或者有很多请求),这可能需要相当长的时间。

答案 2 :(得分:0)

ASI请求响应和队列响应被故意移动到主线程以用于库设计目的。 你有两个解决方案: -Subclass ASIHTTPRequest并覆盖2种方法。 (在代码中查找类似“主线程的子类”)。 - 修改图书馆。 (很简单,但我个人不喜欢这个解决方案)。