16

I have Core Data working in my app. So, I fetch an XML file, parse the data into model objects and insert them into core data. They are saved in the persistent store and I can access them when I relaunch the app. However, I want to be able to refresh the data in the persistent store at will, so I need to first remove existing objects from the store. Is there a straight-forward method for this?

Thanks


I found this solution:

[managedObjectContext lock];
[managedObjectContext reset];//to drop pending changes
if ([persistentStoreCoordinator removePersistentStore:persistentStore error:&error])
{
NSURL* storeURL = [NSURL fileURLWithPath:[self pathForPersistentStore]];
[[NSFileManager defaultManager] removeFileAtPath:[storeURL path] handler:nil];
[self addPersistentStore];//recreates the persistent store
}
[managedObjectContext unlock];
Julian
  • 9,299
  • 5
  • 48
  • 65
conorgriffin
  • 4,282
  • 7
  • 51
  • 88

8 Answers8

63

Here's what I have done to clean my Core Data entirely. It works perfectly. This is if you only have one persistent store which is probably the case if you didn't add one more manually. If your managedObjectContext has the same name as here you can simply copy/past it will work.

NSError * error;
// retrieve the store URL
NSURL * storeURL = [[managedObjectContext persistentStoreCoordinator] URLForPersistentStore:[[[managedObjectContext persistentStoreCoordinator] persistentStores] lastObject]];
// lock the current context
[managedObjectContext lock];
[managedObjectContext reset];//to drop pending changes
//delete the store from the current managedObjectContext
if ([[managedObjectContext persistentStoreCoordinator] removePersistentStore:[[[managedObjectContext persistentStoreCoordinator] persistentStores] lastObject] error:&error])
{
    // remove the file containing the data
    [[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];
    //recreate the store like in the  appDelegate method
    [[managedObjectContext persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];//recreates the persistent store
}
[managedObjectContext unlock];
//that's it !
Nicolas Manzini
  • 8,379
  • 6
  • 63
  • 81
  • Thanks, very nice! A question: How do we use 'NSError ** error' with ARC? It gives the error: 'Pointer to non-const type "NSError **" with no explicit ownership'. – Timo Jun 18 '12 at 00:07
  • yeah right you must declare it with some preinfo like _SomethingIDontRemeber NSError ** error, or just pass nil if you dont want the error it will be easier... ;) – Nicolas Manzini Jun 18 '12 at 07:11
  • 7
    Or just change the first line to 'NSError *error' and from then on, reference '&error'. – Joshua C. Lerner Nov 02 '12 at 02:30
  • @NicolasManzini, Your code, is not working for me.check this out http://stackoverflow.com/questions/14646595/how-to-clear-reset-all-coredata-in-one-to-many-realationship – Ranjit Feb 04 '13 at 07:04
  • Thank you, this is exactly what I need. In this answer, they also mention that as of iOS 5, we still need to delete the external binary storage also if it exists. http://stackoverflow.com/a/1222145/1672161 – abc123 Apr 17 '13 at 17:30
  • hi @NicolasManzini, can you please look at my question http://stackoverflow.com/questions/19358095/core-data-reset – Ranjit Oct 14 '13 at 14:41
  • Worked flawlessly! :) – Keith OYS Jun 16 '14 at 08:46
  • 3
    Note that lock is deprecated. The solution should be using performBlockAndWait: – olivaresF Jun 09 '15 at 01:43
8

swift version of @Nicolas Manzini answer:

if let psc = self.managedObjectContext?.persistentStoreCoordinator{

        if let store = psc.persistentStores.last as? NSPersistentStore{

            let storeUrl = psc.URLForPersistentStore(store)

            self.managedObjectContext?.performBlockAndWait(){

                self.managedObjectContext?.reset()

                var error:NSError?
                if psc.removePersistentStore(store, error: &error){
                    NSFileManager.defaultManager().removeItemAtURL(storeUrl, error: &error)
                    psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeUrl, options: nil, error: &error)
                }
            }
        }
    }
Ben
  • 3,832
  • 1
  • 29
  • 30
7

Based on @Nicolas Manzini answer I have wrote a Swift 2.1 version with little improvements. I have added an extension to NSManagedObjectContext. Full code below:

import Foundation
import CoreData

extension NSManagedObjectContext
{
    func deleteAllData()
    {
        guard let persistentStore = persistentStoreCoordinator?.persistentStores.last else {
            return
        }

        guard let url = persistentStoreCoordinator?.URLForPersistentStore(persistentStore) else {
            return
        }

        performBlockAndWait { () -> Void in
            self.reset()
            do
            {
                try self.persistentStoreCoordinator?.removePersistentStore(persistentStore)
                try NSFileManager.defaultManager().removeItemAtURL(url)
                try self.persistentStoreCoordinator?.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
            }
            catch { /*dealing with errors up to the usage*/ }
        }
    }
}
Julian
  • 9,299
  • 5
  • 48
  • 65
2

There is a function

enter image description here

According to WWDC 242, you can use it for clearing a database.

Also there is a func replacePersistentStore which is replacing the current database with a selected one.

Nike Kov
  • 12,630
  • 8
  • 75
  • 122
1

You could loop through all objects and delete them by doing this:

[managedObjectContext deleteObject:someObject];

If you want to remove all objects it is probably fastest to delete the store and then recreate the CoreData stack.

Felix Lamouroux
  • 7,414
  • 29
  • 46
1

Trash your data file and remake it.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
1
import Foundation
import CoreData

extension NSManagedObjectContext
{
    func deleteAllData() {
        guard let persistentStore = persistentStoreCoordinator?.persistentStores.last else {
            return
        }

        guard let url = persistentStoreCoordinator?.url(for: persistentStore) else   {
            return
        }

        performAndWait { () -> Void in
            self.reset()
             do
            {
                try self.persistentStoreCoordinator?.remove(persistentStore)
                try FileManager.default.removeItem(at: url)
                try self.persistentStoreCoordinator?.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
             }
            catch { /*dealing with errors up to the usage*/ }
         }
    }
}

Thanks @Julian Krol - updated answer for Swift 5.1

blakeness
  • 211
  • 2
  • 4
  • So i put this on my file that i want to delete so badly because everytime i open xcode and select core data the same file always pop up exactly but it still doesn't work like i mean exactly the code i written years ago everything in there with persistence.swift filled and core data filled as well its annoying i add it ontop of the old code on my persistence file. is it wrong? – arman Feb 16 '22 at 14:32
-4

The fastest way to ditch everything is to send your managed object context the reset message.

Giao
  • 14,725
  • 2
  • 24
  • 18
  • Won't that just empty the context but leave my objects in the persistent store? – conorgriffin Mar 04 '10 at 00:55
  • 8
    That is wrong. If you have objects in your persistent store these will get refetched with the next fetchRequest. All that reset does is to invalidate all references to managedObjects that the context currently holds. – Felix Lamouroux Mar 04 '10 at 09:46