线程安全 NSMutableArray 问题 [英] Thread-safe NSMutableArray question
问题描述
我正在开发一个 RSS 阅读器,它使用 NSMutableArray (_stories) 来存储 RSS 提要的内容.该数组由应用程序中的两个不同线程使用,并且可能在两种情况下同时访问,因为:
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:
- 它是 UITableViewController 的数据源(它读取它的内容并向用户显示所需的信息)
- 它由 XMLParser 使用(它从 Internet 下载内容,解析 XML 数据并将内容添加到其中).
部分代码如下所示:
在 UITableViewController 类中
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"]];
}
在 XMLParser 类中
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];
}
如果用户想从 RSS 提要加载更多帖子",我将开始另一个解析操作,传递 _stories 数组作为对解析器的引用,解析器将其他帖子"附加到数组.解析结束时,调用 UITableViewController 方法 reloadData,然后更新 UITableView.
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.
如果用户在解析运行时向上/向下滚动 UITableView 会怎样?UITableViewController 是否会尝试同时访问 _stories 数组(以创建单元格)并可能使应用程序崩溃(它偶尔发生但会发生)?
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)?
我虽然有使用@synchronized 块的想法,但我不太确定我必须把它准确放在哪里(_stories 数组在代码的许多地方都可以访问).另一个问题是:我必须在哪里处理@synchronized 块可能抛出的异常?这可能会导致大量冗余代码.
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.
我也想过在没有非原子"的情况下使用 @property,但我认为它不太适合这个问题.
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.
推荐答案
我刚刚发现了这个,我怀疑你还在寻找答案,但也许这对其他人有用:)
I just spotted this and I doubt you are still looking for an answer, but maybe this will be useful to someone else :)
快速浏览一下,我将如何解决这个问题:
On a quick look, how I would solve this is:
- 在后台线程中运行新的解析,并将此加载到另一个数组中.
- 当新数组加载完毕并且您拥有所有数据后,在主线程上运行一个方法,将新数组添加到您的 _stories 数组中,然后通知 UI 刷新.
您现在已经安全地更新了您的 _stories 数组并显示了它,没有并发访问该数组的任何风险.
You have now safely updated your _stories array and have displayed it, without any risk of concurrent access to the array.
这篇关于线程安全 NSMutableArray 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!