Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Blog Reader iPhone App Viewing a Web Page Implementing a UIWebView

Boaz Keren Gil
Boaz Keren Gil
18,947 Points

Multi Thread App, Bonus from the course

Hello, I'm trying to solve the bonus part in this course where they ask to make the BlogReader app a multi thread with AFNetworking and SDImage.

I think I managed to do it because everything works as expected - the app is usable even when the array is loading at the background.

I just wanted to make sure I did everything as minimal as it should be.

This is my viewDidLoad method:

    [super viewDidLoad];
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        [self createArray];
    });

and this is my createArray method (a void method):

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager GET:@"http://blog.teamtreehouse.com/api/get_recent_summary/" parameters:nil success:^(AFHTTPRequestOperation *operation, id jsonData) {

        self.blogPosts = [NSMutableArray array];
        NSArray *blogPostsArray = [jsonData objectForKey:@"posts"];

        for (NSDictionary *bpDictionary in blogPostsArray) {
            BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]];
            blogPost.author = [bpDictionary objectForKey:@"author"];
            blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"];
            blogPost.date = [bpDictionary objectForKey:@"date"];
            blogPost.url = [NSURL URLWithString:[bpDictionary objectForKey:@"url"]];

            [self.blogPosts addObject:blogPost];

        }

        [self.tableView reloadData];


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

What do you think? For me it seems I can use the interface before the blogObjects array is ready.. which means it's multi threaded right? I didn't add the SDImage code because it's pretty much straight forward.

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hey Boaz,

Your code looks good! FYI, I added code coloring to your existing code by adding in some markdown, like this:

```objective-c

[your code here]

```

Anyway, back to the task at hand. I'm a lot more comfortable in Swift than Objective-C, but as far as I can see your code looks to be doing what you want it to do.

If you want to test the asynchronous part of it, just comment out the asynch line (and the closing curly brace) and see if the UI locks up on you while the request is occurring.

Nice work!

Boaz Keren Gil
Boaz Keren Gil
18,947 Points

Thanks Greg, I was trying to figure out how to do it for like 3 hours :)

Have a nice weekend!