قالب وردپرس درنا توس
Home / IOS Development / ios – Core data sometimes loses data

ios – Core data sometimes loses data



We run an App through Citrix Secure Hub, it seems that sometimes it is a return with losing data in CoreData.

As I understand, CoreData has something like a working copy of all the objects, and sometimes it attempts to continue it on the file system.

Well tried to simulate the behavior, but without success we couldn't figure out data loss or refunded data in the test environment.

So is there a way to force iOS to write the current "working copy" on the disk to prevent data loss by using too much memory (and maybe crashing)? We call our storage function after

As we have already found:

We did not use:

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

to store the context, can this be a problem (we already save the context after each created object)?

At the moment we do not really handle problems when the context cannot be stored, are there any recommendations how to handle it in a productive environment? And is it a good thing to maybe crash to the app to prevent the user from struggling with data loss?

Edit: This is the used Core Data Handler:

  Import Foundation
import CoreData

leave context = CoreDataManager.shared.managedObjectContext

func saveContext (_ completion: ((- -> Invalid)? = null) {

CoreDataManager.shared.save (completion)
}

func saveContextSync () {

CoreDataManager.shared.saveSync ()
}

class CoreDataManager: NSObject {

static let shared = CoreDataManager ()

lat var managedObjectContext: NSManagedObjectContext = {

var managedObjectContext = NSManagedObjectContext (concurrencyType: .mainQueueConcurrencyType)

managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator

return managedObjectContext
} ()

And our storage functionality:

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

saveAsync (completion)
}

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

func save () {

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

completion? ()
}
}

if Thread.isMainThread {
save ()
} other {
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's going on
interrupt()
}
}
}

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

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

Core data goes back to the previous state for no apparent reason

Edit 3: It seems that there is no crash, some users tell me they add data, just press the home button and after a couple of hours the data from the last "task" is lost.


Source link