我想從核心數據讀取數組。我的代碼如下無法讀取數據使用NSManagedObjectContext
-(NSArray *)getAges
{
NSArray *fetchedObjects = [[NSArray alloc] init];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *context = nil;
context = [[NSManagedObjectContext alloc] init];
@try {
NSPersistentStoreCoordinator *tempCordinator = (NSPersistentStoreCoordinator *)[(AppDelegate *)[[UIApplication sharedApplication] delegate] persistentStoreCoordinator];
[context setPersistentStoreCoordinator:tempCordinator];
NSError *error;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Age"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
}
@catch (NSException *exception) {
NSLog(@"core data exception occured.");
fetchedObjects = nil;
}
[fetchRequest release];
[context release];
return fetchedObjects;
}
現在在主線程中,在一個IBAction方法中,我寫了下面一行。
[NSThread detachNewThreadSelector:@selector(loadAgesMethod) toTarget:self withObject:nil];
問題是這個方法返回一個20個元素的數組。我可以看到數組數20,但在調試器中,對陣數組的每個元素,我看到「超出範圍」和「不可用匯總」。而當我試圖從這個數組中獲取任何對象時,我什麼也得不到。有一點需要提到的是,我正如上面提到的那樣,從輔助線程調用loadAgesMethod中的getAges方法。這是我的要求。另外,當我嘗試從主線程檢索這個數組時,我完全確定了所有數據。
任何人都可以給我一個提示,我錯了嗎?
此致敬禮