Can we stop the for loop when app goes background - iphone

The for loop is running fetching the data(images and text) from array but when i press home screen button the loop still continues in background and lasts more than 5 seconds which is default time, it should not be more that that as i studied while googling. And when i press home button even then the app delegates respective method like appEnterBackground also called after completion of this loop containing method. So, is it possible to break the loop when the home button is pressed.
Please guide.

If you wanted to detect the entering into background, in addition to responding to the app delegate applicationDidEnterBackground method, you could alternatively register yourself as an observer of the UIApplicationDidEnterBackgroundNotification notification (e.g., using the addObserverForName method of [NSNotificationCenter defaultCenter]).
Note, if you want to cancel your requests, you will want to ensure that (a) the requests are run asynchronously; and (b) they're cancelable. If you're targeting iOS 7 and later, you can accomplish this with NSURLSession (e.g., dataTaskWithRequest or dataTaskWithURL which return a NSURLSessionDataTask object, on which you can call the cancel method if and when desired). Then you can write a handler for the notification that cancels any pending requests.
If you really want to cancel the requests, you can do something like the above, but you alternatively could simply request additional time to complete the requests if the app happens to go into background while the requests are running. This way you get a few minutes to finish requests rather than just a few seconds. See Executing Finite-Length Tasks in App Programming Guide for iOS: Background States.
Or, perhaps even better, you could add your tasks to a background NSURLSession. See the Downloading Content in the Background section of the aforementioned App Programming Guide for iOS: Background States. This way the tasks will continue even after your app is suspended (or, if you app is terminated due to memory pressure). For more information, see WWDC 2013 video, What's New in Foundation Networking.

Related

In Corona SDK How to hide a group if application suspended?

I am building a word game and I want to hide the board when application is suspended?
the code looks fine however it givs a strange behaviour!!,
when I suspend the app nothing will happen but when i resume the application then the board will hide!!
local onSystem = function( event )
if event.type == "applicationSuspend" then
print("suspend")
board_group.alpha = 0
end
end
Runtime:addEventListener( "system", onSystem )
Note: you might wonder how do I know how the application looks when suspended?
the answer is: by pressing the home button twice.
example
SpellTower in normal state
https://dzwonsemrish7.cloudfront.net/items/430k0c0b0y0b413d0b42/Image%202012.11.12%208:08:24%20AM.png?v=4822f549
SpellTower after pressing the home button twice
https://dzwonsemrish7.cloudfront.net/items/280a1y0r2U3W321y1B2z/Image%202012.11.12%208:08:31%20AM.png?v=09c37567
you can see how they are hiding the letters, this is exactly what I want to do for my game, the only difference is i am using Corona SDK
When you do board_group.alpha = 0 you only has set a variable, the result will only take effect after a screen update.
But since the application is suspended... it won't update! So, changing any graphics on applicationSuspend don't work.
I believe the reason is because the application is not considered as suspended. In normal objective c programming it means that applicationWillResignActive is called when the user double clicks on the home button. So what you want to do is to add that code for this part.
Here is a flow of events:
http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/
Corona seems to have these events:
"applicationStart" occurs when the application is launched and all code
in main.lua is executed.
"applicationExit" occurs when the user quits the application.
"applicationSuspend" occurs when the device needs to suspend the application such as during a phone call or if the phone goes to sleep
from inactivity. In the simulator, this corresponds to the simulator
running in the background. During suspension, no events (not even
enterFrame events) are sent to the application while suspended, so if
you have code that depends on time, you should account for the time
lost to an application being suspended.
"applicationResume" occurs when the application resumes after a suspend. On the phone, this occurs if the application was suspended
because of a phone call. On the simulator, this occurs when the simulator was in the background and now is the foreground application.
So my guess is that you have to implement it outside of the corona API.
According to the corona documents you can implement them in the delegate:
You can intercept UIApplicationDelegate events via your implementation
of the CoronaDelegate protocol.
This protocol conforms to the UIApplicationDelegate protocol. Corona's
internal delegate will call your protocol's method if it is
implemented.
Please keep in mind the following:
Methods that Apple has deprecated will be ignored.
In most cases, your class' version will be invoked after Corona's corresponding version of the UIApplicationDelegate method. There is one situation in which your version will be called before.
In situations where the app is about to suspend or go to the background, your method will be called before Corona's version, e.g.
applicationWillResignActive: and applicationDidEnterBackground:.
http://docs.coronalabs.com/native/enterprise/ios/CoronaDelegate.html
But this is just a guess. Hope it helps!
Edit:
I was thinking, something really simple you could do is catch it outside and present a "pause" screen, then just hide it when the application enters foreground.
So if you can't do that (for now), one other option is to save application state when the application is about to terminate, and then set UIApplicationExitsOnSuspend = true in your plist file. This will cause the application to exit instead of suspending, which will avoid any screenshots, effectively "hiding" the board, etc. The downfall is, the app will have to read the session state when it launches again... this is only useful if your application can be designed to actually exit without losing your state, and is quite honestly, a little extreme. That said, it may be the only way to effectively do what you're trying to do.
Other ideas would be to see if you can add a large black layer to the screen, even though the application is suspending; perhaps this will somehow trigger an internal screen update by natively setting setNeedsDisplay. Also, instead of modifying the alpha, you might consider temporarily removing all of your layers and see if that has a similar effect.

applicationDidEnterBackground: is not being called

I am creating a video editing app for iPhone.
When the user sets up the video editing and then starts the rendering, obviously it takes time. When the user presses the home button, the application is minimized but the rendering still continues and applicationDidEnterBackground: is not getting called unless the rendering process finishes.
Can you post the code that performs the rendering?
If it's a blocking call that takes a lot of time to complete and you are doing it in the main runloop, you are clogging up the run loop. Your rendering code would need to finish before applicationDidEnterBackground would be called, and if this takes too long, iOS will simply kill your application instead. IT would also freeze the UI, which you definitely don't want.
The most likely solution would be to move your rendering code to a background thread, but it really depends on what you are doing.

Iphone Calendar Application

i try to retrieve Calendar events between two dates. After that I try to delete all events. this program is working fine. The Events deleting process will take few second/minute based on number of events.
When device goes to sleep this application will work in background as usual. After coming to normal state(wake up) my application will exit. But, i did over come this problem, disable idle time.
But this same problem repeating when i press sleep/wake up button on the top of iPhone. How to over come this problem. What happens in my application, when changing state, any memory problem or any other...
When the sleep/wake button is pressed, your application is sent to the background/changes mode to inactive. Check UIApplicationDelegate documentation applicationWillResignActive: and applicationDidEnterBackground:. When this happens there are restrictions about your application doing background operations, which are described within the documentation.

Crash when receiving call while playing audio

I am working on an online radio and it works fine, but gets problem when user received a call. When call finishes I start the radio automatically, by using AVAudioSessionDelegate.
Now radio is playing and if user open the app (moving it to foreground) and press that stop/play button again my app goes crashes. I might know the reason, the reason may be due to threading. But I am unable to handle this (I can't put the code, it is huge and private).
To prevent crash when your app goes in background and comes in foreground, call the pause button of radio app in background and releases threads. when user touches the play button it it will start threading. Try it,hopes it may work for you.

Can I cancel the method "applicationDidEnterBackground?

I wanted to cancel the method, to request a confirmation from the user when he presses the button "HOME" where on iPhone would go into the execution in background.
If the user accepts, enters into the execution in background, if not accept, I do not do anything.
I looked in the FORUM, in the documentation from Apple and I found nothing.
Could anyone help me?
Thanks in advance!
No you can't override this. The event will always fire but it does give you time to clean up before you move to the background.
Your delegate’s applicationDidEnterBackground: method has approximately five seconds to finish
any tasks and return. In practice, this method should return as quickly as possible. If the method does not
return before time runs out, your application is terminated and purged from memory. If you still need more
time to perform tasks, call the beginBackgroundTaskWithExpirationHandler: method to request
background execution time and then start any long-running tasks in a secondary thread. Regardless of
whether you start any background tasks, the applicationDidEnterBackground: method must still exit
within five seconds
iOS Application Programming Guide
No, you can't override the operating system or the user's button press.
If the user presses the button to send the app into the background, the app goes into the background.