Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top