0

I am trying to use SVProgressHUD with cocoapods. I have install it in my xcode workspace following numerous tutorials online. It seems simple enough to use, but I cannot get it to work. Here my code to load data from rottentomatoes app. I want to show "loading" while the network request is doing its thing. I am wondering if this is ui thread issue ? I added a sleep inside the network request because the results were coming back too fast !

- (void) reload{
    [SVProgressHUD showWithStatus:@"Updating" maskType:SVProgressHUDMaskTypeBlack];

    NSString *url = @"http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/top_rentals.json?";

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [NSThread sleepForTimeInterval:3];
        NSDictionary *object = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[object objectForKey:@"movies"]];
        self.movies = [[NSMutableArray alloc]init];

        for(NSDictionary *item in array){
            SFMovie *movie = [[SFMovie alloc]initWithDictionary:item];
            [self.movies addObject:movie];

        }
                [SVProgressHUD dismiss];
        [self.tableView reloadData];
    }];
}

EDIT for comment: The reload is called on both init methods (since I am using storyboard for this project)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self reload];
    }
    return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if(self){
        [self reload];
    }
    return self;
}

Second EDIT: I added a pull down to refresh and the "updating ..." shows up when I pull the tableview down. But it does not show up on init.

- (void)viewDidLoad
{
    [super viewDidLoad];


    UIRefreshControl *refreshControl = [UIRefreshControl new];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh..."];
    self.refreshControl = refreshControl;   
}

- (void)refresh:(UIRefreshControl *)sender{
    [self reload];
    [sender endRefreshing];
}

So I guess I cannot run UI stuff on init methods as the view is not ready. I am now calling the reload method on viewdidload and it works file. Thank you John !

U-L
  • 2,671
  • 8
  • 35
  • 50
  • Is `reload` called on the main thread? – John Jan 19 '14 at 18:56
  • @John reload is called from init method, so I guess it is the UI thread. Thank you. – U-L Jan 19 '14 at 19:20
  • This doesn't entirely pertain to the answer, but I would suggest moving that logic to `viewDidLoad` -- since the `tableView` is not exactly ready at this point, dispatching a request with `[self.tableView reloadData]` in the callback introduces a race condition. – John Jan 19 '14 at 19:29
  • Thank you @John. I added a second edit and did the same thing you mention here. That seems to resolve the issue for me. – U-L Jan 19 '14 at 19:32
  • Great, I'll answer-ify it for future views – John Jan 19 '14 at 19:33

1 Answers1

0

In any init methods, messing with the view hierarchy is tricky business -- keep that logic in viewDidLoad to avoid any of those modifications from being wiped (especially from storyboard initialization).

John
  • 2,675
  • 2
  • 18
  • 19