Alex Rivera | Logout

Objective-C: server requests in a thread (like AsyncTask in Android)

Asked 2010-11-24T17:44:40.760
11

I would like to start a server request, you can cancel.

My idea is to start the request in a thread so that the user interface does not freeze. So you can kill the whole thread including the request with a click on a "Cancel"-button.

With Android it works: the server request gets started in a "AsyncTask" and in the "onReturn()"-method I can react as soon as the server request finish.

How can I implement this using Objective-C on iOS? My first attempt was a "NSInvocationOperation". You can cancel the operation, but it's difficult to handle when a request is completed and results are available. I think NSInvocationOperation is not the solution for my issue.

The would you recommend to me? Is NSThread the right choice for me?

Thank you very much!

Edit
Report

1 Answer

2

Cocoa's built-in async networking code is not thread-based but works with run loop events, but the result (asynchronous connections) is the same.

Create an NSURLConnection with +[NSURLConnection connectionWithRequest:delegate:]. The delegate you set will be informed about the progress of the connection and can cancel it anytime with -[NSURLConnection cancel].

answered 2010-11-24T17:56:16.390

Your Answer