Skip to main content

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


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!


Source: Tips4allCCNA FINAL EXAM

Comments

  1. It is unbelievably simple to do this with ASIHttpRequest.

    (Asynchronous is so simple, there is no reason you would ever do it not-asynchronously.)

    Here are some rough extracts that might get you started.

    ...
    ASIFormDataRequest *request;
    ...
    NSURL *url = [NSURL URLWithString:@"https://blah.blah/blah.cgi?blah"];
    request = [ASIFormDataRequest requestWithURL:url];

    [request setPostValue:@"fred" forKey:@"username"];
    [request setPostValue:@"flint" forKey:@"passie"];
    [request setPostValue:@"stone" forKey:@"town"];

    // send up data...
    [request setData:[NSData dataWithBytes:blah length:blah] forKey:@"thefile"];

    // or perhaps something like...
    [request setData:imageData withFileName:@"blah.png"
    andContentType:@"image/jpeg" forKey:@"photoimage"];

    [request setDelegate:self];
    [request setDidFinishSelector:@selector(postingDone:)];
    [request setDidFailSelector:@selector(postingDoneProblem:)];
    [request startAsynchronous];
    ...

    -(void) postingDone:(ASIHTTPRequest *)request
    {
    // it worked
    }
    -(void) postingDoneProblem:(ASIHTTPRequest *)request
    {
    // failed
    }


    Couldn't really be any easier. You're basically just typing out the fields and values.

    Per your question, here is how you cancel an "in-flight" request... just set the delegate to nil and then "cancel" it.

    [myRequest setDelegate:nil];
    [myRequest cancel];
    [myRequest release];


    ASIHttpRequest is the "miracle library". If you are new to iOS, ASIHttpRequest is simply THE most used 3rd party library. Essentially, every single iPhone app of the 300,000 iPhone apps uses it.

    If at all possible BE SURE to donate a few bucks to the guy -- if he stops supporting that library, 100,000 iPhone programmers are buggered!

    the documentation is trivial, a child can follow it:
    http://allseeing-i.com/ASIHTTPRequest/How-to-use
    "Creating an asynchronous request"

    it is probably - almost certainly - the most amazingly simple networking library on any platform. It is trivial to do what you describe, happily. Enjoy.

    ReplyDelete
  2. NSURLConnection is async by default and supports cancelation as well as delegate methods when connection has been established, data has been received or whole request has been completed.

    Also data transfer takes place in background so that UI stays responsive.

    ReplyDelete
  3. 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].

    ReplyDelete
  4. Check out ASIHTTPRequest, specifically, the ASINetworkQueue subclass, which is described as:


    ASINetworkQueue

    A subclass of NSOperationQueue that
    may be used to track progress across
    multiple requests.


    I've only used ASIHTTPRequest for a single synchronous request to download directly to disk, which was easy to implement, but I've heard good reports of using queues to manage multiple asynchronous server requests at once.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex