4.0 and the Multi-tasking/Suspended crashes games and prefs

Hi

Prior to 4.0, my playerprefs worked perfectly. For example, if the player set sound “off” in the app then closed the app, the next time the app is opened it would show the sound as ‘off’ and the user could switch it back on and the music would kick in without dramas.

Now that 4.0 is here, the above isn’t occurring. Yes, the sound button works fine if the game goes into a suspended state i.e. just closing it down. If you reopen it takes off from where it left off from it’s ‘paused’ state.

However, if I close the app, then remove the app from the suspended apps menu (double tap Home button to reveal), and then open the app to start from scratch, NO player prefs have been saved.

Also, now I’m finding that if I delete the app from the suspended menu on bottom screen, and then try to launch the app from main screen, the app crashes at the point where playerprefs were originally set in Scene 1. (function Update() code as follows to see if playerprefs set or not, as when we go into Scene 2, the sound button needs to know what state the music is in to display the correct on or off texture).

	if (!PlayerPrefs.HasKey("Sound"))
	{
		PlayerPrefs.SetString("Sound", "On");	
	
	}

BUT this crash ONLY occurs if I had changed the setting of sound in the app first before closing and removing its suspended icon from the bottom screen. If I DON’T change the original setting (On state), and remove the icon bottom screen, then the app doesn’t crash on re-launch.

My architecture is Standard (armv 6, armv7)
Base SDK 4.0
Unity 1.7
Valid architecture armv6, armv7

Deployment iPhone only
iPhone OS Deployment Target iPhone OS 3.1.3 (changing this to 4.0 makes no difference, so would rather support devices still on 3.1.3 for the time being)

I’m Building to an iPhone 3GS running latest 4.01 OS.

Is the above setup correct? Any advice or other people’s experiences like this? This is doing my head in ><

Where do you save it and do you ensure thta you don’t save too much? (using player prefs for more than preferences and small data is a horrible idea cause playerprefs or better the underlying NSUserDefaults are permanentely loaded in the application!)

the optimal place to save it is:

  1. ongoing (bad for performance with more than trivial data amounts)
  2. in onapplicationpause which you will get called when it becomes inactive. You might want to add the go into background method that the AppController could support in iOS4 and copy the code there that pauses unity etc as it is present for the other cases. that way you know for sure that it the function will be called

I’m only using playerprefs to save:

Sound on/off state
Voices on/off state
Unlocked levels (11) Yes or no (and occurs only once - the player can’t clear the levels/restart the game once unlocked)

That’s it.

The preferences for the unlocked levels only gets updated once when the player has unlocked that level.

And the prefs for sound and voice update on button click (on or off).

Sorry, I don’t understand what you mean in point 2, it’s gone over my head :stuck_out_tongue:

If you save it at such specific places and you really only save this kind of stuff, it should technically work.
Pretty strange what you see there.

Yes, my app is quite ‘simple’. Nothing severe going on in terms of saving prefs etc. It was all quite fine pre 4.0. This suspended state is screwing with the prefs somehow, but that’s beyond my technical know-how.

Has anyone else seen this? Or have you even tried it (removing the app from suspension menu then reloading).

its beyond mine too but not because I have no knowledge, more because I have not heard of scenarios where saving to NSUserDefaults is killed by background tasking and alike. Its a fundamental and pretty lightweight iOS core functionality.

I’ve spent the past 10 hours trying to figure this out, running various user testing etc to pin point the problem.

What I’m finding is this:

If the user presses the sound off then exits straight away and then deletes the paused game from the bottom suspended apps menu, then the player preferences DON’T get set.

HOWEVER

If the user presses the sound button off, then waits say 5 seconds, and THEN exits the app and removes it from the suspension menu, then the prefs DO get set ><

So, it seems to be a timing issue.

And considering my earlier posts, I’m not saving/doing much with player prefs. It’s just an on/off switch for goodness sake!!!

Anyone have any ideas how I could possibly speed up on a button press:

PlayerPrefs.SetString("Sound","Off");

I hope to god Mr Jobs doesn’t release OS5.0 any time soon >< This 4.0 is costing me time and sanity.

I can confirm this, I just tested it with my app SunGPS, when I change settings and quit after that I see exactly the same results. Does that mean playerprefs get written with a delay of 5 seconds?
That sux for the enhancement pack too.

Well Vic, I’m glad I’m not going mad then — that’s some relief :slight_smile:

I’ve now changed my xcode project settings from standard architecture (6 7) and now trying on optimised armv7 with deployment to 4.0, but it’s made no difference… still 5-6 seconds to save player preferences.

I wonder if this is a ‘unity thing’ or an ‘os4’ thing. My money is on Apple :stuck_out_tongue:

I would report it to UT ignore it. The chances that someone changes music settings and quits directly after changing are extremely low. And it seems Apple does not test it, otherwise my App won’t be on the store.

I’m having the exact same effect with my options menu in 1.7: when I tried to save 6 ints and 2 floats on pressing an ok button I had to wait at least 30 seconds to be sure it works.
Even if PlayerPrefs don’t work in an instant, saving 8 entries in one event should be possible, right? But having to wait half a minute before I can continue to use one of those entries renders it pretty much useless.
For me it’s not a time critical problem so I decided to wait until Unity3 is grown out of beta and file a bug if the problem still persists.

I’m more worried about the saving of unlocked items (11 in total) by the player. If they quit out just as it unlocks they’ll have to replay ><

I’ve also been testing a host of other apps to see if they have the same issue, and so far it looks like just us. From this I can only assume it’s a Unity bug.

I use PlayerPrefs as well and pre 4.0 everything worked great. Post 4.0 is crashes at diffrent times and some PlayerPrefs don’t get updated.

Apple really got too big for its boots on this 4.0, huh. It’s incredibly disappointing considering the amount of time and money people have invested into supporting their products.

However, I’ve yet to come across a non-Unity app that has issues. I’m wondering if other engines are having issues? Or have I just ‘gotten lucky’ so far on the other apps I’ve chosen to test… then again, I don’t know if they use playerprefs or not…

I sugesst you file a bug now under 1.7. That way Unity knows about it and can work on a fix for Unity 3. If you wait until Unity 3 comes out and it still has the bug it will take some time to fix it and release it.

Yeah I put something in the forum section for unity support… hope that’s the right place :slight_smile:

I was thinking the same the minute after I posted :slight_smile:
I will get back to my game save code after the weekend and file a bug.

hi folks,

i can confirm that too.
i managed to get around by setting the key UIApplicationExitsOnSuspend to true.

my game is so simple that its no problem to start over again.

although, its strange that even in this setup the icon is still displayed in the list of “runngin in background-apps” (the bar at the bottom you see after a double-home-click).

but, to stay with the topic: same here - playerprefs not updated when tapping home once, then quitting the game in the multitasking-bar.

with the key UIApplicationExitsOnSuspend set to true in info.plist, it works.

greets, thomas

Thanks for this Thomas. Does doing so comply with Apple’s TRCs though? Are we allowed to set it to true?

And (as I’m no programmer and definitely not too familiar with Xcode itself), how and where do I set this?

Many thanks

hi,

see this thread:
http://forum.unity3d.com/viewtopic.php?t=57393&highlight=game+quit

so the steps are:
open info.plist in xcode
add a new line after the last one
enter the key UIApplicationExitsOnSuspend
change value-type to boolean
check the checkbox

hth

greets thomas

i would not know how this could break the tos