Objective c 使用调度异步、can';是否在同一控制器上触发两个函数调用?
我的AppDelegate中有以下方法。我找不到一种方法来更新updateView上的标签并通过ASIHTTPRequest执行上载。在下面的代码中,它将执行上载(startUpload),但不更新标签(updateLabel)。如果我注释掉对startUpload的调用,它将更新标签。我还尝试在uploadViewController中创建一个调度队列,结果类似,我只能使用这两种方法中的一种Objective c 使用调度异步、can';是否在同一控制器上触发两个函数调用?,objective-c,cocoa,grand-central-dispatch,Objective C,Cocoa,Grand Central Dispatch,我的AppDelegate中有以下方法。我找不到一种方法来更新updateView上的标签并通过ASIHTTPRequest执行上载。在下面的代码中,它将执行上载(startUpload),但不更新标签(updateLabel)。如果我注释掉对startUpload的调用,它将更新标签。我还尝试在uploadViewController中创建一个调度队列,结果类似,我只能使用这两种方法中的一种 - (void) showProgressView:(NSString *)filePath {
- (void) showProgressView:(NSString *)filePath {
NSView *view = [uploadViewController view];
dispatch_queue_t queue = dispatch_get_global_queue(0,0);
dispatch_async(queue, ^{
// assigns the upload view to the root NSPanel
[box setContentView:view];
});
dispatch_async(queue, ^{
// starts the upload using ASIHTTPRequest
[uploadViewController startUpload:filePath];
});
dispatch_async(queue, ^{
// updates a label in the upload view
[uploadViewController updateLabel];
});
}
在UploadViewController中:
-(void)startUpload:(NSString *)filePath {
HandZipper *handZipper = [HandZipper alloc];
zipPath = [handZipper compressHands:(filePath):(processStatus)];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ignore"]] autorelease];
[request setRequestMethod:@"PUT"];
[request setPostBodyFilePath:zipPath];
[request setShouldStreamPostDataFromDisk:YES];
[request setDelegate:self];
[request setUploadProgressDelegate:progressIndicator];
[request setDidFinishSelector:@selector(postFinished:)];
[request setDidFailSelector:@selector(postFailed:)];
[request startSynchronous];
}
您应该始终更新主线程上的UI元素。如果您知道将从主线程调用
showProgressView:
,则从[box setContentView:view]周围删除dispatch\u async代码>和[uploadViewController updateLabel]直接给他们打电话。否则,如果可能从另一个线程调用showProgressView:
,则只需使用dispatch\u get\u main\u queue(void)
而不是queue