我有一些麻烦传递一个NSNumber对象到不同的线程。 我在viewDidload上调用了一个从核心数据中加载一些对象作为后台进程的函数。它调用另一个函数,通过加载的对象进行循环,以查看是否有任何与其相关的图像被下载。如果它不存在,请异步下载图像并将其保存在本地。事情是我需要在主线程上执行startDownloadFor:atIndex:。但是应用程序因传递的NSNumber对象而崩溃。这里是代码..Iphone:传递对象和多线程
- (void)viewDidLoad {
...
...
[self performSelectorInBackground:@selector(loadImages) withObject:nil];
}
-(void)loadImages{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
...
...
[self fillInImages];
[pool release];
}
-(void)fillInImages{
NSString *imageURL;
for (int i=0; i < [dataManager.objectList count]; i++) {
...
if ([dataManager.RelatedImages Image] == nil) {
//[self startDownloadFor:imageURL atIndex:[NSNumber numberWithInt:i]; // << WORKS FINE
[self performSelectorOnMainThread:@selector(startDownloadFor:atIndex:) withObject:(imageURL, [NSNumber numberWithInt:i]) waitUntilDone:YES]; // << CRASHES
...
}else {
...
}
...
}
...
}
-(void)startDownloadFor:(NSString*)imageUrl atIndex:(int)indexPath{
NSString *indexKey = [NSString stringWithFormat:@"key%d",indexPath];
...
}
这样做的正确方法是什么?
感谢