【问题标题】:Magical Record, saving objects神奇的记录,保存对象
【发布时间】:2014-08-04 21:48:12
【问题描述】:

我在我的 iOS 应用程序中使用 MR。我使用它的方式与以前的应用程序相同,但我的对象没有保存在数据库中。

我从调试中看到 Magical Record 在setupAutoMigratingCoreDataStack 方法中正确找到了*.sqlite 数据库

这里是一些示例测试代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [MagicalRecord setupAutoMigratingCoreDataStack];

    NSManagedObjectContext *sampleContext = [NSManagedObjectContext MR_defaultContext];

    NSLog(@"hasChanges: %d", [sampleContext hasChanges]);
    self.entity = [MyEntity MR_createEntityInContext:sampleContext];
    NSLog(@"hasChanges: %d", [sampleContext hasChanges]);

    [sampleContext MR_saveOnlySelfWithCompletion:^(BOOL success, NSError *error) {
        NSLog(@"save: %d %@", success, error);
    }];

    return YES;
}

这是输出:

 hasChanges: 0
 hasChanges: 1
 save: 1 (null)

我重新打开应用程序,发现本地数据库 ([MyEntity MR_findAll]) 中没有保存任何实体,我还打开 *.sqlite 数据库,发现 ZMyEntity 表为空。

这种行为的原因是什么?

提前致谢!

【问题讨论】:

    标签: ios sqlite core-data magicalrecord


    【解决方案1】:

    如果您希望将更改推送到商店,请使用 MR_saveToPersistentStore* 变体。 MR 使用嵌套上下文,因此,您可以选择只保存一个级别,或者保存到存储中。我们将此作为明确的代码选择,以帮助您在不编写所有样板代码的情况下决定您想要的选项。

    【讨论】:

    • 谢谢,对我来说工作得很好,也感谢那个 azaming 框架
    猜你喜欢
    • 1970-01-01
    • 2012-06-14
    • 2014-02-24
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多