Monday 15 September 2014

core data - What is the proper usage of NSManagedObjectContexts when editing/creating new objects (iPhone)? -


I am trying to find out the proper way to use NSManagedObjectContexts when you are viewing NSManagedObjects, editing Are and are making. I think the documentation and examples have explained to them how to use them in the most basic cases, but I'm not sure what is in a slightly more complicated setup than in the right way.

Setup:

  • 3 main screen: A list of objects, edit the object screen, and a new object screen.
  • List the background to add another thread objects.

Needs:

  • The list screen uses an MOC and an NSFetchedResultsController to get all these items.
  • Use MOCs to edit and save / delete new object screen objects and use NSFetchedResultsControllers for relationships.
  • Downloaded objects need a MOC so that objects can be saved in core data. Main thread).

Question:

  • How many MOCs are required?
  • How should manipulate these MOCs?
  • Possible answer:

    • A "View" MOC is never edited and this list Used in the screen. Use different MOCs for editing, new object screens and downloads when these MOCs save, then merge the changes back to "View" MOC. The "view" does not affect the MOC until such changes are saved. This is what I am doing; It does not seem that I am working smoothly because I hope to have a disconnect between editing and seeing and being able to check those things, who know that they can change. NSFetchedResultsController will have to wait for delegate methods so that they can complete every possible thing that may have changed, so it makes it even harder if I need to change some data in the list list.

    • There is an MOC for everything that I did for the first time, but it was not sure how to deal with editing and making. Now that I understand a bit more, I think I can edit the object or create an object and can cancel it on the rollback , I saw something that seemed like one Who had asked to create an undo group around the return when editing / creating and canceling, then I think that I can use a different MOC on the downloaded object as it Using It may end and edits in the main MOC.

    • Anyway, do not know what is the proper way can you help me?

    Disconnect the example for the first possible answer

    1. Save an object (1) edit moc. Notified with mock by notification.
    2. A new mock was created because I downloaded items in the background. Some items have been updated which are related to (1). Rescued. Merge with moc seen by notification.
    3. Edit (1) in the editing moc. Rescued. Notified with mock by notification.
    4. Problem: Because the edit moc never got a new mock change, when it saves it, it removes all the new mock changes it affects.
    5. SOLUTION: I realize that I can merge editing changes in Mach or I can always use a new Mock to edit things. However, I am moving in small things like this and want to find a solution, so it leaves me to believe that this is not the best answer.
    6. You must have at least one MOC per thread (they are not secure). You can do an MOC and other activity for the activity in the main thread list, for the downloader (in the background thread), and edit the new one.

      When you say that a disconnected, can you be more specific? Are you using Notifications ( NSManagedObjectContextDidSaveNotification ), and when you receive that information, then mergeChangesFromContextDidSaveNotificationNotificationNotification are done, remember that the merge on the main code Changes should be made to change the communication notification . In your visual controller with NSFetchedResultsController , you are handling all the cases of NSFetchedResultsControllerDelegate


No comments:

Post a Comment