I've searched a lot within SO but I can't find the right answer for my question. Here the problem:
I'm figuring out the right mechanism to send multiple upload requests within an NSOperation
subclass. In particular, this class performs two different operation within its main
method:
- First it retrieves data from a local db
- Then it sends the composed data to a web server
Since, these two operations can take time to executed I wrapped them, as already said, within an NSOperation
.
To upload data I decided to adopt a sync pattern (I need to sync my application with the number of upload requests that has been successfully submitted to the web server).
To perform a similar upload I'm using ASIHttpRequest in a synch fashion like the following.
for (int i = 0; i < numberOfUploads; i++) {
// 1-grab data here...
// 2-send data here
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
int response = [request responseStatusCode];
if(response == 200)
uploads++;
}
}
So, my questions are:
- is this a valid solution to upload data to a web server?
- is it valid to create
ASIHTTPRequest *request
within a background thread? - do I have to use an async pattern? If yes, how to?
Note I'm using ASIHttpRequest for sync requests but I think the same pattern could be applied with NSUrlConnection
class through
sendSynchronousRequest:returningResponse:error:
Thank you in advance.