ManagedObjectContext 似乎删除了我的 NSUserDefault
保存 ManagedObjectContext 似乎会吃掉
NSString *defaultSiteUrl = [[NSUserDefaults standardUserDefaults] objectForKey:kSelectedSiteUrlKey];
NSLog(@"the default site url is: %@", defaultSiteUrl);
if (![[managedObjectContext] save:&error]) {
NSLog(@"Error saving entity: %@", [error localizedDescription]);
}
NSString *defaultSiteUrl2 = [[NSUserDefaults standardUserDefaults] objectForKey:kSelectedSiteUrlKey];
NSLog(@" the default site url is: %@", defaultSiteUrl2);
控制台中的 NSUserDefault 结果:
2011-05-10 15:26:40.710 Moodle[44566:207] the default site url is: http://jerome.moodle.local/~jerome/Moodle_iPhone
2011-05-10 15:26:40.717 Moodle[44566:207] the default site url is: (null)
什么可能导致此问题? NSUserDefaults 以何种方式与 ManagedObjectContext 相关?
注意:
为了让您了解我的应用程序:我正在开发一个 Moodle 应用程序。这段代码位于显示课程列表的屏幕上。当我在“站点设置”屏幕上设置用户默认值时,我使用[[NSUserDefaults standardUserDefaults]同步]。所以我想它保存得很好
如果我在设置用户默认值后直接进入课程列表屏幕之前重新启动模拟器,那么这段代码可以工作(用户默认值不会被“保存manageObjectContext”删除)。
saving the managedObjectContext seems to eat the NSUserDefault
NSString *defaultSiteUrl = [[NSUserDefaults standardUserDefaults] objectForKey:kSelectedSiteUrlKey];
NSLog(@"the default site url is: %@", defaultSiteUrl);
if (![[managedObjectContext] save:&error]) {
NSLog(@"Error saving entity: %@", [error localizedDescription]);
}
NSString *defaultSiteUrl2 = [[NSUserDefaults standardUserDefaults] objectForKey:kSelectedSiteUrlKey];
NSLog(@" the default site url is: %@", defaultSiteUrl2);
Result in the console:
2011-05-10 15:26:40.710 Moodle[44566:207] the default site url is: http://jerome.moodle.local/~jerome/Moodle_iPhone
2011-05-10 15:26:40.717 Moodle[44566:207] the default site url is: (null)
What could cause this issue?
In which way is NSUserDefaults related with ManagedObjectContext?
Note:
To let you know my application: I'm developping a Moodle app. This piece code is on a screen displaying a list of courses. I use [[NSUserDefaults standardUserDefaults] synchronize] when I set the userdefault on a "site settings" screen. So I guess it's well saved
The piece of code works (the user defaults are not deleted by the "save manageObjectContext") if I restart the simulator before going to the course listing screen straight after setting the user default.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须通过[NSUserDefaults registerDefaults:appDefaults]初始化NSUserDefaults值,使用同步方法instiliaztioan可能会破坏您的用户默认值,注意,这与核心数据无关。
You must initialize the NSUserDefaults value by [NSUserDefaults registerDefaults:appDefaults], using synchronize method for instiliaztioan could destroy you user defaults, NOTE, it's nothing to do with core data.