multiple download threads from a single source via iphone - iphone

I have a problem of downloading 5 different files from a single server. Is that possible to download all 5 files from that server over 5 individual threads ? If yes, is that efficient than downloading each file one after another via single thread ?

It is always more efficient to download files via separate threads. If you download them via the main thread, this will simply block the main thread and it will appear that your app has frozen for a moment while the downloads are taking place. I would suggest that if you are downloading a large number of files, look into the MBProgressHUD library which is an excellent way of communicating the downloads' progress to your users.

assuming you're downloading from secondary threads at this time:
yes. 5 concurrent downloads is fine, and can ultimately decrease the time it takes to complete all 5 requests (in practice). however, you should keep the number of concurrent downloads relatively low - don't try to run 30 at the same time, but create a queue.
it's also good to prioritize your downloads, and add suspension for times when you need something downloaded at a higher priority.

Related

iOS FTP concurrent upload and download

I am trying to write a code in which i have to create 2 parallel threads (Which is running inside a dispatch serial queue) 1 thread will upload and another thread will download the file from the server. Both of the upload and download progress is going to be updated on another screen with bytes upload and downloaded.
But I am facing some strange issues:-
As soon as I create secondary threads my functions returns to the dispatch Serial Queues and it starts another serial task scehduled instead of waiting for 2 current parallel Tasks to complete first.
As soon as the Download Threads starts the upload Thread Stops uploading file and returns -1 during Writeto server.
I am using Apple SimpleFTP example and trying to run it in 2 parallel threads 1 is for put and second for get.
Any Idea why the Upload stops while downloading file from server(I have made 2 different connnections to FTP server as well)
Advance Thanks for any help.
solved the issue using the Syncronous FTP request. The run loop run function was blocking the other thread from calling the stream delegate function.

How to organize background bulk download?

I'm trying to organize background download of multiple images (couple hundred) in the way that it will not freeze the main UI and I will be able to control number of simultaneous downloads.
First attempt was using serial dispatch queue, which failed due it will span threads although serially, but the sync download code in queue block will be executing simultaneously, thus producing number of errors(server will simply drop most of such connection).
The question is - how to organize this background download? Is it better to have fill dispatch queue with as many download jobs as server will bee comfortable, then write sync download routines in queue block and upon completion of download span other bulk?
Is there any better or more natural way to do that?
Use NSOperationQueue. There's a nice tutorial about it, too: http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues

Core Data - How to force NSPrivateQueueConcurrencyType context save in serial?

I was excited about the newly supported concurrent functions of CoreData since iOS 5.
A private queue is maintained and all save or fetch requests can be done via that queue.
However, can I set up the private queue for CoreData so that it executes request one by one?
My app is downloading news items from a number of feeds. Each time after downloading and parsing from one feed are finished, I just save the feed's items into CoreData via the private queue.
However, since I am downloading and parsing from multiple feeds simultaneously, I always have multiple groups of items, i.e., multiple save requests, for the CoreData.
Now the situation is that I guess CoreData just have a number of threads and each one is saving a group of items into the db. My UI got stuck in the mean time.
Do you think I can control the private queue so that no matter how many simultaneous save requests are, they will be done one by one?
Core Data is (probably) only using one serial queue or thread since its serial. I recently converted my app from using a serial queue I had created (app was 4.3) to use this new option in iOS 5. In all cases when you 'performBlock' the method is handled in a serial fashion. Also, you can now call '[moc performBlocK:...]' from any queue as that call is thread safe!
I believe what you want to do is have your background threads, which are most likely adding options, to use 'performBlock:' (without the wait). The block you provide is then queued and processed in a FIFO fashion. Later on, if your table wants to get objects, it can issue a 'performBlockAndWait:', or optionally your code can ask for the latest objects using performBlock, and at the end of the supplied block message back to your app the set of objects you need.
Also, I only ever save often in development builds, to verify validity. Once you are pretty sure things are working OK, you can then just perform a background save once all the data is downloaded.
EIDT: To reiterate - if you are downloading and also using images or other data while loading a viewController, and lots of things are going on, this is the WORST time to do a save. Use a timer or dispatch_after, and many seconds after everything seems stable THEN do the save.

architectural design for image downloading

I am working on implementing optimized image downloading mechanism from remote server.
I have thought of two different approches.
1. Create one new thread and download all the images in asynchronous way in that single thread.
2. Create thread to download each image. Say i want to download 50 image then there will be 50 thread to download those images.
Which approach is better in terms of design and optimization?
Thanks,
Jim.
Both your solution have pitfall.
1) Having only one thread means that you download only one image at time? This seems inefficient
2) For the same reason that having one download at time is a bad idea having 50 image that download simultaneously is a bad performance idea and will slow down everythings. Also consider that thread that download data are resource expensive for the system (network I/O, Disk I/O, etc..)
I can advice to do not reinvent the wheel and use an NSOperationQueue that is the cocoa / cocoa touch implementation for a queue, that means you can add how many operation (where, in you case, operation are the image download) but you can specify the maximum number of concurrent operation (via the maxConcurrentOperationCount property).
NSOperationQueue handle all the multithreading stuff, and since iOS 4 it uses GCD to execute operations.

iphone app photo upload to server from app threads

I have an app that needs to upload a least 5 photos to a server using API call available with the server. For that I am planning to use threads which will take care of photo upload and the main process can go on with the navigation of views etc. What I cant decide is whether it is OK to spawn five separate threads in iphone or use a single thread that will do the upload. In the later cases obviously it will become quite slow.
Basically an HTTP POST request will be made to the server with the NSMutableURLRequest object using NSCOnnection.
More threads mean more complexity and sync issues, but I can try to write code as neat as possible if it means better performance than a single thread which is simple but is a real stopper if performance is considered.
Anybody with any experience in this kinda app. ??
I would suggest using one extra thread and queuing the uploads, one after the other. You'll end up clogging the network interfaces if you try 5 uploads concurrently. Remember that the iPhone will often be on a 3G or even EDGE connection, not always WiFi, so photo uploads can be really slow, and even slower if there are 5 at once.
You could probably benefit from using NSOperation and NSOperationQueue to nicely handle the ugly threading for you. Wrap up an upload process into an NSOperation, and then queue 5 of them for each image. Should work quite nicely.
Jasarien thanks for the bit on NSOperation and NSOperationQueue. It did simplyify in great measure. But right now I hve run into a major issue.
I spawn an extra thread from the main thread. This thread queues up each of the picture upload operation. So this thread queues up 5 picture opload operations in a queue. This is absolutely fine when working with Mac PC. Now when I push the app to the device, only one pic upload is successful and the rest fails. Most of the cases of failure are due to server time out error. So basically I am wondering, whether NSOperationQueue ensure only one operation at a time or not ? I mean if the first pic upload is in progress and say the next operation is already added in the queue. Would it create an extra thread for the second operation too while the first one is running ? I think as the name suggests, it must wait in the queue until the previous one is done. Not sure how to go abt doing it. I am uploading pic which are taken with iphone camera.