I am developing a RSS reader which uses a NSMutableArray (_stories) to store the contents of the RSS Feed. This array is being used by two different threads in the application and may be accessed simultaneously in two occasions, since:
- It is the data-source of the UITableViewController (which reads it's content and shows the desired information to the user)
- It is used by the XMLParser (which downloads the content from the Internet, parses the XML Data and adds the contents to it).
Some piece of code is shown below:
In the UITableViewController class
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
[_stories count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Update the Cell title, for example..
[[cell title] setText:[[[_stories objectAtIndex: storyIndex] objectForKey: @"title"]];
}
In the XMLParser class
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
// after finished the parsing of an Item of the XML, add it "multi-threaded array"
NSLog(@"[_stories addObject:_item]");
[_stories addObject:_item];
}
If the user wants to load "More posts" from the RSS Feed I am starting another parse operation passing the _stories array as a reference to the parser, which appends other "posts" to the array. By the end of the parsing, the UITableViewController method reloadData is called and then the UITableView is updated.
What if the user scroll up/down the UITableView while the parsing is running? Will the UITableViewController attempt to access the _stories array (in order to create the cells) simultaneously and possibly crashing the Application (it occurs very sporadically but occurs)?
I though about the idea of using @synchronized blocks but I'm not quite sure where I'll have to put it exactly (the _stories array is accessed in many places of the code). Another question is: Where will I have to handle the exceptions that are possibly thrown by the @synchronized blocks? This may result in a lot of redundant code.
I also though about using @property without "nonatomic", but I don't think it is well suited for this problem.
Any idea of how to solve this? Thanks in advance.