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

后端 未结 9 467
终归单人心
终归单人心 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:03

    Update re. XCODE 4.6.3 and MACOSX 10.9.5:

    To solve the problem I removed the app directory related to the product in xcode derived data location:

    rm -rd /Users//Library/Developer/Xcode/DerivedData/. Apparently the data gets messed up after playing with the object model.

    0 讨论(0)
  • 2020-12-24 03:06

    I solved it by removing the app from simulator and run it again. I guess it happens because the previous version of app does not have core data in it.

    0 讨论(0)
  • 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).
    0 讨论(0)
  • 2020-12-24 03:16

    I saw this error during testing when I spun up some temporary NSManagedObjectContexts without hanging on to them anywhere - the context would get deallocated out from under me at random by ARC.

    What wound up working was to hold a reference to the MOC on the tests and reset it for each test.

    Not sure how relevant this is to non-test circumstances, but particularly if you're using child/sibling contexts, it's worth making sure they're actually still there. :P

    0 讨论(0)
  • 2020-12-24 03:22

    I have also stuck with this problem. For that I have first check core data migration code Since the code was correct in my case, I have delete my app from device and reinstall it. And it works.

    0 讨论(0)
  • 2020-12-24 03:22

    For me, I found addPersistentStoreWithType always returns nil in my case. So the persistent store wasn't created successfully. Then, I print the storeURL

    ~/Library/Developer/CoreSimulator/Devices/1F8C6299-1F80-4212-9FE2-CA33BD365851/data/Containers/Data/Application/9CA7B7BB-A835-4BC2-B12D-23B84D420F91/Library/Documentation/my-database.sqlite
    

    This is a directory in which we can't write data. So I checked my code and found a silly mistake.

    • I should use NSDocumentDirectory instead of NSDocumentationDirectory. Code completion in Xcode offers the documentation directory before the document directory enum.

    • When I used NSDocumentationDirectory, NSPersistentStoreCoordinator always tried to create the persist store in .../Library/Documentation/my-database.sqlite, which is an invalid path.

    • Instead, the correct path should be .../Documents/BeeLocation.sqlite.
    0 讨论(0)
提交回复
热议问题