Clearing CoreData and all that inside

后端 未结 4 665
日久生厌
日久生厌 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:02

    If your app can create the files then just remove them when the app quits. If you have some notion of a database that has valuable schema info, and you just need to truncate the file, that isn't really the case in core data... The schema is in the compiled entity models and xcwhatever files.

    Also I must have just read it wrong, because if you want core data to be not persistent then you are using the wrong tool.

    0 讨论(0)
  • 2021-02-08 10:04
    NSPersistentStoreCoordinator *storeCoordinator = self.persistentStoreCoordinator;
    [storeCoordinator removePersistentStore:store error:&error];
    [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
    

    So you're removing the persistent store from the persistent store coordinator, and then trying to delete the file. But just removing the store from the coordinator isn't enough to guarantee that the file associated with the store has been closed, and if it hasn't then the file system will probably prevent you from deleting the file. I can't tell from your question whether you're using ARC here, but since you say that it's old code there's a good chance that you aren't. Either way, if removing the store causes it to be autoreleased instead of released, or if you keep any other references to the store anywhere, then the store won't be deallocated just because you removed it from the coordinator, the file may remain open, and deleting the file will fail.

    A good way to see what's really going on, in addition to looking at the error object provided by -removeItemAtPath:error:, is to take a look in the file system and see whether the file is still there after you try to remove it. Go to the Organizer window, select your device and application, and download a copy of the app's sandbox. You should then be able to see whether the file is really being deleted.

    If you do see that the files are being deleted as you think they should be, then look for other ways that the data may be restored. If UserA logs out and then UserB logs into the app while it's still running, are you sure that you've started with a fresh managed object context? Is it possible that UserA's data remains in the MOC? It could then be written out when you create and add a new store. Another possibility that occurs to me is that iCloud is "helping" here. I see that your data files are kept in the Documents directory, and iOS will normally try to keep files in that directory backed up to iCloud. Perhaps when you create a new store for UserB's data, iCloud adds UserA's records back to that store. (Seems unlikely -- I'm pretty sure iCloud is more sophisticated than that -- but something to check.)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-08 10:22

    try the following method to flush the database, it works perfect for me.

    -(void) flushDatabase{
        [__managedObjectContext lock];
        NSArray *stores = [__persistentStoreCoordinator persistentStores];
        for(NSPersistentStore *store in stores) {
           [__persistentStoreCoordinator removePersistentStore:store error:nil];
           [[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:nil];
        }
        [__managedObjectContext unlock];
        __managedObjectModel    = nil;
        __managedObjectContext  = nil;
        __persistentStoreCoordinator = nil;
    }
    
    0 讨论(0)
提交回复
热议问题