Thread Vs Async call - iphone

I wanted to know the best between Spawning a Thread or making a async call using NSURLConnection class.

See the documentation.
An NSURLConnection object provides
support to perform the loading of a
URL request. The interface for
NSURLConnection is sparse, providing
only the controls to start and cancel
asynchronous loads of a URL request.
i.e. NSURLConnection only supports asynchronous loading of content. Even the sendSynchronousRequest:returningResponse:error: method is really just a wrapper around async loading that blocks until done.
That class reference has links to both the programming guide and numerous examples.

Related

iPhone: Asynchronous HEAD method

I am very new here. I am trying to update the content of a file from a webserver only if it has changed. I found an example of code here using a synchronous connection in NSURLconnection.
Is it possible to do asynchronously?
NSURLConnection has the method sendSynchronousRequest:returningResponse:error: for synchronous connection. It also has a method sendAsynchronousRequest:queue:completionHandler: for async connection. You need to create a block for completionHandler parameter, which handles the response from web server.
Also:
initWithRequest:delegate:
initWithRequest:delegate:startImmediately:
start
are available for async use. You gonna need to implement some methods of NSURLConnectionDelegate to handle the async response.
Please refer to NSURLConnection Class Reference and NSURLConnectionDelegate Protocol Reference.
i think ASIHTTPRequest is the easiest solution:
set the request Method for your ASIHTTPRequest
// HTTP method to use (eg: GET / POST / PUT / DELETE / HEAD etc). Defaults to GET
NSString *requestMethod;
or you can use a download cache
ASIHTTPRequest can automatically store downloaded data in a cache for use later. This can be helpful in many situations...

iPhone Mobile Application and Polling / Web Service

I'm working with an iPhone developer on an application. I know very little about the iPhone. I'm writing the server-side code in Grails. I'm wondering what is available to me as far as approaches to using a RESTful JSON service with an occasional poll of the service.
From the iPhone:
Is there a way to put these calls on a background thread? when the data comes back is there a callback mechanism? Looking for some basic information on how to accomplish this on the iPhone / Objective-C.
Yes, typically you either provide a delegate that is notified when an asynchronous request has some kind of event (e.g. more data comes in, the request fails, etc.), spawn a background thread that performs synchronous requests, or pass success/failure blocks to an asynchronous request that executes them when appropriate.
But if you are working with an iPhone developer, shouldn't he be dealing with this, not you?
Have a look at NSURLConnection. It automatically puts the request on a background thread and has some callback methods that get called on its delegate.
I prefer using the ASIHttpRequest library for my integrations with RESTful rails apps. One of many advantages is the completion block:
request.completionBlock = ^{ NSLog(#"I'm finally complete!"; };
[request startAsynchronous];

How does Objective-C's asynchronous request model work?

I've been playing with Objective-C the past couple of weeks and have a working app that performs an async HTTP Request to pull some data from Twitter.
I come from a web application background and have become use to how Javascript handles callbacks by passing a function that is called when an XHR request is done loading. As such, I find Objective-C's model of handling async requests with delegate objects and the perfomSelector method pretty darn cool.
[callbackObj performSelector:#selector(callbackMethod:) withObject:argObj];
So my question is, what is the design of Objective-C's async model and how does it work?
All of "Cocoa Classic's" async behavior is / was implemented on top of NSRunLoop. Many of the more recent additions and updates to Cocoa are moving to Grand Central Dispatch.

NSURLConnection sendSynchronousRequest - is it possible to implement without leak

Is there a way to implement NSURLConnection without it leaking? A number of Apps including NYTimes and others (including mine) suffer from this. Anyone have a working implementation?
It appears that best practice is to use NSURLConnection asynchronously.
According to the documentation, +[NSURLConnection sendSynchronousRequest:returningResponse:error:] is built on top of the asynchronous loading code made available by NSURLConnection. It would not be difficult to reimplement this by spawning and blocking on an NSThread, running the request asynchronously in the background on a run loop and ending the thread once either connectionDidFinishLoading: or connection:didFailWithError: is received.
Of course, you are better off using the asynchronous code in the first place; it makes for a much better user experience

Small, simple examples of asynchronous transmission in iPhone

I am trying to work on application in which images should be loaded asynchronously.
Are there any examples related this?
I tried pokeb-asi to understand asynchronous transmission, and it works within that application fine.
But I want to know how to do it from scratch
I am facing following problems.
I don't know the exact way to do it?
what kind of files / frameworks should be added?
The ASIHTTPRequest class is a subclass of NSOperation, so it's typically used by adding the request to either an NSOperationQueue or an ASINetworkQueue. Each request is pulled off the queue, run in a background thread, and then the delegate for that request is notified via a callback method on the main thread.
You could wrap NSURLConnection in an NSOperation and achieve the same thing, however NSURLConnection has an asynchronous API which may suit your needs a bit better.
Try:
http://allseeing-i.com/ASIHTTPRequest/Setup-instructions