I’m trying to get a blog app to refresh the articles, and add any new articles that may have been written since prior launch.
The TableView uses an array _allEntries. I was told that when the user clicks the refresh button, i should clear the array, and after clearing it, run the original launch code to parse the rss and repopulate the table view. I am doing that with:
[_allEntries removeAllObjects];
[self refresh];
However, when I do that, it crashes with error:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds for empty array'
Any thoughts as to what I am doing wrong?
EDIT: Here is my viewDidLoad and refresh code, along with the update code.
- (void)viewDidLoad { [super viewDidLoad];
self.allEntries = [NSMutableArray array];
self.queue = [[[NSOperationQueue alloc] init] autorelease];
self.feeds = [NSArray arrayWithObjects:@"feedurlhere", nil];
[self refresh];
}
- (void)refresh {
for (NSString *feed in _feeds) {
NSURL *url = [NSURL URLWithString:feed];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[_queue addOperation:request];
}
}
-(void)updatearticle {
[_allEntries removeAllObjects];
[self refresh];
}
It works when you first load, because feeds gets populated in your viewDidLoad method, before you call refresh. When you remove all objects and then call refresh, there’s nothing in the array, hence the out of bounds error. You should move all the code (except the [self refresh]) out of viewDidLoad and into the refresh method.