Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

flummoxed

macrumors member
Original poster
Nov 27, 2010
38
0
This is too weird.

I am saving a few integer values to a plist for a "saved game state" using the
applicationDidEnterBackground method .

I'm saving 6 values, the last 3 of which are created by random generators.
Sometimes after I close out of the application in the simulator I discover that the previous state of the 3 random values has changed.

When I open the plist to look at the saved values the last 3 values are 6 6 6.
I'm not making this up!

Those 3 values can only be changed by calling the random generation method.
That method can only be invoked by pressing a button in the UI.
Whenever the plist gets changed by this behavior it is always 6 6 6, so it can not be coming from the random generator.

It does not always happen. Only when I close out of the simulator in a specific manner. I don't know if it is from first clicking the home button, and then the red "kill task" button in Xcode, or some other order of operations, but it does not happen all the time.

Does this sound familiar to anyone or do I need to call an exorcist?
 

flummoxed

macrumors member
Original poster
Nov 27, 2010
38
0
Ok, I found the problem...

When I am saving the values I am saving them from instance variables that store the random numbers. The problem arose when I would quit from the application immediately after opening it. Since the random number method was never invoked, the variables did not get assigned the proper values and remained initialized at 0 from startup.

So when the plist was being saved it was saving values from an array indexed from the instance variables that where still initialized at zero. The zero indexed values of my arrays just happened to be 6 for no special reason.

So saving right after opening just saved 0 indexed array values that where 6's.
Pure coincidence - as most unexplained mysteries usually are.
I guess I can put away the holy water.

Incidentally, using -applicationWillTerminate does not seem to be the correct method in iOS 4. I tried that and it would not work. The docs mention that it is proper to use -applicationDidEnterBackground when saving within iOS4 .
 

bredell

macrumors regular
Mar 30, 2008
127
1
Uppsala, Sweden
On the bright side, having your app possessed by the Devil is actually one of the few things that's not forbidden in the App store rules.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.