1

I have a table view that use result of fetching data with NSURLSession as a datasource. Here is my NSArray which is responsible about that table.

@property (strong, nonatomic) NSMutableArray *results;

And this is my delegate and datasource method

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_results count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell...
    WordResult *word = (WordResult *)[_results objectAtIndex:indexPath.row];
    cell.textLabel.text = word.defid;

    return cell;
}

In my viewDidLoad, I fetched request from Mashape and try to map the result into my custom class WordResult Here is my fetch method

#pragma mark - GET Request
- (void)fetchDataFromMashape:(NSURL *)URL {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    [request setHTTPMethod:@"GET"];
    [request setValue:API_KEY_MASHAPE forHTTPHeaderField:API_MASHAPE_HEADER_1];
    [request setValue:API_ACCEPT forHTTPHeaderField:API_MASHAPE_HEADER_2];

    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        self.results = [self processResultData:jsonResult];
    }];

    [task resume];
}

- (NSMutableArray *)processResultData:(NSDictionary *)dict {

    NSArray *list = [dict objectForKey:@"list"];
    NSMutableArray *tempListOfWord = [[NSMutableArray alloc] init];

    if (list) {
        for (NSDictionary *item in list) {
            WordResult *word = [[WordResult alloc] initWithDictionary:item];
            [tempListOfWord addObject:word];
        }
    }

    NSLog(@"Result array of word: %@", tempListOfWord);
    return tempListOfWord;
}

My problem is, I dont know where to reload data after my result array was assigned by fetch method and dismissing my progress HUD that I showed on my viewDidLoad. Here is my viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];

    [SVProgressHUD show];
    [self fetchDataFromMashape:_finalURLrequest];

}

So where should I put [SVProgressHUD dismiss] and [self.tableView reloadData] after my request has been finished? Any help would be appreciated.

Sonic Master
  • 1,238
  • 2
  • 22
  • 36

4 Answers4

1

Reload your table on main thread

dispatch_async(dispatch_get_main_queue(), ^{
    [_tableView reloadData];
});
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Wasif Saood
  • 1,998
  • 1
  • 11
  • 12
0

Try this:

You can put [SVProgress dismmis] and also reload your tableview [self.tableView reloadData] after self.results = [self processResultData:jsonResult];

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Kamlesh Shingarakhiya
  • 2,757
  • 2
  • 16
  • 34
0

A great tableView coding pattern is putting reload call in setters of model objects, then you won't miss any data change. For example setter for result in your case:

-(void)setResult:(NSMutableArray*)result{
  _result = result;
  [self.tableView reloadData]
  [SVProgressHUD dismiss]
}
lcl
  • 1,045
  • 11
  • 13
0

In your viewdidload before calling fetchdata do

self.results=[[NSMutableArray alloc]init];

And then call your [self.tableView reloadData] within the completion block after assigning the array with data

And then call svprogress hud hide method outside the block after [task resume] in the fetch data function

Subhajit Halder
  • 1,427
  • 13
  • 21