【问题标题】:MagicalRecord not savingMagicalRecord不保存
【发布时间】:2017-05-20 16:10:29
【问题描述】:

我想知道为什么我的 Core Data 停止保存更改。事实上,上面的代码在几个小时前就已经工作了。当我尝试打印错误时,它会打印(null)。下面的代码是NSManagedObjectSubclass的自定义方法:

-(void)bindWithModel:(MenuAPIModel*)model{

    self.basketId = [model.basketId integerValue];
    self.coreId = [model.itemId integerValue];
    self.name = [model name];
    self.orderId = [model.orderId integerValue];
    self.payedFrom = [model payedFrom];
    self.persons = [model persons];
    self.price = [model.price integerValue];
    self.price3 = [model.price3 integerValue];
    self.price12 = [model.price12 integerValue];
    self.status = [model status];

     [[NSManagedObjectContext MR_defaultContext] MR_saveOnlySelfWithCompletion:^(BOOL contextDidSave, NSError * _Nullable error) {

    NSLog(@"error %@", error.localizedDescription);

}];

错误为空,contextDidSave 为 YES。但是当我尝试访问实体时,它会打印 null,并且 SQL 表是空的。为什么?

【问题讨论】:

    标签: ios core-data magicalrecord


    【解决方案1】:

    我假设bindWithModel 方法在NSManagedObject 子类中。如果是这样,那么您应该使用此类中的managedObjectContext 属性而不是MR_defaultContext

    [self.managedObjectContext  MR_saveOnlySelfWithCompletion:^(BOOL contextDidSave, NSError * _Nullable error) { (...) }];
    

    以前它工作可能是因为来自[NSManagedObjectContext MR_defaultContext] 的上下文与self.managedObjectContext 相同。

    【讨论】:

    • 感谢您的帮助,但它确实保存并且数据存在。问题是 - 我不知道它在哪里存储数据。沙盒中的数据库文件为空
    • 如果沙箱中的数据库文件为空,则表示数据未保存。您如何验证数据是否已保存?也许尝试在其他地方使用MR_defaultContext(或一些新的上下文)来获取这些数据,以确保这些数据存在。
    • 我可以通过在控制台记录数据来验证,是正确的
    • 但是您从哪个来源获取这些数据?如果此数据的上下文相同,那么您将看到所有数据。如果数据仍然可用,也许可以尝试杀死应用程序并再次打开,然后问题出在其他地方:(
    • 当我尝试记录文件路径时,它会打印名为 CoreData.sqlite 的其他文件,但即使我使隐藏文件可见,我也无法在 finder 中找到它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 2019-08-30
    • 1970-01-01
    • 2013-03-11
    相关资源
    最近更新 更多