有点卡在这个问题上,我不确定我哪里出错了。这是我在做什么:
类调用:
- (void)updateApplicationDataInBackground {
updateView = [[UpdatingView alloc] init];
[self.view addSubview:updateView.view];
DataSynchronizer *dataSynchronizer = [[DataSynchronizer alloc] init];
[NSThread detachNewThreadSelector:@selector(initWithDataRequest:) toTarget:dataSynchronizer withObject:self];
[dataSynchronizer release];
这将创建一个线程来从服务器检索数据并对其进行解析。在 DataSynchronizer 中,这是被调用的方法:
- (void)initWithDataRequest:(id)parent {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
controller = parent;
NSLog(@"DataSynchronizer initWithDataRequest called");
NSURL *url = [NSURL URLWithString: ApiUrl];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:ApiKey forKey:@"key"];
[request setPostValue:ApiPass forKey:@"password"];
[request setPostValue:@"somevalue" forKey:@"framework"];
[request setPostValue:@"somevalue" forKey:@"method"];
[request setDidFinishSelector:@selector(parseResult:)];
[request setDidFailSelector:@selector(requestError:)];
[request setTimeOutSeconds:60];
[request setDelegate:self];
[request startAsynchronous];
[pool release];
收到数据后,我会解析内容并进行数据同步。这一切都按预期工作。我决定加入一个 UIProgressView,这样用户就可以看到这个请求发生了什么,这个进度视图位于 updateView 中,它是在 updateApplicationDataInBackground 中创建的。
我不是要显示 Web 服务调用的进度,而只是在数据处理中达到里程碑时。在 DidFinishSelector 中,它的调用 parseResult
它使用响应数据调用了五个方法:
[self parseData:[data objectForKey:@"types"] forObject:[Types class] andParent:nil];
[controller performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:.4] waitUntilDone:YES];
在我尝试更新 UIProgressView 的每个进程之后,它永远不会更新。现在,如果我只是从 ASIHTTPRequest 外部调用 performSelectorOnMainThread,它会按预期工作,但不会在 DidFinishSelector 内工作。我已经尝试了很多变体,它调用了一个更新 mainThread 的本地方法,我只是使用 performSelector。没有任何效果,如何更新 UIProgessView?
问题是产生线程的线程吗?
谢谢
编辑:
看起来 DidFinishSelector 已经在主线程上被调用了。我已经更新了我的代码以简单地调用:
[controller updateProgress:[NSNumber numberWithFloat:.8]]
还是没有运气......
意识到查看 UIProgessView 更新方法可能会有所帮助。
- (void)updateProgress:(NSNumber *)progress {
float newProgess = [progress floatValue];
[updateView.myProgress setProgress: newProgess];