Using Core Data Concurrently and Reliably

后端 未结 4 760
感动是毒
感动是毒 2021-02-06 08:41

I\'m building my first iOS app, which in theory should be pretty straightforward but I\'m having difficulty making it sufficiently bulletproof for me to feel confident submittin

相关标签:
4条回答
  • 2021-02-06 09:28

    Pre-iOS 5, we've usually had two NSManagedObjectContexts: one for the main thread, one for a background thread. The background thread can load or delete data and then save. The resulting NSManagedObjectContextDidSaveNotification was then passed (as you're doing) to the main thread. We called mergeChangesFromManagedObjectContextDidSaveNotification: to bring those in to the main thread context. This has worked well for us.

    One important aspect of this is that the save: on the background thread blocks until after the mergeChangesFromManagedObjectContextDidSaveNotification: finishes running on the main thread (because we call mergeChanges... from the listener to that notification). This ensures that the main thread managed object context sees those changes. I don't know if you need to do this if you have a parent-child relationship, but you did in the old model to avoid various kinds of trouble.

    I'm not sure what the advantage of having a parent-child relationship between the two contexts is. It seems from your description that the final save to disk happens on the main thread, which probably isn't ideal for performance reasons. (Especially if you might be deleting a large amount of data; the major cost for deletion in our apps has always happened during final save to disk.)

    What code are you running when the controllers appear/disappear that could be causing core data trouble? What kinds of stack traces are you seeing the crash on?

    0 讨论(0)
  • 2021-02-06 09:29

    NSFetchedResultsController has been proven to be a bit sensitive to massive deletes so that is where I would start digging first.

    My initial question is, how are the re-fetch and reload of tableview related to the start of delete operation. Is there a chance that the deletion block will save child MOC while the NSFetchedResultsController is still fetching or no?

    Is it possible that when you switch from detail view to master and then back to detail view there will be multiple concurrent background tasks running? Or are you retrieving all the data from the web service at once not only that relevant to a particular row?

    One alternative to make this more robust is to use a pattern similar to what UIManagedDocument uses:

    Instead of using a parent MOC as Main Thread concurrency type, UIManagedDocument actually creates the main MOC as private queue and makes the child MOC available to you use on the main thread. The benefit here is that all the I/O goes on in the background and saves to the parent MOC does not interfere with the child MOC at all until child MOC is explicitly made know about them. That's because save commits changes from child to parent and not the other way around.

    So if you did your deletes on a parent queue which is private, that would not end up in the NSFetchedResultsController scope at all. And since it is old data, that is actually the preferred way.

    An alternative I offer is to use three contexts:

    Main MOC (NSPrivateQueueConcurrencyType)

    • Responsible for persistent store and deletion of old data.

    Child MOC A (NSMainQueueConcurrencyType)

    • Responsible for anything UI related and NSFetchedResultsController

    Child MOC B (NSPrivateQueueConcurrencyType, child of Child MOC A)

    • Responsible for inserting new data and committing it up to Child MOC A when done.
    0 讨论(0)
  • 2021-02-06 09:30

    Just an architectural idea:

    With your stated data refresh pattern (once a day, FULL cycle of data deleted and added), I would actually be motivated to create a new persistent store each day (i.e. named for the calendar date), and then in the completion notification, have the table view setup a new fetchedresultscontroller associated with the new store (and likely a new MOC), and refresh using that. Then the app can (elsewhere, perhaps also triggered by that notification) completely destroy the "old" data store. This technique decouples the update processing from the data store that the app is currently using, and the "switch" to the new data might be considered dramatically more atomic, since the change happens simply be starting to point to the new data instead of hoping you aren't catching the store in an inconsistent state while new data is being written (but is not yet complete).

    Obviously I have left some details out, but I tend to think that much data being changed while being used should be re-architected to reduce the likelihood of the kind of crash you are experiencing.

    Happy to discuss further...

    0 讨论(0)
  • 2021-02-06 09:31

    The main issue I've had with multi-threaded core data is inadvertently accessing a managed object in a thread/queue other than the one it was created in.

    I've found a good debugging tool is add NSAsserts to check that to managed objects created in your main managed object context are only used there, and ones created in a background context aren't used in the main context.

    This will involve subclassing NSManagedObjectContext and NSManagedObject:

    • Add a iVar to the MOC subclass and assign to it the queue it was created on.
    • Your MO subclass should check the current queue is the same as its MOC's queue property.

    It's only a few lines of code, but long term can prevent you making errors that are hard to track down otherwise.

    0 讨论(0)
提交回复
热议问题