MagicalRecord 和 CoreData 在不同上下文中保存错误
MagicalRecord and CoreData saving in different context error
所以我有一些方法可以使用 MagicalRecord 将我的设置保存在 CoreData 中。但是后来我试图这样做,我得到这个错误:Illegal attempt to establish a relationship 'settings' between objects in different contexts
所以这是我的代码:
此方法由正在使用程序的特定用户保存数据
-(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 获取设置:
-(SettingsData*)settingsDataForCurrentUserInContext:(NSManagedObjectContext*)context{
NSLog(@"In settingsDataForCurrentUserInContext");
SettingsData *settings = [SettingsData MR_findFirstByAttribute:@"settings" withValue:[[CacheManager shared] currentUserWithContext:context]];
return settings;
}
最后是从 CoreData 获取当前用户的 userData 的方法:
-(UserData*)currentUserWithContext:(NSManagedObjectContext*)context{
UserData *persons = [UserData MR_findFirstInContext:context];
if (persons!=nil) {
NSLog(@"Current user with context not nil value");
}
return persons;
}
我需要帮助来认识到这是我的错误,因为对我来说这一切似乎都是合乎逻辑的。
您在获取 SettingsData
时使用的是默认上下文。所以,改变:
SettingsData *settings = [SettingsData MR_findFirstByAttribute:@"settings" withValue:[[CacheManager shared] currentUserWithContext:context]];
至:
SettingsData *settings = [SettingsData MR_findFirstByAttribute:@"settings" withValue:[[CacheManager shared] currentUserWithContext:context] inContext: context];
(免责声明:在浏览器中输入,未测试拼写错误)
所以我有一些方法可以使用 MagicalRecord 将我的设置保存在 CoreData 中。但是后来我试图这样做,我得到这个错误:Illegal attempt to establish a relationship 'settings' between objects in different contexts
所以这是我的代码: 此方法由正在使用程序的特定用户保存数据
-(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 获取设置:
-(SettingsData*)settingsDataForCurrentUserInContext:(NSManagedObjectContext*)context{
NSLog(@"In settingsDataForCurrentUserInContext");
SettingsData *settings = [SettingsData MR_findFirstByAttribute:@"settings" withValue:[[CacheManager shared] currentUserWithContext:context]];
return settings;
}
最后是从 CoreData 获取当前用户的 userData 的方法:
-(UserData*)currentUserWithContext:(NSManagedObjectContext*)context{
UserData *persons = [UserData MR_findFirstInContext:context];
if (persons!=nil) {
NSLog(@"Current user with context not nil value");
}
return persons;
}
我需要帮助来认识到这是我的错误,因为对我来说这一切似乎都是合乎逻辑的。
您在获取 SettingsData
时使用的是默认上下文。所以,改变:
SettingsData *settings = [SettingsData MR_findFirstByAttribute:@"settings" withValue:[[CacheManager shared] currentUserWithContext:context]];
至:
SettingsData *settings = [SettingsData MR_findFirstByAttribute:@"settings" withValue:[[CacheManager shared] currentUserWithContext:context] inContext: context];
(免责声明:在浏览器中输入,未测试拼写错误)