I was planning to use ASIHttprequest to download an mp3 file.
i will need to use queues because there can be multiple simultaneous downloads.
I saw that you can use
[request setShouldContinueWhenAppEntersBackground:YES];
if you want to support background execution...
If the file completes in the background, how can i handle this?
will the delegates be called anyway?
can i make one of those notification badges from the delegate if the app was in background whenn the delegate was called?
PS: I AM on iOS4, and background exec is supported.
The requestFinished method should still be called before the endBackgroundTask: is called on the download; however, to ensure the functionality you want gets executed (a notification badge or something), you should use a UIBackgroundTask.
Basically the idea is that you create a new background task and use beginBackgroundTaskWithExpirationHandler: in the delegate finished method and then do whatever you want in the background task (see the apple dev guide about it here).
Make sure you call endBackgroundTask on your task after you are done!
Related
I want to call a method in background to check for new notifications and mentions for Facebook and Twitter. How can I do this ? Currently I am calling methods on view will appear of main view only. If any notifications will be there then I have to show badge count also.Shall I use NSThread For this purpose . I am not very much aware about NSThreads.
Please help
Thanks..
You can use NSOperationQueue for your requirment. Which executes set of NSOperation objects concurrently. And Implement a delegate method to update badge.
http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationObjects/OperationObjects.html#//apple_ref/doc/uid/TP40008091-CH101-SW1
In my app from applicationDidEnterBackground i want to ask the application for more time to
create a UIWebView and load request with UIBackgroundTaskIdentifier, then in the delegate
method of UIWebView (webViewDidFinishLoad) i want to do a stuff there and show an alert or
notification while the
application is still reining in the background .
so how i can do that?.
Apple's documentation for UIApplication class for beginBackgroundTaskWithExpirationHandler: method says:
You can call this method at any point in your application’s execution.
You may also call this method multiple times to mark the beginning of
several background tasks that run in parallel. However, each task must
be ended separately. You identify a given task using the value
returned by this method.
This method can be safely called on a non-main thread.
So, once web view finish loading in background you can trigger another operation from webViewDidFinishLoad to show alert.
When you receive applicationDidEneterBackground your app is already effectively in the background. At that moment all your networking should be closed and you really shouldn't try to show any alerts or notifications.
I am trying to get my UIActivityIndicator start animating when the process of data consumption start downloading. My concern is: how do you get the activity indicator start animating at the same time? I have gone through some solutions here on Stack Overflow, yet I am trying to find some universal solution, since I am utilizing a subclass of UIActivityIndicator. I am not sure if a separate thread can resolve this issue?
I used:
[self performSelector:#selector(methodname) withObject:nil afterDelay:0];
However, this didn't solve my problem.
Just return from the method starting the activity indicator. The UI (including the activity indicator) updates after an app returns to the main run loop. Handle any long activities (such as your download) asynchronously with delegates or callbacks, so that the activity indicator can continue to run. So return from the method that starts your download as well. Finish later in another method.
Another option is to do the download, or other long process, in another thread. Operation queues and blocks can be used for this purpose. Use a performselector on the main thread to update the UI, as an app can't update the UI directly from within a background thread.
I have background NSInvocationOperation creating and saving NSArray to the NSManagedObject subclass.
I know that save should happen on main thread, so I use performSelectorOnMainThread for save in the operation.
When user pushes home button on iPhone 3G, app is going to quit. In applicationDidEnterBackground I do [queue waitUntilAllOperationsAreFinished], so that NSInvocationOperation has time to finish.
The problem is, that it waits only for "background part" of the operation - app is shutted down before performSelectorOnMainThread part of the operation is called. This means my NSManagedObject is not saved.
I tried to save object in operation's thread - app is shutted gracefully and changes are saved. But I think this is not good as NSManagedObject is not thread safe. Or is it OK to do this?
It seems like catch 22. I must be missing something - is there any elegant way how to solve this?
You should just do the save operation in the background using a separate context that notifies the main context. This means creating, fetching, and saving managed objects should be done on this separate context and this is documented in the Core Data - Concurrency with Core Data. You should also start a background task to ensure you have enough time to finish saving the data.
I have an app (a game) which saves data: game state, high scores, achievements, etc. Currently the app delegate does this on applicationWillTerminate:. After playing around with iPhone 4 for a bit, it seems that applications pretty much never terminate: they just run in the background forever, unless the user goes out of their way to quit them, or restart the phone.
So my question is, should I find another place to save my data, and if so, when?
To minimize the amount of time spent in the delegate method call, you should find a place that makes sense to save during the game (level completion, checkpoints, etc). You can also add a new delegate method to your application delegate which will be called when your application transitions to the background where you can duplicate some of the things you may have done previously in applicationWillTerminate:. The new delegate method to implement is -applicationDidEnterBackground:.
You will also receive a notification that the user switched back to your app as applicationWillEnterForeground:.
you can do so in the views diddisappear delegate method
-(void)viewDidDisappear:(BOOL)animated
{
//CODE FOR SAVING
}
There are 2 App delegate methods you can use
applicationDidResignActive: //pausing the app, used when a msg comes up. if multitasking this wont help
applicationDidEnterBackground: // called in iOS 4 for when the app is in the background
you can see when it loads into the foreground using
applicationWillEnterForeground:
check out the reference for more info
You should save in applicationDidEnterBackground. Make sure to wrap your saving code with – beginBackgroundTaskWithExpirationHandler: and endBackgroundTask, since without that, you have less than a second (or something like that) before execution suspends.