2

I'm wondering if iOS allows one to do the following:

I have a puzzle game and I've been working on saving data when the user returns to the home screen. To do this, using NSNotificationCenter, I had one of my game classes to observe [UIApplication sharedApplication]'s ApplicationWillResignActive method so when that happens I save my game state. But if the user decides to exit while animations are going on, the game will save a midState where the model values are still changing and that will often cause crashes. My question is if it is possible to somehow delay the saving process (even though it is on the background) until the animations are complete (or some variable equals 1)?

My first idea is to create scheduled event with NSTimer to try to save until everything is set. Any ideas would be appreciated. Thank you

Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41

2 Answers2

2

You can use performSelector:withObject:afterDelay:

// Call doSomething after a 1 second delay
[self performSelector:@selector(doSomething) withObject:nil afterDelay:1.0f]; 
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
1

Rather than trying to delay the saving, especially in ApplicationWillResignActive, you should look into how to stop the animation and record the expected final values of the animation. Depending on the method of animation (UIView static methods, block based, 3rd party) there is usually a way to stop them, and since you define what the animation does you should already know the final state.

In the case of block based animations: How to cancel UIViews block-based animation?

Community
  • 1
  • 1
Joe
  • 56,979
  • 9
  • 128
  • 135
  • Unfortunately my animations use a lot of completion handlers so some methods are not called until some animations are finished. But of course I can write a custom method that calculates everything at the background, I was just wondering if there was any other way.Thank you for your response. – Kaan Dedeoglu Feb 28 '12 at 20:28
  • @Kaan you are welcome. I know that Apple is quite strict in the guidelines about doing things in `ApplicationWillResignActive` that takes too long (They will kill your app after a specified time, maybe 10 seconds). There is also good chance if you try and delay the save operation without blocking the thread the application will exit before the save ever happens. – Joe Feb 28 '12 at 20:31
  • well then I think I just have to write a custom method that calculates every value without animations and store it somewhere, it's going to be ugly but it will work, I wish I had used more NSTimer events to handle animation handlers than completion blocks. (sigh...) :) – Kaan Dedeoglu Feb 28 '12 at 20:41
  • You can try to delay the operation but you will just need to block the thread while animation completes I believe. If that doesn't work then you would need a way to get the results. – Joe Feb 28 '12 at 20:45
  • If you're using a lot of completion blocks I would recommend looking into Core Animation. It might make things easier. The WWDC video Session 421 - Core Animation Essentials is a good place to start. – jackslash Feb 28 '12 at 20:53