views:

596

answers:

2

I have an iPhone app which uses ASIHTTPRequest to communicate to a REST service on my server. When I'm running on the simulator, it works just fine, but when I get onto the phone itself, I get weird behavior.

The very first time I click the button that initiates the request, I get the results back immediately, and all is fine. From that point on, when I click the button to initiate the connection it takes about 2-3 minutes to connect. It almost seems like the ASIHTTPRequest that I kicked off first (and from which I've already received the correct results) has not completed. Is there some sort of magic I need to use to terminate the original request before starting the next one? I assumed that since the -start method returned, and I have results from the server that the original request was completed and I could start another.

Any ideas?

Thanks

--Steve

+2  A: 

You're not suppose to call the -start method, it belongs to the NSOperation. The ASIHTTPRequest interface is either -startSynchronous or -startAsynchronous.

However, it's highly recommend to use the asynchronous call otherwise, your main thread (ie., UI) will be blocked.

From the ASIHTTPRequest documentation[1]

In general, you should use asynchronous requests in preference to synchronous requests. When you use ASIHTTPRequest synchronously from the main thread, your application's user interface will lock up and become unusable for the duration of the request. Synchronous requests are only really suitable for software without a UI (like a script that runs from the terminal), or if you are running the request from a separate thread that you maintain (perhaps from inside your own NSOperation, for example).

[1] http://allseeing-i.com/ASIHTTPRequest/How-to-use

fbrunel
The version of ASIHTTPRequest I haddownloaded back in December didn't have the startSynchronous method, only the -start method. I've downloaded the most recent version of ASIHTTPRequest, and changed the code to use startSynchronous, but the effect is the same.
Steve
Switching to startAsynchronous hasn't helped. The first call goes through, every subsequent call fails with a "A connection failure occurred". The underlying error is a timeout.
Steve
I've changed the code so that each ASIHTTPRequest runs on a seperate queue. Now it works about 50% of the time, the rest of the time it times out.This is becoming frustrating. Should I just give up on ASIHTTPRequest for the phone and go back to NSURLConnection to make this work?
Steve
Why don't you set the timeOutSeconds of ASIHTTPRequest much longer?
tomute
+1  A: 

Steve - What you've described is a common problem that will occur if the requests are attempting to keep a persistent connection. Try this out:

[request setShouldAttemptPersistentConnection:NO];
amehta