khanduri / bootcamp-twitter

0 stars 0 forks source link

Twitter Clone : Threading issue #1

Open khanduri opened 10 years ago

khanduri commented 10 years ago

/cc @timothy1ee

This is the implementation from scratch using the ios social framework. The following items are causing issues:

timothy1ee commented 10 years ago

It is a subtle threading issue. I'll explain more after Wednesday's lecture on multithreading. The gist is your success blocks aren't being run in the context of the main thread. There's a better solution, but a quick workaround is:

[[TwitterClient instance] homeTimelineWithCount:20 success:^(NSDictionary * data) {
    NSLog(@"hello");
    self.tweets = [Tweet tweetsFromDataDict:data];
    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
} failure:^(NSError * error) {
    NSLog(@"Error:  %@", [error localizedDescription]);
}];
khanduri commented 10 years ago

That worked! Thank you.

Here's the change I made to correct the behaviour: https://github.com/khanduri/bootstrap-twitter/commit/d418668f22ea87e28863e03814a95c5d2172268a.

It would be great of you could share the gist of the better approach as well.

timothy1ee commented 10 years ago

The general rule of thumb when I'm created asynchronous libraries is that the callback blocks should be run in the same thread as the calling method. One of the asynchronous methods in the chain that you're calling is not respecting that rule and the callback block is being run in the context of a background thread. I would figure out which method is doing that, and, in the callback, I would switch back to the main thread, perhaps by using the performSelectorOnMainThread method.

If you still have questions after lecture tomorrow, let me know.