kar*_*e23 6 iphone sdk nsthread ios
我正在做一个应用程序,在读取XML文件时使用NSThread加载viewControllers的内容.
我做到如下:
-(void)viewDidAppear:(BOOL)animated
{
// Some code...
[NSThread detachNewThreadSelector:@selector(loadXML) toTarget:self withObject:nil];
[super viewDidAppear:YES];
}
-(void)loadXML{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Read XML, create objects...
[pool release];
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果用户在加载NSThread时更改为另一个viewController,我不知道如何停止NSThread,这样做应用程序崩溃了.
我试图取消或退出NSThread如下,但没有成功:
-(void)viewsDidDisappear:(BOOL)animated{
[NSThread cancel];
// or [NSThread exit];
[super viewDidDisappear:YES];
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?谢谢.
当您分离新线程时,您无法再从viewDidDisappear等取消或退出它.这些特定于UI的方法仅在主线程上执行,因此退出/取消适用于主线程,这显然是错误的.
而不是使用分离新线程方法,在.h中声明NSThread变量并使用initWithTarget: selector: object:方法对其进行初始化,并在需要时随时随地取消它.
| 归档时间: |
|
| 查看次数: |
9117 次 |
| 最近记录: |