5

I want to upload 300 images to server through NSOperationQueue. I have to use one single URL for each image, so I will use 300 URLs. In other words 300 NSOperations for those URLs to push into NSOperationQueue.

Is it Right Approach? Does not it affect application performance in main thread?

Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • 1
    please throw some more light on the url.. say for example you might upload 1st image on www.xyz.com/1 ... 2nd image on www.xyz.com/2 ??? are the url's completely different or just the numbers (eg) changes ?? – Deepak Thakur May 02 '14 at 05:48

3 Answers3

2

If you want to run 300 uploads in parallel then this is rather bad idea regardless of platform.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    This assume the operation queue tries to perform all 300 at once. It is trivial to setup the queue to only do a few at a time. – rmaddy May 02 '14 at 05:52
  • @rmaddy Create 300 threads is a bad idea. But NSoperationQueue does not do so. I limits count of operations running at the same time depending on system conditions. I think this answer is incorrect. – Avt May 02 '14 at 09:26
2

The performance in the main thread will not be affected because the main thread will not take too much time off no matter how many the threads are. But the problem is too much of context switching between a large number of threads running in parallel will give much lesser time to them to execute. Never keep the number of threads running parallel to such a huge number. I would recommend no more than 5 threads at once.

Another advantage associated with running the upload process in a thread (but only in a single thread is) if the upload process stops within the upload lets say after 50 images are uploaded then at least you will have those 50 images there. If you do this all in thread, then may be even after half of the uploading done, you may have no image completely uploaded.

Khawar Ali
  • 3,462
  • 4
  • 27
  • 55
  • you have given good suggestion.But I had already ticked Marcin Answer.I have given upvote to you :) –  May 02 '14 at 05:58
  • When we consider lazyloading in tableview, many downloading operations are added to Queue.How is it possible? –  May 05 '14 at 10:07
  • @JeffWood NSOperationQueue not works this way. It always limits the number of concurrently running operations with a very small number. And new operations will not be started until the previous will be completely finished. – Avt May 05 '14 at 10:41
  • @JeffWood NSOperationQueue could run several operations at the same time. You can set the max number of them using 'setMaxConcurrentOperationCount' method of NSOperationQueue. About FIFO - if A was pushed to NSOperationQueue before B it will be STARTED before B but because of concurrency it could FINISH after B. – Avt May 05 '14 at 11:03
1

It is ok to push 300 NSOperations to NSoperationQueue. NSoperationQueue will not run all of them at the same time - it will limit the number of currently running operations. You also can manually set the number of concurrent operations using

- (void)setMaxConcurrentOperationCount:(NSInteger)count

Sets the maximum number of concurrent operations that the receiver can execute. The specified value affects only the receiver and the operations in its queue. Other operation queue objects can also execute their maximum number of operations in parallel. Reducing the number of concurrent operations does not affect any operations that are currently executing. If you specify the value NSOperationQueueDefaultMaxConcurrentOperationCount (which is recommended), the maximum number of operations can change dynamically based on system conditions.

UPDATE:

Setting the 'MaxConcurrentOperationCount' you set the upper limit. But the system decides itself how many downloads run simultaneously. So even if you will set MaxConcurrentOperationCount to 300 in fact usually system will run 1-10 operations at the same time.

System does not know what operations do. AFAIK it analyses only CPU and memory usage to determine how many operations run simultaneously. So it can run 10 operations simultaneously even if your connection is bad - not a good idea. In such a case, if your operations use network it will be a good idea to set MaxConcurrentOperationCount to 1-2 for GPRS/EDGE connection and to 5-10 for WiFi.

Avt
  • 16,927
  • 4
  • 52
  • 72
  • @JeffWood NSOperation itself does not take a lot of memory. I assume that you do not store all your 300 images in memory. I have just checked - creating 300 NSOperations does not take noticable amount of time. – Avt May 02 '14 at 08:41
  • 1
    Why was this down voted? This is the best answer so far. – rmaddy May 02 '14 at 15:16
  • @JeffWood No. You have to check was operation successful or not yourself. And it not depends on how much operations will be added to NSOperationQueue. – Avt May 05 '14 at 10:36
  • @JeffWood depends on how do you create them. – Avt May 05 '14 at 11:05
  • Can I give 250 as setMaxConcurrentOperationCount ? Is it a Best Approach? –  May 06 '14 at 06:15
  • @JeffWood I have updated the answer. Also you ask me for additional info but at the same time it seems that you do not even upvoted my answer. Do you know that it is possible to change accepted answer? – Avt May 06 '14 at 08:02
  • @JeffWood About FIFO - if A was pushed to NSOperationQueue before B it will be STARTED before B but because of concurrency it could FINISH after B. Please read more about NSOperationQueue in Apples's docs. – Avt May 06 '14 at 09:26