1

I'm not using Core Data for persistent storage at all, I'm using the in-memory store type because I really only need Core Data for its database-like benefits.

In some cases, I'll load a view controller and get data from an API, and populate Core Data with the API's response objects which then populates the view controller through NSFetchedResultsController.

However, I'll then go to a different view controller, completely not needing the previous one, and make a new API call and need to populate Core Data with those response objects.

My issue is, when I bring in the new ones my old ones are still there. How do I quickly tell the old ones to be gone and to make way for just the new ones?

I tried deleting all the entries manually as shown in this answer but if I load new entries directly after that it crashes my app.

Is there a better way to be doing this?

Community
  • 1
  • 1
user212541
  • 1,878
  • 1
  • 21
  • 30
  • Go Through this link hope it can help you http://stackoverflow.com/questions/14727583/clearing-coredata-and-all-that-inside – Pandey_Laxman Apr 21 '14 at 04:12

2 Answers2

1

Since you're using an in-memory store, you can skip all of the steps people describe for clearing out persistent stores. In-memory stores are objects, so you get rid of them the same way as you get rid of any other object, i.e. by setting any references to the object to nil and letting ARC take care of cleaning them up. Then you can create a completely new store that has no data, and work with that.

It's slightly more complicated because you need to deal with any managed objects you've fetched, but if you don't keep any references, they'll go away as well. It's still just letting objects get deallocated like any other object though.

The easiest approach with an in-memory store is:

  1. Set any references to any object associated with the persistent store to nil. This includes all managed objects, the managed object context, and the persistent store and persistent store coordinator.

  2. Create a new persistent store, persistent store coordinator, and managed object context. Do this the same way you created it the first time.

There is no step 3, and no need to explicitly delete all of the contents of the persistent store.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Also, why would I have a persistent store if I'm not saving to disk? – user212541 Apr 24 '14 at 04:16
  • You have a persistent store because that's how Core Data works. An in-memory store is still a persistent store. I don't have any code examples of how to set a variable to have a nil value, sorry. – Tom Harrington Apr 24 '14 at 16:18
0

This is how I reset Core Data:

//  reset core data
[managedObjectContext lock];

NSPersistentStoreCoordinator *persistentStoreCoordinator = managedObjectContext.persistentStoreCoordinator;

for(NSPersistentStore *persistentStore in persistentStoreCoordinator.persistentStores) 
{
    [persistentStoreCoordinator removePersistentStore:persistentStore error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:persistentStore.URL.path error:nil];
}

[managedObjectContext unlock];

I use this is my app to clear it all out and start fresh. Hope this is what you are looking for. Apologies if it isn't.

Infinity James
  • 4,667
  • 5
  • 23
  • 36
  • On iOS 7 that's going to leave behind a lot of extra data, unless you previously made sure to change the SQLite journal mode to "delete". By default there's also a `wal` file in the same directory (which can be very large) and a `shm` file. – Tom Harrington Apr 21 '14 at 17:52