-4

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?

user2083364
  • 744
  • 1
  • 7
  • 20
  • These days you can only support iOS 4.3 or later (and no new apps should waste time supporting anything earlier than iOS 5.1 or even 6.1). ALL devices that run 4.3 or later support multitasking. So multitasking is a good solution. – rmaddy Sep 02 '13 at 14:59
  • According to developer.apple.com, 94% of devices are running iOS 6. Even dropping support for iOS 5 cuts out a small chunk of the market! https://developer.apple.com/devcenter/ios/checklist/ – bbum Sep 02 '13 at 17:59

4 Answers4

1

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.

Community
  • 1
  • 1
Dabrut
  • 862
  • 1
  • 10
  • 25
  • Thanks, but `Even if the device is running iOS 4 or later, the device may not support multitasking.` So it is not an answer and I will even have more troubles if I use background tasks because they are not always available – user2083364 Sep 02 '13 at 14:34
1

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.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • it is an example of the thing I use. These functions (or part of their code) may be processed in different threads but they always have this order: 1, 2, 3. In my current implementation the next function call is placed into the end of the previous one. My idea is to store at least steps which are already processed. So I can lose the last step but I always know that all the previous steps are already performed. – user2083364 Sep 03 '13 at 11:07
0

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.

Calin Chitu
  • 1,099
  • 7
  • 13
  • the problem is also how to atomically confirm changes and store current state in settings. For example if you update a database and user interrupts this process before current state storing in settings is updated then on next launch an app thinks that it didn't try to update the database. – user2083364 Sep 02 '13 at 14:40
0

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;
        }
    }
    
Oded
  • 489,969
  • 99
  • 883
  • 1,009
user2083364
  • 744
  • 1
  • 7
  • 20