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.
Related
I am making a website, where I provide a button which can :
POST a JSON to a route (nothing hard for now)
but the process should also start multiples system commands, and at the end of it provide a zip the user can download
To do that, I think I need a queue. Because two users connected on the same time CANNOT start the process.
Is a queue ok ? But I do not know how to retain the session and send back the zip file...
PS: I am using angular2 & a Python WS.
There are three parts to your question:
First, allow only one execution of your system commands per user at a time.
This could be as simple as maintaining a synchronized flag bit per user, which stores 1 if the request can be processed, 0 otherwise. Whenever a post request comes, first check if this flag is set or not. Continue if it is not 1 return some non-200 status code. Else, set it to 0 and trigger the commands.
Second, handle multiple POST requests that trigger the system commands.
You should use a queue only if your system commands take more time and usually run in the background.
Three, how to retain the session
Retaining a session is not a good idea. You have two options. One, client continuously pools to another end point to check if the zip creation is complete or not. Second, (better than first) use websockets to send the notification back to client once the zip creation is complete.
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
Currently i am following thread to check wheather my internet is active or not in my application, but as it is taking time to give the response ,so this will freeze my UI.
So is there any way to implement it without freezing UI(like NSOperation).
If the internet is indeed down, it takes time. It is limitation of Apple's API. We have to live with it or put a timer to cancel the operation after 30 secs or so. But if a genuine response especially via GPRS takes more than 30 secs, you will be canceling that too if you put timer condition.
Alternatively, you could check for internet status asynchronously and display an ActivityIndicator or similar in the main thread. This means that you create a new thread which will run parallel with your main thread (in your case, the GUI that are freezing).
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.
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.