Delete/Reset all entries in Core Data?

后端 未结 30 3194
天命终不由人
天命终不由人 2020-11-22 16:00

Do you know of any way to delete all of the entries stored in Core Data? My schema should stay the same; I just want to reset it to blank.


Edit

30条回答
  •  不知归路
    2020-11-22 16:30

    Thanks for the post. I followed it and it worked for me. But I had another issue that was not mentioned in any of the replies. So I am not sure if it was just me.

    Anyway, thought I would post here the problem and my way that solved it.

    I had a few records in the database, I wanted to purge everything clean before write new data to the db, so I did everything including

    [[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error]; 
    

    and then used managedObjectContext to access the database (supposed to be empty by now), somehow the data was still there. After a while of troubleshooting, I found that I need to reset managedObjectContext, managedObject, managedObjectModel and persistentStoreCoordinator, before I use managedObjectContext to access the dabase. Now I have a clean database to write to.

提交回复
热议问题