Delete/Reset all entries in Core Data?

后端 未结 30 2755
天命终不由人
天命终不由人 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:28

    MagicalRecord makes this very easy.

    [MyCoreDataObject MR_truncateAll];
    
    0 讨论(0)
  • 2020-11-22 16:29

    iOS 10 + Swift 3 solution:

    func clearCoreDataStore() {
        let delegate = UIApplication.shared.delegate as! AppDelegate
        let context = delegate.persistentContainer.viewContext
    
        for i in 0...delegate.persistentContainer.managedObjectModel.entities.count-1 {
            let entity = delegate.persistentContainer.managedObjectModel.entities[i]
    
            do {
                let query = NSFetchRequest<NSFetchRequestResult>(entityName: entity.name!)
                let deleterequest = NSBatchDeleteRequest(fetchRequest: query)
                try context.execute(deleterequest)
                try context.save()
    
            } catch let error as NSError {
                print("Error: \(error.localizedDescription)")
                abort()
            }
        }
    }
    

    Iterates through all of the core data entities and clears them

    0 讨论(0)
  • 2020-11-22 16:29

    Several good answers to this question. Here's a nice concise one. The first two lines delete the sqlite database. Then the for: loop deletes any objects in the managedObjectContext memory.

    NSURL *storeURL = [[(FXYAppDelegate*)[[UIApplication sharedApplication] delegate] applicationDocumentsDirectory] URLByAppendingPathComponent:@"AppName.sqlite"];
    [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
    for (NSManagedObject *ct in [self.managedObjectContext registeredObjects]) {
        [self.managedObjectContext deleteObject:ct];
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 16:30

    Delete sqlite from your fileURLPath and then build.

    0 讨论(0)
  • 2020-11-22 16:31

    As a quick reference to save searching elsewhere - recreating the persistent store after deleting it can be done with:

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
    // do something with the error
    }
    
    0 讨论(0)
提交回复
热议问题