我正在开发一个RSS阅读器,它使用NSMutableArray(_stories)来存储RSS源的内容。此阵列中的应用程序正在使用的两个不同的线程,并且可以在两次同时访问,因为:线程安全NSMutableArray问题
- 它是的UITableViewController的数据 - 源(读取它的内容,并显示到所期望的信息用户)
- 它由XMLParser(从Internet下载内容,解析XML数据并向其中添加内容)使用。
一些片的代码如下所示:
在的UITableViewController类
- (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"]];
}
在XMLParser的类
- (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];
}
如果用户想要加载 “更多职位”从RSS源我开始另一个解析操作通过_stories阵列作为AR引用解析器,它将其他“帖子”附加到数组中。在解析结束时,调用UITableViewController方法reloadData,然后更新UITableView。
如果用户在解析运行时向上/向下滚动UITableView会怎么样? UITableViewController是否会尝试同时访问数组(以创建单元格)并可能导致应用程序崩溃(它偶尔会发生,但会发生)?
我虽然关于使用@synchronized块的想法,但我不太确定我需要把它完全放在哪里(在代码的很多地方访问数组)。另一个问题是:我将在哪里处理可能由@synchronized块引发的异常?这可能会导致大量冗余代码。
我也虽然关于使用@property没有“nonatomic”,但我不认为它很适合这个问题。
任何想法如何解决这个问题? 在此先感谢。
其实我想实施类似于Tweetie应用程序的东西。我正在检测用户何时到达UITableView(最后一个UITableViewCell)的末尾,并自动启动一个新的分析器。此时,可以同时访问_stories(通过解析器和UITableView(如果用户向上滚动)),问题可能出现在哪里。 – 2009-11-10 10:55:13