Problem with NSUserDefaults on Android

208 views
Skip to first unread message

Edi Held

unread,
Mar 13, 2014, 5:00:37 AM3/13/14
to apportabl...@googlegroups.com
I am using [NSUserDefaults standardUserDefaults] to save a highscore for my game on the local device. It works without problems on my iPhone. But on my Android device, it doesn't work properly:

- as long as the app is running, it works normally
- after leaving to the home screen and returning to the app it still works as it should
- but when manually closing the app (so it isn't running in the background anymore) and then restarting it, the NSUserDefaults deliver wrong values

Here's an example: The current highscore is 15. I set a new highscore at 18. I play a little longer, but I don't set another highscore. I manually close the app, so that it's not running in the background anymore. I restart the app. The highscore is 15 again.

This is how I use the NSUserDefaults: At first, I define NSUserDefaults *def = [NSUserDefaults standardUserDefaults];. Then I get the highscore with int highscore = [def integerForKey:@"insertNameHighscore"]; and (if neccessary) save a new highscore with [def setInteger:_points forKey:@"insertNameHighscore"];

Is there any way to make the NSUserDefaults work or is there an alternative I should rather use?

Philippe Hausler

unread,
Mar 13, 2014, 5:02:22 AM3/13/14
to apportabl...@googlegroups.com, Edi Held
That should work. How are you closing the app? Are you calling synchronize before entering background? Which version of apportable do you have?
--
You received this message because you are subscribed to the Google Groups "Apportable discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apportable-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Edi Held

unread,
Mar 13, 2014, 5:14:33 AM3/13/14
to apportabl...@googlegroups.com, Edi Held
I am closing the app with the button on the lower right (were you see open apps and swipe to right to close an app).


I am updating the highscore to the NSUserDefaults as soon as it was made. Is this what you meant by calling synchronize before entering background or is this something different I also have to perform?


This is the output of apportable --version:

Apportable SDK version 8e4f1916ffd49a9d4c9a988bf9ee454098375f23 starter
clang version 7fc8b05e4f57f61dbbbe5c8e62581b0e0c42941e
gdb version 4e9eedd60f78ce91b4f71b222299ef3cf7d765d2
android sdk version r21.0.1.1
android ndk version r9c
ninja version 315d897710c6ca6e7df7d3b0bf68879356841c5d


Thanks for your help!
To unsubscribe from this group and stop receiving emails from it, send an email to apportable-discuss+unsub...@googlegroups.com.

Philippe Hausler

unread,
Mar 13, 2014, 5:19:59 AM3/13/14
to apportabl...@googlegroups.com, Edi Held, Edi Held
Ok, so you are force quitting the app by that command (from what you described). That will immediately terminate the process after it is in background.

[[NSUserDefaults standardDefaults] synchronize]

that is the synchronize call I am speaking of. If you are having issues with persistence, you can try calling that immediately after setting the value. Also there is the backing function call CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication) that will immediately sync to disk.
To unsubscribe from this group and stop receiving emails from it, send an email to apportable-disc...@googlegroups.com.

Tomasz Wilczynski

unread,
Mar 13, 2014, 5:20:45 AM3/13/14
to apportabl...@googlegroups.com, Edi Held
I had the same problem. The solution was to extend NSUserDefaults in a way that every method that writes data calls [self synchronize] at the end.

Tom

Edi Held

unread,
Mar 13, 2014, 5:30:40 AM3/13/14
to apportabl...@googlegroups.com, Edi Held
Thanks guys!

I added [[NSUserDefaults standardUserDefaults] synchronize]; and it seems to work now.
Reply all
Reply to author
Forward
0 new messages