Iphone 如何从核心数据中检索最新对象?
我正在编写一个应用程序,它使用UIPickerView并将选择器的每个更改存储在核心数据中。该应用程序的名称是iTunes商店中的Wizard Blood。到目前为止,我已经取得了一些成功,我的-didSelectRow方法通过以下代码成功地将新对象添加到核心数据中:Iphone 如何从核心数据中检索最新对象?,iphone,core-data,ios4,uipickerview,Iphone,Core Data,Ios4,Uipickerview,我正在编写一个应用程序,它使用UIPickerView并将选择器的每个更改存储在核心数据中。该应用程序的名称是iTunes商店中的Wizard Blood。到目前为止,我已经取得了一些成功,我的-didSelectRow方法通过以下代码成功地将新对象添加到核心数据中: // Get context, entity, managedobject from the fetchedResultsController [newManagedObject setValue:[NSDate dat
// 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]);
}
然而,在my-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方法,提前感谢您的帮助。如果您自上次查询后更改了数据,您可能需要删除fetchedResultsController的缓存吗
[NSFetchedResultsController deleteCacheWithName:@"(cachename)"];
缓存名称在initWithFetchRequest中设置
如果这不起作用,请尝试在数据库更改后将fetchedResultsController设置为nil,这将强制它重新执行请求。是的,这两个都不起作用。我甚至没有尝试删除缓存,而是将其设置为nil。我也有95%的把握缓存会自动更新,而删除缓存的唯一原因是释放内存。在提取之前,我还尝试将fetchedResultsController设置为nil,但也不起作用。谢谢你的努力。