Core Data multi thread application

前端 未结 3 1699
天命终不由人
天命终不由人 2020-11-29 14:49

I\'m trying to use core data in a multi thread way. I simply want to show the application with the previously downloaded data while downloading new data in background. This

3条回答
  •  难免孤独
    2020-11-29 15:30

    The Apple Concurrency with Core Data documentation is the place to start. Read it really carefully... I was bitten many times by my misunderstandings!

    Basic rules are:

    1. Use one NSPersistentStoreCoordinator per program. You don't need them per thread.
    2. Create one NSManagedObjectContext per thread.
    3. Never pass an NSManagedObject on a thread to the other thread.
    4. Instead, get the object IDs via -objectID and pass it to the other thread.

    More rules:

    1. Make sure you save the object into the store before getting the object ID. Until saved, they're temporary, and you can't access them from another thread.
    2. And beware of the merge policies if you make changes to the managed objects from more than one thread.
    3. NSManagedObjectContext's -mergeChangesFromContextDidSaveNotification: is helpful.

    But let me repeat, please read the document carefully! It's really worth it!

提交回复
热议问题