我有一些麻煩傳遞一個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];
...
}
這樣做的正確方法是什麼?
感謝