Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT:</p> <p>Here is some code to run operations in a background thread. Use of any function called from the UI thread will run on on the UI thread. You can use a technique similar to the one specified below to run your operation on a background thread, and then dispatch the result back to the UI thread for later use.</p> <p>Here is the technique I used, you may replace my sendSynchronousRequest call with your <code>AFHTTPRequestOperation</code> :</p> <p>Specify a special type (a block) so you can pass blocks of code around.</p> <pre><code>typedef void (^NetworkingBlock)(NSString* stringOut); </code></pre> <p>Then, you need to dispatch to a background thread, so as not to freeze your UI thread.</p> <p>Here's a function to call stuff in a background thread, and then wait for a response, and then call a block when done without using the UI thread to do it:</p> <pre><code>- (void) sendString:(NSString*)stringIn url:(NSString*)url method:(NSString*)method completion:(NetworkingBlock)completion { //build up a request. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; NSData *postData = [stringIn dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPMethod:method]; [request setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //or whatever [request setHTTPBody:postData]; //dispatch a block to a background thread using GCD (grand central dispatch) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURLResponse *response; NSError* error; //request is sent synchronously here, but doesn't block UI thread because it is dispatched to another thread. NSData* responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error]; //call complete, so now handle the completion block. if (completion) { //dispatch back to the UI thread again dispatch_async(dispatch_get_main_queue(), ^{ if (responseData == nil) { //no response data, so pass nil to the stringOut so you know there was an error. completion(nil); } else { //response received, get the content. NSString *content = [[NSString alloc] initWithBytes:[responseData bytes] length:responseData.length encoding:NSUTF8StringEncoding]; NSLog(@"String received: %@", content); //call your completion handler with the result of your call. completion(content); } }); } }); } </code></pre> <p>Use it like this:</p> <pre><code>- (void) myFunction { [self sendString:@"some text in the body" url:@"http://website.com" method:@"POST" completion:^(NSString *stringOut) { //stringOut is the text i got back }]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload