Norway


We are running an through Citrix Secure Hub, it seems that sometimes there is a rollback with loosing some in CoreData.

As i , CoreData is having something like an working copy of all the objects, and sometimes its tries to persist that on the filesystem.

Well tried to simulate the behavior but without any success, we could not find out any data loss or rollbacked data in our test environment.

So is there a way to force to write the current “working copy” on the disk to prevent any data loss when using too much memory (and maybe crash)? We call our function after

As we already found out:

We were NOT using:

func applicationWillResignActive(_ application: UIApplication) {
      print("applicationWillResignActive")
}

to save the context, could this be a problem (we are already the context after every created object) ?

At the Moment we dont really handle problems when the context could not be saved, are there any recommendations how to handle that in a productive environment? And is it a good thing to maybe crash to app to prevent the user from struggeling with data loss?

Edit: this is the used Data Handler:

import Foundation
import CoreData

let context = CoreDataManager.shared.managedObjectContext

func saveContext(_ completion: (() -> Void)? = nil) {

     CoreDataManager.shared.save(completion)
}

func saveContextSync() {

     CoreDataManager.shared.saveSync()
}

class CoreDataManager: NSObject {

    static let shared = CoreDataManager()

    lazy var managedObjectContext: NSManagedObjectContext = {

    var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)

    managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator

    return managedObjectContext
}()

And our save functionality:

@objc func save(_ completion: (() -> Void)?) {

    saveAsync(completion)
}

func saveAsync(_ completion: (() -> Void)?) {

    func save() {

        context.perform {
            do { try context.save() }
            catch {
              // HERE WE NEED TO HANDLE IT FOR A PRODUCTIVE ENVIRONMENT
            }

            completion?()
        }
    }

    if Thread.isMainThread {
        save()
    } else {
        DispatchQueue.main.async {
            save()
        }
    }

}

func saveSync() {

    func save() {
        context.performAndWait {
            do { try context.save() }
            catch { print(error)
                // TRY TO REPRODUCE MEMORY LOSS APP TO SEE WHAT HAPPENS
                abort()
            }
        }
    }

    if Thread.isMainThread {
        save()
    } else {
        DispatchQueue.main.sync {
            save()
        }
    }
}

Edit 2: This question in Objective C should be very similar:

Core Data reverts to previous state without apparent reason

Edit 3: It seems that there is no crash, some users telling me that they are adding data, then just press the home button and after a couple of hours the data from the last “task” is lost.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here