Reputation: 4207
I need to upload files to a server in an iOS app. I would like this to occur while the app is running, but in a concurrent manner (on a separate thread) so the upload is invisible to the user of the application.
Running anything in the "background" seems to complex, so I want to make sure I get the question right. What I would like to know is:
1) While the app is in the foreground, can I have a "background thread" that contains a timer that will upload data to the server as the data becomes available?
2) If so, what will happen to the upload task when another application is brought to the foreground by the user?
3) If so, what are the best practices for implementing this type of concurrency? Coming in cold, a thread would be the first thing I would consider, but there may be better, simpler ways to accomplish this.
Thanks for the help!
Upvotes: 1
Views: 826
Reputation: 42588
The answer is Grand Central Dispatch or Operation Queues. See the Concurrency Programming Guide for more details.
1) By using a dispatch queue or an operation queue, you don't need to worry about having a timer or polling for data. The system handles all those details. When you get a piece of data you wish to upload, you create an simple upload task, send that task to a queue and the queue takes care of all the scheduling.
2) The queue will be paused. When your app is resumed the queue will be resumed also. Your task must be capable of handing reachability problems and network timeouts. In addition, you must keep in mind that your app could be killed at anytime. Save state before going to the background.
3) Don't use threads; do use queues.
Upvotes: 3
Reputation: 21221
1) While the app is in the foreground, can I have a "background thread" that contains a timer that will upload data to the server as the data becomes available?
Yes could create a background worker thread that starts when the data is available
2) If so, what will happen to the upload task when another application is brought to the foreground by the user?
It depends, please read this for more information
3) If so, what are the best practices for implementing this type of concurrency? Coming in cold, a thread would be the first thing I would consider, but there may be better, simpler ways to accomplish this.
Best way is as you stated is to use NSThread and threading mechanism
Upvotes: 1