我正在使用MagicalRecord,我無法理解如何使其工作穩定且可預測。如何使用MagicalRecord正確保存更新?
當我需要更新一些實體,我從數據庫檢索它們,根據邏輯改變他們,之後那我送他們到我的「節電器」的方法:
- (void) saveEntities:(NSArray *)entities {
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
for (Entity_class *entityElement in entities) {
NSPredicate *entitySearchPredicate = [...] // Composing predicate
Entity_class *foundEntity = [Entity_class MR_findFirstWithPredicate:entitySearchPredicate];
foundEntity = entityElement;
}
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
} completion:^(BOOL contextDidSave, NSError *error) {
// contextDidSave always equals NO. Sometimes changes get saved, but sometimes they don't
}];
}
我試圖挽救本地上下文[localContext MR_saveToPersistentStoreAndWait]
而不是默認的,但它從來沒有工作。
第二天晚上我正在爲這些背景努力工作,只是我已經用完了Google的搜索查詢變體。如何處理上下文並正確保存它們?
我只是試圖挽救實體'的背景下[entityElement.managedObjectContext MR_saveToPersistentStoreAndWait]',它的工作原理。我仍然不明白,爲什麼'saveWithBlock:'保存如此難以預測。如果我只需要保存實體的上下文,爲什麼在這個世界上存在這種方法呢? –