Question

For example:

- (void)someFunc {

    [self someFunc1];
    [self someFunc2];
    [self someFunc3];
}

I call someFunc. As I understand if I interrupt the application then the application doesn't guarantee that all the inner code in someFunc will be performed.

I must call someFunc1, someFunc2 and someFunc3 only once.

The problems I don't know how to solve:

  • someFunc1, someFunc2 and someFunc3 should be called atomically.
  • storing info for next launch. For example if we successfully have performed someFunc1 only then at next launch the application should call someFunc2 and someFunc3 only.

I know about method applicationWillTerminate:, but I don't know how to solve the current issue with it.

EDITED

Multitasking is not a solution because Even if the device is running iOS 4 or later, the device may not support multitasking., so it doesn't solve the general problem and makes the final solution more difficult only.

EDITED

For those who spam with off topic answers: read the title first - Save state when user exits an application. Where have you seen here putting the application into background?

Was it helpful?

Solution

This does't make sense. If these functions are running on the main thread, there is no way that the application can terminate normally while your functions are running. This is because the events sent like applicationWillTerminate: are sent on the same thread.

If your function is running on a different thread to the main thread, you will need to save some state information after each function completes, but you still have a race condition.

It might be better to check your application's state before running each function. For example, if you have a three step login/registration process with a server, you should query the server to see if the stage has been completed already before running it.

It's difficult to be more specific without knowing what you are doing in these functions.

OTHER TIPS

You should use background tasks ! Take a look at the documentation here :

Executing a Finite-Length Task in the Background

Put the call of someFunc in the middle of the background task. If your app goes to the background state, you'll have extra time to finish the execution of the method.

Make your functions to return bool, and when you call them, store the bool value to nsdefaults.

When the app restarts,check the bools from sndefaults, and if they are NO, run the functions and update them.

Nobody wants to help. So my temporary solution:

  • to save a last state I use a writing to a file because it enables to set its operation as atomic/nonatomic

  • I have replaced this code with something like this:

    typedef enum {
        state1,
        state2,
        state3
    } MyState;
    
    @property (assign) MyState state;
    
    -(void)someFunc {
    
        switch (state) {
    
            case state1:
            {
                [self someFunc1];
                state = state2;
                [self someFunc];
                break;
            }
            case state2:
            {
                [self someFunc2];
                state = state3;
                [self someFunc];
                break;
            }
            default:
                break;
        }
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top