Reputation: 7370
When i parsing json with tableview everything good when have json items but if not load json items and when i clicked back button gives me this error.
erminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
I think json don't load when i clicked fastly back button and gives this error my table view codes under.
@interface MasterViewController ()
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger totalPages;
@property (nonatomic, assign) NSInteger totalItems;
@property (nonatomic, assign) NSInteger maxPages;
@property (nonatomic, strong) NSMutableArray *activePhotos;
@property (strong, nonatomic) NSMutableArray *staticDataSource;
@property (nonatomic, strong) NSMutableArray *searchResults;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
self.activePhotos = [[NSMutableArray alloc] init];
self.searchResults = [[NSMutableArray alloc] init];
self.staticDataSource = [[NSMutableArray alloc] init];
}
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.activePhotos.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (indexPath.row == [self.activePhotos count]) {
cell = [self.tableView dequeueReusableCellWithIdentifier:@"LoadingCell" forIndexPath:indexPath];
UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)[cell.contentView viewWithTag:100];
[activityIndicator startAnimating];
} else {
NSDictionary *photoItem = self.activePhotos[indexPath.row];
cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [photoItem objectForKey:@"name"];
if (![[photoItem objectForKey:@"description"] isEqual:[NSNull null]]) {
cell.detailTextLabel.text = [photoItem objectForKey:@"description"];
}
}
return cell;
}
- (void)loadPhotos:(NSInteger)page
{
NSString *userismim =[[NSUserDefaults standardUserDefaults] stringForKey:@"userisim"];
NSArray* words = [userismim componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString* nospacestring = [words componentsJoinedByString:@""];
NSLog(@"%@",nospacestring);
NSString *apiURL = [NSString stringWithFormat:@"http://bla.com/server/table.php?user=%@",nospacestring];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:apiURL]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
if (!error) {
NSError *jsonError = nil;
NSMutableDictionary *jsonObject = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
NSLog(@"%@",jsonObject);
[self.staticDataSource addObjectsFromArray:[jsonObject objectForKey:@"photos"]];
self.currentPage = [[jsonObject objectForKey:@"current_page"] integerValue];
self.totalPages = [[jsonObject objectForKey:@"total_pages"] integerValue];
self.totalItems = [[jsonObject objectForKey:@"total_items"] integerValue];
self.activePhotos = self.staticDataSource;
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
}] resume];
}
Thanks for everything . i need your help.
Upvotes: 2
Views: 231
Reputation: 244
You are showing activity indicator which will keep rotating till json
loads.
If you are pressing back button before json
loads, what happens is app tries to allocate empty reference to array which is not possible, so it throws an error.
To avoid this, you can stop userInteraction
once request goes, and enable only after getting success or failure response.
To disable interaction, add
[[UIApplication sharedApplicaton] beginIgnoringInteractionEvents]
after
NSURLSession *session = [NSURLSession sharedSession];
And to enable again, add :
[[UIApplication sharedApplicaton] endIgnoringInteractionEvents]
before
if (!error) {
This will solve your issue I hope.
Upvotes: 1