lov*_*tkt 2 iphone objective-c nsoperation ios afnetworking
我将应用程序上传图像从uiimagepickercontroller实现到服务器.但我想在上传过程中实现取消按钮取消上传.
在上传功能:
[operation setCompletionBlock:^{
ProgressView.hidden = YES;
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Uploading successfull." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[av show];
[av release];
overlayToolbar.userInteractionEnabled = YES;
NSLog(@"response string: %@", operation.responseString); //Lets us know the result including failures
}];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
Run Code Online (Sandbox Code Playgroud)
并且buttoncancel:
[httpClient.operationQueue cancelAllOperations];
Run Code Online (Sandbox Code Playgroud)
当我按下buttoncancel时,它不会停止上传过程,然后出现alertview"Uploading successful".我不知道为什么不能停止但仍然会出现alertview.alertview你能帮帮我吗?
您正在取消错误的操作队列.您添加的操作,一个全新的NSOperationQueue,但你打电话cancelAllOperations的httpClient.operationQueue.
如果在已添加操作的同一操作队列上取消上载,则应该可以正常工作.这AFURLConnectionOperation.m是取消时发生的情况:
- (void)cancel {
[self.lock lock];
if (![self isFinished] && ![self isCancelled]) {
[self willChangeValueForKey:@"isCancelled"];
_cancelled = YES;
[super cancel];
[self didChangeValueForKey:@"isCancelled"];
// Cancel the connection on the thread it runs on to prevent race conditions
[self performSelector:@selector(cancelConnection) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]];
}
[self.lock unlock];
}
Run Code Online (Sandbox Code Playgroud)
有关操作队列的更多背景信息:
cancelAllOperations通常取消所有待处理的操作.如果某个操作已在进行中,则可以取消它正在执行的操作(AFNetworking正在处理此情况).
此方法向队列中当前的所有操作发送取消消息.排队操作在开始执行之前被取消.如果操作已在执行,则由该操作识别取消并停止其正在执行的操作.
资料来源:http://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html#//apple_ref/occ/instm/NSOperationQueue/cancelAllOperations
这可能对您有所帮助:http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues
此外,在AFNetworking的特殊情况下,这也可能很有趣:如何立即强制取消使用AFNetworking的NSOperation?
| 归档时间: |
|
| 查看次数: |
1426 次 |
| 最近记录: |