我在寫一個應用程序,它使用UIPickerView並將每個更改存儲在覈心數據中的採集器中(應用程序的名稱是iTunes商店中的嚮導血液)。我已經有一些成功,我-didSelectRow方法通過下面的代碼成功添加到核心數據的新對象至今:如何從Core Data中檢索最新的對象?
// 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方法,感謝您提前給予幫助。
是的,這些都沒有工作。我甚至不想刪除緩存,而只是將它設置爲零。我也95%確定緩存會自動更新,而您將其刪除的唯一原因是釋放內存。我也嘗試將fetchedResultsController設置爲在提取之前爲零,但它也不起作用。感謝您的嘗試。 – 2010-09-12 16:19:08