Objective c - Core Data saving approach

前端 未结 3 1253
伪装坚强ぢ
伪装坚强ぢ 2021-01-28 12:38

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

3条回答
  •  醉梦人生
    2021-01-28 13:04

    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. Create UIManagedDocument object (in application did load or wherever) and assign it to a property
    2. Setup the document (check whether it exist on disk or is already open, etc.. and respond accordingly)
    3. Pass the UIManagedObjectContext to the initial UIViewController in your app (from there you can pass the context to other view controllers)

    UIManaged document will save the context for you.

    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 documentation: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIManagedDocument_Class/Reference/Reference.html

    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).

提交回复
热议问题