目标c - 核心数据保存方法 [英] Objective c - Core Data saving approach

查看:103
本文介绍了目标c - 核心数据保存方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中有一些 NSManagedObject 子类,我试图理解何时以及如何保存更改。

我将尝试解释自己,例如类 A NSManagedObject 子类。

在应用程序生命周期中:

I have some NSManagedObject subclass in my app, and I'm trying to understand when and how to save changes.
I will try to explain myself, for example class A is NSManagedObject subclass.
During app life cycle I do:

App launched
...
Create an instance of class A
...
Change some properties of A instance
...
App go to background
...
App becomes active again
...
Change some more properties of A instance
...
App terminates

我什么时候需要调用[context save:]?

我在每次更改A实例后调用它?或者我可以只在应用程序转到背景时才调用它?

在创建或删除任何A实例后应该调用它吗?

When do I need to call [context save:] ??
Do I call it after every change in A instance? Or maybe I call it only when app go to background?
Should I call it after creation or deletion of any A instance?

推荐答案

一个很好的方法是将UIManagedDocument放在AppDelegate中。然后,每当应用程序发生某些更改时(如崩溃),您可以调用[上下文保存]。我喜欢遵循的顺序是:

A nice approach is place UIManagedDocument in your AppDelegate. Then you can call [context save] whenever some change occurs in the app (like a crash). The order I like to follow is something like:


  1. 创建UIManagedDocument对象(在应用程序加载或任何位置)
    并分配到属性

  2. 设置文档(检查文件是否存在于磁盘上或已经打开等)并相应地做出响应)

  3. 传递UIManagedObjectContext到您应用程序中的初始UIViewController(从那里您可以将上下文传递给其他视图控制器)

UIM管理文档将保存上下文您。

UIManaged document will save the context for you.

查看UIManagedDocument文档以配置持久存储选项(向UIManagedDocument实例发送一个NSDictionary选项,请参阅下面的链接查看第一个示例)。

Take a look at the UIManagedDocument documentation to configure persistent store options (you send an NSDictionary of options to your UIManagedDocument instance, see the first example through the link below).

UIManagedDocument文档:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIManagedDocument_Class/Reference/Reference.html

UIManagedDocument documentation: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIManagedDocument_Class/Reference/Reference.html

还可以参阅iPhone和iPad应用程序开发课程的CoreData演讲和演示(第13和14课),并在iTunesU上免费提供Paul Hegarty(2011年秋季)。

Also see the CoreData lecture and demo (lectures 13 and 14) of the iPhone and iPad application development course with Paul Hegarty available free on iTunesU (Fall 2011).

这篇关于目标c - 核心数据保存方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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