3

Very rarely (<1% of users), a user will inform me that they open my app, and all their data is gone. The problem I'm having is that I am unable to replicate the issue because it happens so rarely. I don't even know how to begin fixing this problem. It seems like this does not happen while a user is using the app. They seem to just open it one day and the data is no longer there.

On one occasion, a user was able to recover the data by turning off and on the iCloud switch for my app (in the phone settings). However, this did not work for another user.

I was reading that iOS may delete the cache when the storage is full. However, to my understanding, the default location for the container is in Application Support.

Here is my Core Data implementation in App Delegate:

lazy var persistentContainer: NSPersistentContainer = {
    let container = NSPersistentCloudKitContainer(name: "Bench")
    
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            print("Unresolved error \(error), \(error.userInfo)")
        }
    })
    
    container.viewContext.automaticallyMergesChangesFromParent = true
    
    return container
}()

func saveContext () {
        let context = persistentContainer.viewContext
        if context.hasChanges {
            context.perform {
                do {
                    try context.save()
                } catch {
                    let nserror = error as NSError
                    print(nserror.localizedDescription)
                }
            }
        }
    }

I access it globally like this:

let ad = UIApplication.shared.delegate as! AppDelegate
let context = ad.persistentContainer.viewContext
Arturo Lee
  • 61
  • 1
  • 8
  • I don't know what's wrong but it's extremely unlikely that Core Data is randomly deleting people's data. It's more likely to be something odd with their iCloud setup that's preventing Core Data from loading the data-- maybe due to something like turning it off, or changing to a different iCloud account? It might be good for you to look into one of the analytics frameworks so that the error messages would be sent back to you instead of being lost on the users' devices. – Tom Harrington Jul 09 '21 at 23:03
  • 1
    Thanks for the comment. That is a good idea. I just wonder if it would show up as an error since technically, it doesn’t crash the app. It just opens with the data reset. – Arturo Lee Jul 10 '21 at 00:55
  • I also think the data was not deleted, especially in iCloud. As I've read somewhere, NSPersistentCloudKitContainer works with requests and doesn't just simply has a copy of a container from an iPhone, so if the were no requests from your app to delete all data, it still should be in iCloud. Did you change something in your CoreData scheme? I've had a similar issue on my own phone, when I've incorrectly updated it - there we no data in the app, but it was still in iCloud (I've check in the Settings), so reinstallation of the app helped and all data is back. – Viktor Gavrilov Jul 10 '21 at 06:18
  • I didn't fully understand the issue with my case, my guess from the messages I've seen - I've accidentally created two separate Core Data containers: all my previous data entries were created in Container 1, but then app was working with Container 2 and saw no data – Viktor Gavrilov Jul 10 '21 at 06:21
  • @ViktorGavrilov When you delete the app to reinstall it, doesn't all the data get removed from iCloud as well? I did update the scheme a while back but I'm pretty sure I did that correctly. That wouldn't explain why it happens so rarely though. – Arturo Lee Jul 10 '21 at 16:20
  • @Arturo Lee When you delete an app, you get a following alert: "Deleting this app will also delete its data, but any documents or data stored in iCloud will not be deleted", so it should be fine – Viktor Gavrilov Jul 10 '21 at 16:45

0 Answers0