0
我試圖更新核心數據中的某些記錄。我米採用以下步驟來完成它更新核心數據記錄<fault>
- 獲取與謂詞函數從核心數據
- 將結果存儲在一個對象數組
- 遍歷數組設置檢索記錄並更新每個記錄
- 呼叫保存上下文
餘米運行到兩個問題
- 初始運行後,我得到<故障>在日誌中
- 我不能確定是否保存上下文將保存對象
我的代碼:
- (void)fetchExpenses {
// Define our table/entity to use
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:managedObjectContext];
// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// Define how we will sort the records
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"publishTimestamp == nil"];
[request setPredicate:predicate];
// Fetch the records and handle an error
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (!mutableFetchResults) {
// Handle the error.
// This is a serious error and should advise the user to restart the application
}
// Save our fetched data to an array
[self setExpenseArray: mutableFetchResults];
[mutableFetchResults release];
[request release];
}
- (void) save: {
[self fetchExpenses];
int i = 1;
int max = [expenseArray count];
for(i=1; i<=max; i++) {
// Get the expense selected.
Expense *expense = [expenseArray objectAtIndex: i];
// Do your updates here
[expense setTimestamp:2]
}
}
Thanks TechZen如果我在Array中存儲fetchResults並對Array進行更改,然後調用save將它存儲在覈心數據中的數據? –
是的,這是管理對象的「管理」部分。針對上下文的提取返回由上下文管理的對象,上下文保存對對象的所有更改。 – TechZen