简体   繁体   中英

Sqlite database in iOS on termination

I have build an application integrated with Sq-lite database, the application works as expected , but when i terminate the application from the device, the database file deleted and the application lose all the needed data !!

i appreciate any help regarding a way i can use to save the database file after terminate the application.

Try explicitly making a call to save your data before closing your app in your AppDelegate.m . This should solve your problem.

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Saves changes in the application's managed object context before the application terminates.
    if (managedObjectContext != nil)
    {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
        {
            // manage db save error
        }
    }
}

Pls not if the save is more than 5 sec (eg. because too many rows to persist, or complex db structure) , then you can use beginBackgroundTaskWithExpirationHandler to request background execution time from ios in a different thread for saving your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM