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.