Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strike>Based on your response in the question comments, it sounds like you are calling [tableView reloadData] from a background thread.</p> <p><strong>Do not do this.</strong> UIKit methods, unless otherwise specified, <strong>always</strong> need to be called from the main thread. Failing to do so can cause no end of problems, and you are probably seeing one of them.</strike> </p> <p>EDIT: I misread your comment. It sounds like you are not updating the UI from a background thread. But my comments about the architecture (i.e. why are you updating in a background thread AFTER the download has finished?).</p> <p>You state that "when the data comes back from the server, I call a background operation..." This sounds backwards. Normally you would have your NSURLConnection (or whatever you are using for the download) run on the background thread so as not to block to UI, then call out to the main thread to update the data model and refresh the UI. Alternatively, use an asynchronous NSURLConnection (which manages its own background thread/queue), e.g.:</p> <pre><code>[NSURLConnection sendAsynchronousRequest:(NSURLRequest *) requestqueue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler]; </code></pre> <p>And just make sure to use [NSOperationQueue mainQueue] for the queue.</p> <p>You can also use GCD, i.e., nested dispatch_async() calls (the outer to a background queue for handling a synchronous connection, the inner on the main queue to handle the connection response).</p> <p>Finally, I will note that you in principle can update your data model on the background thread and just refresh the UI from the main thread. But this means that you need to take care to make your model code thread-safe, which you are likely to mess up at least a couple times. Since updating the model is probably not a time consuming step, I would just do it on the main thread too.</p> <p>EDIT:</p> <p>I am adding an example of how one might use GCD and synchronous requests to accomplish this. Clearly there are <strong>many</strong> ways to accomplish non-blocking URL requests, and I do not assert that this is the best one. It does, in my opinion, have the virtue of keeping all the code for processing a request in one place, making it easier to read.</p> <p>The code has plenty of rough edges. For example, creating a custom dispatch queue is not generally necessary. It blindly assumes UTF-8 encoding of the returned web page. And none of the content (save the HTTP error description) is localized. But it does demonstrate how to run non-blocking requests and detect errors (both at the network and HTTP layers). Hope this is helpful.</p> <pre><code>NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; dispatch_queue_t netQueue = dispatch_queue_create("com.mycompany.netqueue", DISPATCH_QUEUE_SERIAL); dispatch_async(netQueue, ^{ // We are on a background thread, so we won't block UI events (or, generally, the main run loop) NSHTTPURLResponse *response; NSError *error; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error]; dispatch_async(dispatch_get_main_queue(), ^{ // We are now back on the main thread UIAlertView *alertView = [[UIAlertView alloc] init]; [alertView addButtonWithTitle:@"OK"]; if (data) { if ([response statusCode] == 200) { NSMutableString *body = [[NSMutableString alloc] initWithData:data encoding:NSUTF8StringEncoding]; [alertView setTitle:@"Success"]; [alertView setMessage:body]; } else { [alertView setTitle:@"HTTP Error"]; NSString *status = [NSHTTPURLResponse localizedStringForStatusCode:[response statusCode]]; [alertView setMessage:status]; } } else { [alertView setTitle:@"Error"]; [alertView setMessage:@"Unable to load URL"]; } [alertView show]; [alertView release]; }); }); dispatch_release(netQueue); </code></pre> <p>EDIT:</p> <p>Oh, one more big rough edge. The above code assumes that any HTTP status code != 200 is an error. This is not necessarily the case, but handling this is beyond the scope of this question.</p>
    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.
 

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