1

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:

  1. First it retrieves data from a local db
  2. 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:

  1. is this a valid solution to upload data to a web server?
  2. is it valid to create ASIHTTPRequest *request within a background thread?
  3. 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.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • 1
    I am already doing similar thing in my project without any problem. NSOperation which is a download ASIHTTPRequest. – msk Mar 30 '12 at 06:29

2 Answers2

2

To answer your questions directly:

  1. Yes, calling an NSUrlConnection (in your case, ASI wrapper) with a sync call is valid in an NSOperation.

  2. You can create NSUrlConnections in background threads, but there are a couple of things to remember here:

    If you use it on a background thread, you have to either call the synchronous methods, or you have to keep the thread alive yourself. Using the async in an NSOperation is explained pretty well here: How do I do an Asychronous NSURLConnection inside an NSOperation? I have used this pattern and it works well.

    NSUrlConnnection Delegate callbacks call back to the thread that the NSUrlConnection was created on. Just something to remember.

  3. You do not have to use the async pattern, but you can. The async pattern provides more flexibility. For example, if you need to cancel the operation, you have the ability to cancel the NSUrlConnection request with the async pattern. With the sync pattern, you are forced to let it run (unless you kill the thread explicitly).

One note: I would reconsider using ASI as it is no longer supported. AFNetworking seems to be the most popular replacement, though I chose to start using NSUrlConnection directly.

Community
  • 1
  • 1
Joseph DeCarlo
  • 3,268
  • 23
  • 28
1

whenever you want to call using ASIHTTPRequest in background thread you have to call synchronous call only, because threads will close the request as soon they are sent,and about your questions

1, this is valid solution but call using synchronours only 2. you can call ASIHTTPRequest in background thread or you can call it using

nsurlconnection sendSynchronousRequest:returningResponse:error:
  1. async pattern will not work for background thread you have to use them on main thread only .

hope it helps .

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
Pavan
  • 443
  • 5
  • 21