Using Core Data Concurrently and Reliably

后端 未结 4 761
感动是毒
感动是毒 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: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.

提交回复
热议问题