我想从核心数据读取数组。我的代码如下无法读取数据使用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方法。这是我的要求。另外,当我尝试从主线程检索这个数组时,我完全确定了所有数据。
任何人都可以给我一个提示,我错了吗?
此致敬礼