Mann
Mann

Reputation: 5507

iOS saving data if app crash

I have got requirement in one of my projects where user is asking to save data locally in case of sudden app crashes. Its form based app so user fill lots of data in form and if app crashes suddenly then user lost all data entered.

App is using Core Data to save data locally.

There is a point where we save entered data in core data but not during every second user fills data.

Also if app crashes core data also vanishes.

Upvotes: 0

Views: 3616

Answers (4)

Aurelien
Aurelien

Reputation: 116

In fact, if your app crash, the callback :

- (void)applicationWillTerminate:(UIApplication *)application

will not be called. This one is only called if for some reasons the system need to shutdown your app (usually because resources are rare or because your background job is still doing work after the maximum time allowed) or if you are on < iOs 4. You don't have any way to know when your app will crash (but when you relaunch the app, you can know if it had crashed).

So for your particular case you have two solutions :

  • Use either a NSTimer with a quick firing rate, or call a fonction each time you edit a field and update the core-data context then save it on disk.

    NSError *error = nil;
    [managedObjectContext save:&error]
    

    Did you set a persistentStoreCoordinator on your context ? If no, core-data will never persist your data on disk.

  • Crashes don't appear out of nowhere, find the place(s) where crashes might happen and fix it or if you can't, use a try-catch to keep your app running (but please, try not to do that...).

Hope this help

Upvotes: 4

Hemang
Hemang

Reputation: 27050

In particular case you can should use, try catch block, (but not) everywhere.

try {
   //write your code
}catch(NSException *e){
    //See what's the error
}finally{
    //Save your context
}

This it the best solution in my thinking. However you can create a NSTimer which executes a method at some reasonable seconds where you can hold and save your context.

You can also save your context in AppDelegate's method like (if you're targeting iOS 4.0 and above and if your app was exit by iOS it self for some reason),

- (void)applicationWillTerminate:(UIApplication *)application{};

below method will always call when your app goes in background,

- (void)applicationDidEnterBackground:(UIApplication *)application{};

Upvotes: 1

Duyen-Hoa
Duyen-Hoa

Reputation: 15784

You can implement a HandleException to catch all exceptions that crash your application.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //for uncaughted exceptions
    NSSetUncaughtExceptionHandler(&HandleException);

    struct sigaction signalAction;
    memset(&signalAction, 0, sizeof(signalAction));
    signalAction.sa_handler = &HandleSignal;

    sigaction(SIGABRT, &signalAction, NULL);
    sigaction(SIGILL, &signalAction, NULL);
    sigaction(SIGBUS, &signalAction, NULL);
    //and more if you need to handle more signals    


    //another setup ...
    return YES
}

#pragma mark crash management
void HandleException(NSException *exception) {
    NSLog(@"[FALTAL] App crashing with exception: %@", exception);
    //try to save your DB here.
}

void HandleSignal(int signal) {
    //try to save your DB here.
}

#pragma -

However, I don't know about how many time you will have before application exits. But I suppose that you will have enough time to do the DB-backup task.

Upvotes: 1

Abhishek
Abhishek

Reputation: 99

Use

  • (void)applicationWillTerminate:(UIApplication *)application

delegate method of AppDelgate to save your data in core data.

Upvotes: -1

Related Questions