I am using core data as local DB to save local data in my iOS App. When the app is terminated and relaunched again then local data is cleared from core data. I am calling saveContext() from applicationDidEnterBackground and applicationWillTerminate methods. One thing is that I have changed the core data version. This change will do any effect on clear data after the relaunched app.
saveContext Method - :
func saveContext () -> Bool {
let context = persistentContainer.viewContext
var saveFlag = false
if context.hasChanges {
do {
try context.save()
saveFlag = true
} catch {
saveFlag = false
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
return saveFlag
}
Note : From above saveContext() method, Not goes inside context.hasChanges condition. So, not print an error message. It goes outside the condition and returns saveFlag as false.
var appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate