Clearing CoreData and all that inside

后端 未结 4 666
日久生厌
日久生厌 2021-02-08 09:32

I\'m pretty new to CoreData and this app that uses it. And I\'m currently working on a feature that clears the entire core data when I log out in the app.

I have 2 sqlli

4条回答
  •  抹茶落季
    2021-02-08 10:08

    I use this function in the AppDelegate of one of my apps...

    - (void)deleteAllCoreData
    {
        NSPersistentStore *store = self.persistentStoreCoordinator.persistentStores[0];
        NSError *error;
        NSURL *storeURL = store.URL;
        NSPersistentStoreCoordinator *storeCoordinator = self.persistentStoreCoordinator;
        [storeCoordinator removePersistentStore:store error:&error];
        [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
    
        __persistentStoreCoordinator = nil;
        __managedObjectContext = nil;
        __managedObjectModel = nil;
    
        [self addDefaultData];
    }
    

    It deletes the persistent store that CoreData uses and leaves it so that a new one is set up when core data is accessed again.

    This is the method described in the second link you provided.

提交回复
热议问题