0

In my iOS project, when the user makes a logout I remove the database with the following code:

NSPersistentStore *store = [_persistentStoreCoordinator.persistentStores lastObject];
    NSError *error = nil;
    NSURL *storeURL = store.URL;
    [_persistentStoreCoordinator removePersistentStore:store error:&error];

    _persistentStoreCoordinator = nil;
    _managedObjectModel = nil;
    _managedObjectContext = nil;

    [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];

But after this, if I make a login I keep having the same documents as I used to have even the file has been previously removed. (Notice that the database don't change the name)

In the following link, Delete/Reset all entries in Core Data? One person added the following comment:

Update for iOS5+

With the introduction of external binary storage (allowsExternalBinaryDataStorage or Store in External Record File) in iOS 5 and OS X 10.7, simply deleting files pointed by storeURLs is not enough. You'll leave the external record files behind. Since the naming scheme of these external record files is not public, I don't have a universal solution yet. – an0 May 8 '12 at 23:00

Is already any way to can fix this issue? Thanks

Edit: Don't think it's relevant, but just in case, this are the properties for the class:

@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
Community
  • 1
  • 1
user3009804
  • 329
  • 6
  • 19

1 Answers1

1

Most likely you're leaving the existing WAL mode journal files behind. By default any Core Data persistent store file named something like Foo.sqlite will have journal files called Foo.sqlite-wal and Foo.sqlite-shm. If you don't remove those, most or all of your data will remain when you create a new Foo.sqlite.

External binaries can also be a problem.

The best approach is to put your Core Data file in a custom sub-directory. The journal files, external binaries, and whatever else Core Data thinks of in the future will go in that directory too. If you want to remove the persistent store, you can recursively remove everything in that directory without caring what the file names are.

Otherwise, remote the wal and shm files yourself. If you're using external binaries, figure out the name of the directory where those files live and remove that too.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170