第二次 NSThread 崩溃
我的应用程序,当用户打开它时,启动一个 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];
}
它工作正常!
在第一个视图中有一个表格视图:第一个单元格需要读取用户位置的 GPS 坐标并从 url 解析其他数据。
这是代码(在 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];
它就可以正常工作!
你知道我做错了什么吗?
My app, when the user open it, starts an NSThread for download new data (parsing an xml file).
this is the code (in 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];
}
It works fine!
In the first view there is a tableview: the first cell need to read the GPS Coordinate of the user position and parse other data from a url.
this is the code (in 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];
}
so... when the user touch that cell, the app crashes! And the console has no one log!
(if a call the method without use a NSThread
[self load_data_2];
it works without problem!
Do you know what i'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您向 UIKit/AppKit 对象发送的消息应该从主线程发出。事情就是这样,除非您扩展了对象并在这些部分中显式添加了 MT 支持。
Foundation 定义了一些方便的方法(例如,
-[NSObject PerformSelectorInBackground*
、-[NSObject PerformSelectorOnMainThread*
),因此您也可以删除示例中的大部分样板文件作为从主线程发送给 UIKit 对象的消息。如果问题仍然存在,您可能需要发布更全面的示例。
your messages to UIKit/AppKit objects should be made from the main thread. that's just the way it is, unless you have extended an object and explicitly added MT support in those sections.
Foundation defines a few convenience methods (e.g.,
-[NSObject performSelectorInBackground*
,-[NSObject performSelectorOnMainThread*
), so you can remove much of the boilerplate in your example, as well as the messages to UIKit objects from the main thread.if the problem persists, you may want to post a more thorough example.