Core data destroyPersistentStore, not working for some

Hi all

I have a problem with core data, where when a new user login that is different from the previous user i delete all of core data by using "destroyPersistentStore".

Then i recreate the persistent store, this works when i am testing. When it does not work for one of my users when she test.

I am not sure why this should not work, i have added the code i use to destroy the persistent store below.

This code is run after login but before the view changes away from my login view.

        // Retrieves the shared `AppDelegate` instance
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
            return
        }
        appDelegate.destroyDataSyncBackground()
        // Get a reference to a NSPersistentStoreCoordinator
        let storeContainer =
        appDelegate.persistentContainer.persistentStoreCoordinator

        // Delete each existing persistent store
        for store in storeContainer.persistentStores {
            if let url = store.url {
                do {
                    try storeContainer.destroyPersistentStore(
                        at: url,
                        ofType: store.type,
                        options: nil
                    )
                } catch {
                    print("Failed to deleted all")
                }
            } else {
                print("Failed to deleted all")
            }
        }
        // Re-create the persistent container
        appDelegate.persistentContainer = NSPersistentContainer(
            name: "CueToCue" // the name of
            // a .xcdatamodeld file
        )

        // Calling loadPersistentStores will re-create the
        // persistent stores
        appDelegate.persistentContainer.loadPersistentStores {
            (store, error) in
            // Handle errors
            let description = NSPersistentStoreDescription()
            description.shouldMigrateStoreAutomatically = true
            description.shouldInferMappingModelAutomatically = true
            appDelegate.persistentContainer.persistentStoreDescriptions =  [description]
        }
        // Reapply context configuration
        let viewContext = appDelegate.persistentContainer.viewContext
        viewContext.automaticallyMergesChangesFromParent = true
        viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        do {
            try viewContext.save()
            appDelegate.recreateDataSyncBackground()
        } catch {
            print("Debug: saving delete all failed.")
        }
    }

The function "destroyDataSyncBackground" just set the my sync class to nil so stop any changes to core data while the code is running.

The function "recreateDataSyncBackground" recreate the sync class so fetch, post and patch requests is made again.

Would you mind to elaborate what doesn't work? Is it that the store is not deleted after destroyPersistentStore is called? I am also curious how you observe the error. If you don't mind to describe the flow you use, together with what you expect to see and don't see in the flow, I'll see if I can figure out why.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Sure In the app i develop i have a list of items that can be downloaded when an item is downloaded it changes appearance so to show its downloaded.

This is stored in core data, but i have a user that after core data should have been cleared (with code in first post) still have some of the previous item as downloaded with should not happen if all of core data have been destroyed/deleted.

The users that have this problem is using a old iPad that is running iOS 16.7.13 and have around 3 GB of free space, not sure if this

Hope this helps Best Thomas

Thanks. I don't see anything in your code snippet can lead to the issue. If I have to guess, maybe you retain some managed objects (NSManagedObject), which still exist in memory after the store is destroyed, and your view still presents im-memory values? If you quit and relaunch your app, and see that the issue goes away, it will be likely the case.

Other than that, you might work with the users to hopefully create a reproducible case. I understand that wil take time though.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Core data destroyPersistentStore, not working for some
 
 
Q