Is NSPersistentStoreCoordinator Thread Safe?

前端 未结 2 552
攒了一身酷
攒了一身酷 2021-02-05 23:04

I\'m working on an iPhone app that uses Core Data. The app makes a call to a web service, parses the resulting XML file, and then creates or modifies Core Data objects in my app

相关标签:
2条回答
  • 2021-02-05 23:35

    Is it possible to have a second managed object context running on a separate thread sharing the same NSPersistentStoreCoordinator with the main thread?

    Yes! I recommend you to read the corresponding section of Zarra's book carefully before venturing into multi-threaded access to CoreData.

    Or, more succinctly, is NSPersistentStoreCoordinator thread-safe?

    Well, the word `thread-safe' has shades of meaning in the Apple doc, so be very careful! See this blog article for a nice explanation.

    0 讨论(0)
  • 2021-02-05 23:45

    Zarra later points out that the context locks the store, so it is ok to use the same NSPersistentStoreCoordinator across threads.

    Although the NSPersistentStoreCoordinator is not thread safe either, the NSManagedObjectContext knows how to lock it properly when in use. Therefore, we can attach as many NSManagedObjectContext objects to a single NSPersistentStoreCoordinator as we want without fear of collision.

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