I use AFAmazonS3 (which is an extention of AFNetworking)
It has AFHTTPRequestOperation that is created and added to the operation queue
[self.operationQueue addOperation:requestOperation];
The thing is that when the app goes to background it stops uploading and doesn't resume when it goes back.
How can I acheive it?
I saw some solutions but it was for the old version of AFNetworking
AFNetworking supports Background operations.This post is mentioning setShouldExecuteAsBackgroundTaskWithExpirationHandler: and explaining how to use it https://stackoverflow.com/a/7881866/3033056 .
This method is in AFURLConnectionOperation and AFHTTPRequestOperation inherits from AFURLConnectionOperation
Related
I want to upload a file to a server using NSURLSession.
Cases Are:
1. It should resume uploading a file to the server from where it stopped because of app crash.
2. It should handle background upload as well.
Try AFNetworking Library to upload image asynchronously.You can find a brief example in this thread.
You should use background NSURLSession. If your app crashed or user left the app while upload was in progress, with a background NSURLSession the upload would continue seamlessly in the background. When the upload is done, your app will be notified of this via the delegate (and if your app wasn't alive at the time the download finished, it will be started in a background mode, at which point you can do whatever cleanup you need).
So create NSURLSessionConfiguration with backgroundSessionConfigurationWithIdentifier, and then instantiate a NSURLSession with that configuration.
There are a few caveats:
You cannot use completion handler pattern. You have to use delegate-based implementation.
You have to implement handleEventsForBackgroundURLSession in the app delegate, capturing the completionHandler it passes you and also instantiate the background session again. Likewise, in your NSURLSession delegate methods, you have to implement URLSessionDidFinishEventsForBackgroundURLSession, which will call the saved completion handler.
For more information, see Background Task Considerations in URL Session Programming Guide, see a section of the same name (but different text) in the NSURLSession class reference, or see the WWDC 2013 What's New in Foundation Networking, where Apple first introduced us to background sessions.
I have a situation where I need to download multiple files sequentially as each download depends on its previous downloaded file. (I am processing the file in background itself)
I am using NSURLSessionConfiguration backgroundSessionConfiguration.
There is a scenario where NSURLSessionDownloadTask initiates while the application is in background. This crashes the app with Assertion permittedbackgroundduration.
So, my question is, am I doing wrong by initiating the download task in background???
Thanks in advance,
- Satya
You have limited time when on background, so chances are that you're taking too long to process the file.
If you don't call the completion handler fast enough, the system will crash your app.
Or you may have made a mistake and you are not calling the completion handler at all on the scenario where NSURLSessionDownloadTask initiates in background.
I am working with an iOS application in which i have to upload the images on service (Facebook) from my iOS application.When i start uploading i want the functionality to Pause and Resume method while Uploading. I don't know how to do these functionalities? please help me out if you have any information.
Thanks in advance.
use NSURLSessionUploadTask for uploading a file. NSURLSessionUploadTask class is inherited from NSURLSessionDataTask : NSURLSessionTask : NSObject.
And NSURLSessionTask provides Controlling the Task State
– cancel
– resume
– suspend
useful links: <NSURLSessionTask>
NSURLSessionUploadTask
Try to implement this.
If you are using AFNetworking you can also achieve the same by using AFHTTPRequestOperation.
I have a question about AFNetworking 2 and background downloads/uploads thanks to the new iOS7 NSURLSession background requests
Is this automatically handled by my AFHTTPRequestOperationManager ? Does it automatically set my requests'session to background mode?
I saw that the AFURLSessionManager Has a setDidFinishEventsForBackgroundURLSessionBlock Method but I wonder if everything is automatic?
If my app is killed or suspended, will requests keep on going? How can I get a callback when my app is relaunched?
Thanks a lot for your help!
AFHTTPRequestOperationManager uses the old NSURLConnection so that doesn't facilitate background downloading.
AFURLSessionManager uses NSURLSession under the hood so that does. I think you still need to configure the NSURLSession appropriately.
"The NSURLSession class supports background transfers while your app is suspended. Background transfers are provided only by sessions created using a background session configuration object (as returned by a call to backgroundSessionConfiguration:)."
Suggested reading:URL Loading System
I want to use NSOperationQueue in my app and start downloading the images from server.
If my app goes to background or be terminated, will NSOperationQueue still continues downloading them?
No, the NSOperationQueue will not continue working when the app is moved to the background. You would need to explicitly action this by using the method beginBackgroundTaskWithExpirationHandler.
This is covered in Technical Note TN2277 - Networking and Multitasking
See also iOS App Programming Guide, specifically the section on App States and Multitasking.
As an aside, could I recommend that you instead use the AFNetworking library. It handles a lot of this functionality for you. Specifically, each class is a subclass of NSOperation.
Moreover, it already has an image downloader class in AFImageRequestOperation. So that should be very useful for you. AFImageRequestOperation is a subclass of AFURLConnectionOperation so you have access to the method setShouldExecuteAsBackgroundTaskWithExpirationHandler.
Of course, all this relates to multitasking so it is only available in iOS 4.0 and later.