保存:对一个NSManagedObjectContext不工作 [英] save: on a NSManagedObjectContext not working

查看:87
本文介绍了保存:对一个NSManagedObjectContext不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在导航控制器中,我从视图控制器1转到视图控制器2.

In a navigation controller, I go from view controller 1 to view controller 2.

视图控制器1是一个绑定到FetchedResultsController的表视图控制器。它从特定上下文中获取数据并显示它。然后,如果轻击一行,则查看控制器2。虽然segueing,我使用视图控制器1中的数据设置视图控制器2的一个特定的NSManagedObject属性。

View Controller 1 is a table view controller tied to a FetchedResultsController. It fetches data from a specific context and displays it. I then segue to view controller 2 if a row is tapped. While segueing, I set a particular NSManagedObject property of view controller 2 using the data I have in view controller 1.

现在,在视图控制器2中,我能够显示数据使用NSManagedObject属性,然后还对其进行更改并执行save :.当我回到查看控制器1时,反映了变化。但是,如果我重新启动应用程序,它不会被任何视图控制器反射。

Now, in view controller 2 I am able to show data using the NSManagedObject property and then also make changes to it and perform a save:. When I go back to view controller 1 , the change is reflected. However, if I re-launch the app it is not being reflected any more in any of the view controllers.

这是我如何做保存。

- (void)hiddenAttributeSwitchSlid:(UISwitch *)sender
{
    [self.workoutType.managedObjectContext performBlock:^{
        self.workoutType.workoutHiddenByDefault = [NSNumber numberWithBool:sender.isOn];
        NSError *error = nil;
        if (![self.workoutType.managedObjectContext save:&error]) {
            NSLog(@"There was an error in saving to context - %@", [error localizedDescription]);
        }
        else {
            NSLog(@"No error");
        }
    }];
}

workoutType是一个NSManagedObject,它在prepareForSegue中设置:segueing to this view控制器。

workoutType is a NSManagedObject which is set in the prepareForSegue: before segueing to this view controller.

即使我不使用performBlock:,它也不起作用。

Even if I do not use the performBlock:, it doesn't work.

我知道这种问题以前被问过。我浏览过他们,但没有什么似乎工作。

PS - I know questions of this kind have been asked before. I browsed through them, but nothing seems to be working.

推荐答案

您是否为模型中的该属性设置了默认值?

Do you have a default value set for that attribute in your model?

Core Data中有一个已识别的错误,在某些情况下(我没有将其一直缩小),在应用程序的后台运行期间,具有默认值的属性会重置多次。

There is an identified bug in Core Data where in some situations (I have not narrowed it all the way down) where an attribute with a default value gets reset several times during the backgrounding of an application.

测试的方法是监听通过KVO更改的值,记录更改,然后复制测试。如果您看到几个更改,那么您知道您正在击中该错误。

The way to test for this is to listen for the value being changed via KVO and log the changes then duplicate your test. If you see several changes then you know you are hitting that bug.

我看到的唯一已知的解决方案是可靠的是删除默认值。如果需要默认值,那么我将它添加到 -awakeFromInsert 方法中的 NSManagedObject 子类中,然后更新验证方法来检查它。我知道了。

The only known solution I have seen that is reliable is to remove the default value. If a default value is required then I would add it to the NSManagedObject subclass in the -awakeFromInsert method and then update the validation methods to check for it. Sucks I know.

你有多少背景?

您是否使用父子上下文?

Are you using parent child contexts?

如果是,您是否保存最顶层的父级?

If so, are you saving the top most parent?

好的, UIManagedDocument 有两个 NSManagedObjectContext 里面的实例。是否有使用 UIManagedDocument 的原因?您的应用程序中是否有多个文档?如果你不这样,我强烈建议你切换回一个传统的核心数据堆栈。 UIManagedDocument 实际上并不是在单个堆栈应用程序中。

Ok, a UIManagedDocument has two NSManagedObjectContext instances inside of it. Is there a reason you are using a UIManagedDocument? Do you have more than one document in your application? If you don't then I strongly suggest you switch back to a traditional Core Data stack. The UIManagedDocument is not really meant to be in a single stack application.

至于保存问题, code> UIManagedDocument 尝试在退出应用程序时在后台保存。如果可以花一点时间,而且,个人,是不是很可靠。您可以在退出时请求保存,这将有助于确保保存快速,但即使这样也不可靠。

As for the saving issue directly, UIManagedDocument tries to save in the background when you exit the application. If can take a bit of time and, personally, is not very reliable. You can request a save on exit which will help to insure the save is speedy, but even then it can be unreliable.

如何退出应用程序?

你在Xcode中杀了它吗?

Are you killing it in Xcode?

您是否正在处理它,然后恢复它?

Are you backgrounding it and then resuming it?

设备?

您可以监听 UIManagedDocument 以保存并打印日志语句,保存实际发生在磁盘上。这可能有助于缩减正确的时间和不保存。

You can listen for the UIManagedDocument to save and then print a log statement so you can see when the saves actually happen to disk. That might be useful to help narrow down exactly when it is and is not saving.

这篇关于保存:对一个NSManagedObjectContext不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆