Bill Graner
Bill Graner

Reputation: 141

How can you prevent invalid data states on iOS, when a user can force quit at any time?

Wondering about best practices for maintaining valid states and data.

In an operating system where the user can force quit an app at any time, how can you ensure that the app's data isn't left in a broken state, if they force quit between just the wrong two lines of code executing?

For instance, if I'm making a game where the player can win a level and earn a trophy, and I have two lines of code:

wonLevel = true; hasTrophy = true;

...what if the user quits the app after the first line of code, and I end up with wonLevel == true and hasTrophy == false?

Upvotes: 0

Views: 21

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49896

At least in iOS (and Android, I believe), you get an event when the app is being suspended/exited, so you can handle just such issues.

In general, you don't care about variables as you describe, as they go away with the app. If you are looking at persistent data, then you should probably be using a database, where transactions allow you to group a sequence of modifications into an all-or-nothing affair.

Upvotes: 0

Related Questions