在后台线程上将 XML 解析为 CoreData,以免锁定 UI
我的应用程序将 xml 文件解析为 coredata 以填充 uitableview。当我触发刷新时,它应该下载一个新的 xml 文件,在后台线程上解析它,然后将其全部保存到托管对象上下文(替换旧的托管对象上下文数据),然后更新表。这样,与 Twitter 应用程序类似,用户可以触发重新加载,并且在加载时仍然可以滚动。
这似乎是一个非常标准的事情,但我似乎找不到任何帮助。
目前,我正在使用 NSURLConnection 在应用程序委托中下载 XML,以免锁定 UI。下载完成后,它会调用:
ParseOperation *parseOperation = [[ParseOperation alloc] initWithData:receivedData andArray:[NSManagedObjectContext defaultContext]];
[self.parseQueue addOperation:parseOperation];
这将在我认为是后台线程的地方开始解析。我可以在这里下载 xml,然后......
2 问题: 1)如何将其保存到主线程的 ManagedObjectContext 中? 2) 如何访问核心数据中的旧实体以与传入的更新后的 xml 进行比较?
当然有人之前已经遇到过这个问题...有任何示例代码吗?
My application parses an xml file into coredata to populate a uitableview. When I trigger a refresh it should go and download a new xml file, parse it on a background thread, and then save it all at once to the managedobjectcontext (replacing the old managedobjectcontext data) and then update the table. This way, similar to Twitter App, the user can trigger a reload and still scroll around while it's loading.
This seems like a pretty standard thing to do, but I can't seem to find any help on it out there.
Currently I am downloading the XML in my App Delegate using NSURLConnection to not lock up the UI. Once it is finished downloading it calls:
ParseOperation *parseOperation = [[ParseOperation alloc] initWithData:receivedData andArray:[NSManagedObjectContext defaultContext]];
[self.parseQueue addOperation:parseOperation];
This starts the parse in what I believe is a background thread. I can here download the xml and then....
2 Problems: 1) How do I save it to the main thread's managedobjectcontext? 2) How can I access old entities in core data to compare to the updated xml coming in?
Surely someone has to have run into this before... any sample code out there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来你的总体想法是正确的,但你的执行可能会失败。
首先,仅仅将与 Core Data 相关的操作放入 NSOperation 中并不能让一切神奇地工作。您需要确保将 MOC 专用于操作实例(不要使用主线程 MOC),然后使用 PerformBlock 方法来确保使用正确的队列或者附加一个专用的 MOC到同一个 PSC 并使用“did save”通知将更改合并回主线程 MOC。
至于问题的第二部分,您需要提取现有数据以确定项目是新的还是更新,然后做出相应的反应。您可能希望预先获取然后使用内存谓词来过滤您要从该集合中查找的数据,而不是获取每个新项目(这种方法速度更快,但可能会导致内存压力,具体取决于项目的数量和当出现故障时,它们在内存中的大小有多大)。
It sounds like you have the right general idea but your execution may be off.
First, just putting a Core Data related operation into an NSOperation doesn't make it all magically work. You need to be sure to dedicate a MOC to the operation instance (don't use the main thread MOC) and then either use the performBlock method to ensure the right queue is used OR have a dedicated MOC attached to the same PSC and use the 'did save' notifications to merge changes back to the main thread MOC.
As for the second part of your question, you'll need to perform a fetch of the existing data to determine if items are new or updates and then react accordingly. Instead of fetching for each new item you might want to fetch up front and then use in-memory predicates to filter the data you're looking for from that set (this approach is faster but could cause memory pressure depending on the number of items and how large they are in memory when faulted in).