【发布时间】:2014-01-07 15:16:12
【问题描述】:
给定如下界面:
@interface Country : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * isAggressive;
@end
我有一个屏幕,用户可以在其中看到Countries 的列表并切换isAgressive 标志。只有当用户点击应用时,选项才会被保存。他们还可以选择点击取消。
基于此,我使用以下代码在屏幕加载时加载所有国家/地区:
tempContext = [NSManagedObjectContext MR_context];
// Load our countries.
countries = [Country MR_findAllSortedBy: @"name"
ascending: YES
inContext: tempContext];
我在 tempContext 而不是默认上下文中这样做,因为我不希望这些对象干扰其他任何东西。
取消时,我没有做任何具体的事情。只允许tempContext 离开范围。在申请时,我正在尝试执行以下操作:
// Save changes.
[MagicalRecord saveWithBlock: ^(NSManagedObjectContext * saveLocalContext)
{
[countries enumerateObjectsUsingBlock: ^(Country * country, NSUInteger countryIndex, BOOL * stop)
{
[country MR_inContext: saveLocalContext];
}];
} completion:^(BOOL success, NSError *error) {
NSLog(@"Completed: %@, %@.", success ? @"true" : @"false", error.localizedDescription);
//This is called when data is in the store, and is called on the main thread
}];
然而,这似乎并没有做出任何改变。在调试中运行时,我收到以下日志消息:
[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x6000001dc020) **未命名**上下文没有变化 - 不保存 完成:假,(空)。
我的更新没有被保存。我应该如何正确处理更新的对象并执行保存?
【问题讨论】:
-
展示您如何创建“临时”上下文。
-
tempContext创建在示例代码中:tempContext = [NSManagedObjectContext MR_context];
标签: ios objective-c core-data magicalrecord