我正在编写一个使用UIPickerView的应用程序,并将每个更改存储到核心数据中的选择器中(应用程序的名称是iTunes商店中的Wizard Blood)。到目前为止,我已经取得了一些成功,因为我的-didSelectRow方法通过以下代码成功地将新对象添加到核心数据中:
// Get context, entity, managedobject from the fetchedResultsController
[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
//Iterates through each component and sets the value to the appropriate key
for (int j=0; j < [LifeCounter numberOfComponents]; j++) {
NSString *keyName = [NSString stringWithFormat:@"lifeTotal%i",j];
[newManagedObject setValue:[NSNumber numberWithInteger:[LifeCounter selectedRowInComponent:j]] forKey:keyName];
}
NSError *error;
if (![context save:&error]) {
NSLog(@"Error saving entity: %@", [error localizedDescription]);
}
然而,在我的-viewDidLoad中,我似乎无法获取核心数据来加载来自最新对象的值。这就是我到目前为止所做的:
lifeCounterArray = [[NSMutableArray alloc] init];
for ( int i = 200; i >= -200; i-- )
{
NSString *myString = [NSString stringWithFormat:@"%i", i];
[lifeCounterArray addObject:myString];
}
//Hopefully this retrieves the latest values from the core data store
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//Iterates through each component, setting it to the stored value in core data
for (int j=0; j < [LifeCounter numberOfComponents]; j++) {
NSString *keyName = [NSString stringWithFormat:@"lifeTotal%i",j];
NSInteger row = [[managedObject valueForKey:keyName] integerValue] +180;
[LifeCounter selectRow:row inComponent:j animated:NO];
}
现在,我很确定我的fetchedResultsController方法正在工作,因为我可以看到它创建对象(我可以在sqlite数据库中看到它们)但是我甚至不应该使用fetchedResultsController因为我真的只关心它最新的对象。如果需要,我很乐意发布我的fetchedResultsController方法,感谢您的帮助。
答案 0 :(得分:0)
如果您上次查询后更改了数据,可能必须删除fetchedResultsController的缓存吗?
[NSFetchedResultsController deleteCacheWithName:@"(cachename)"];
缓存名称在initWithFetchRequest中设置。
如果不起作用,请尝试在db更改后将fetchedResultsController设置为nil,这将强制它重做请求。