我的应用程序,当用户打开它时,启动NSThread以下载新数据(解析xml文件)。 这是代码(在AppDelegate.m中):
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSThread* parse_thread = [[NSThread alloc] initWithTarget:self selector:@selector(load_data) object:nil];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[parse_thread start];
[parse_thread release]; //is it right?
-(void)load_data{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
... //here the parser
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[pool release];
[NSThread exit];
}
一切正常!
在第一个视图中有一个tableview:第一个单元格需要读取用户位置的GPS坐标并解析网址中的其他数据。
这是代码(在myview.m中):
- (void)locationUpdate:(CLLocation *)location {
loc = location;
[locationController.locationManager stopUpdatingLocation];
NSThread *parse_2 = [[NSThread alloc] initWithTarget:self selector:@selector(load_data_2) object:nil];
[parse_2 start];
[parse_2 release];
}
-(void)load_data_2{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
//here the parse
[pool release];
[NSThread exit];
}
所以...当用户触摸该单元格时,应用程序崩溃了!而控制台没有一个日志! (如果调用方法而不使用NSThread
[self load_data_2];
它没有问题!
你知道我做错了什么吗?
答案 0 :(得分:0)
您对UIKit / AppKit对象的消息应该来自主线程。这就是它的方式,除非你扩展了一个对象并明确地在这些部分中添加了MT支持。
Foundation定义了一些便捷方法(例如,-[NSObject performSelectorInBackground*
,-[NSObject performSelectorOnMainThread*
),因此您可以删除示例中的大部分样板,以及从主线程到UIKit对象的消息。
如果问题仍然存在,您可能需要发布一个更全面的示例。