CoreData 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'

后端 未结 9 506
终归单人心
终归单人心 2020-12-24 02:58

I have been following these instructions to help integrate iCloud support with CoreData and I am getting some errors.

I have this in my AppDelegate:

         


        
9条回答
  •  春和景丽
    2020-12-24 03:14

    You are probably calling persistentStoreCoordinator method from two (or more) different threads. Either directly or indirectly.

    As written that method is not thread safe.

    It may be you have other methods with the same problem. Typically managedObjectContext exists and is written in a similar way.

    There are two ways to solve this problem:

    • use @synchronize to make those methods thread safe
    • move the initialization code out of them and into an init method and modify them to assert/except if the ivar is nil

    The second thing that you may also be doing wrong is to modify the context (for example adding new managed objects to it) and then trying to save it, before the store has been initialized.

    To solve this make sure you do either one of the following:

    • you do not add any managed object to the context unless you know there is already at least one managed object in it (implying the store is loaded)
    • if you need to add a managed object to the context without checking that there's one already (for example after you initially create the empty store), make sure the store has been initialized already (for example do it when you get the RefetchAllDatabaseData notification and you notice the database is empty).

提交回复
热议问题