所以我有一些方法可以使用MagicalRecord將我的設置保存在CoreData中。但後來我試圖做到這一點,我得到這個錯誤:Illegal attempt to establish a relationship 'settings' between objects in different contexts
MagicalRecord和CoreData在不同的上下文中保存錯誤
因此,這裏是我的代碼: 此方法由特定的用戶保存數據,誰在使用PROGRAMM現在
-(void)saveSettingsFirst:(BOOL)first{
[MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext){
SettingsData *newData = [self settingsDataForCurrentUserInContext:localContext];
//SettingsData *newData = [SettingsData MR_createEntityInContext:localContext];
newData.firstValue = @(first);
NSLog(first ? @"saveSettings FIRST 0" : @"saveSettings FIRST 1");
newData.settings = [[CacheManager shared] currentUserWithContext:localContext];
NSLog(@"Settings one is saved");
}];
}
這種方法採取從CoreData的currentUser設置:
-(SettingsData*)settingsDataForCurrentUserInContext:(NSManagedObjectContext*)context{
NSLog(@"In settingsDataForCurrentUserInContext");
SettingsData *settings = [SettingsData MR_findFirstByAttribute:@"settings" withValue:[[CacheManager shared] currentUserWithContext:context]];
return settings;
}
最後的方法,誰從CoreData獲取用戶數據爲當前用戶:
-(UserData*)currentUserWithContext:(NSManagedObjectContext*)context{
UserData *persons = [UserData MR_findFirstInContext:context];
if (persons!=nil) {
NSLog(@"Current user with context not nil value");
}
return persons;
}
我需要幫助來認識是我的錯誤,因爲對我來說這似乎是邏輯。
歐凱,它的作品!謝謝! 你可以解釋或給出鏈接閱讀爲什麼這個工程。因爲我已經使用上下文加載用戶併成功加載。但正如我所看到的,我們需要在相同的環境下進行搜索,對吧? –
是的,總是使用相同的上下文。如果您檢查MR碼,您會看到我們的原始線路使用默認上下文。但是你傳遞了一個本地上下文,所以你應該調整。 – Koen
另請參閱此處:https://github.com/magicalpanda/MagicalRecord/wiki/Fetching-Entities#finding-entities-in-a-specific-context – Koen