处理来自服务器的JSON数据,并在CoreData中添加/更新对象 [英] Process JSON data coming from server and add/update the objects in CoreData

查看:532
本文介绍了处理来自服务器的JSON数据,并在CoreData中添加/更新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 coreData 的应用程式,我有两个实体,分别是资料夹笔记。所以他们在文件夹 - >笔记之间一对多关系。即1个文件夹=许多注释。



我对文件夹使用了 fetch results controller ,它显示在UITableView上,当用户点击

>

一切正常。这里的一切都是离线的。



现在我想与服务器同步此数据,以便我可以将此数据同步到多个设备。 我使用的是 NSURLConnection 是默认情况下异步,它有委托函数,它从服务器提供json数据。



现在我正试图处理这个 JsonData ,并且我采用了 FindOrCreate 方法。所以如果文件夹/ Notes存在,那么只是更新它,否则创建新的并保存它的coreData。我使用相同的managedObject和后期进程发生在主线程。



面对的问题。 (这是真实世界的情况)



1)当拉动发生时,用户可能会添加新的文件夹/注释,UI应该响应,但在我< case UI stuck stuck 。因此,我想知道我们如何流畅地创建/更新来自服务器的新数据,UI也是响应式的。



在后台队列中执行后处理,方法是使用



dispatch_async(dispatch_get_global_queue(0,0),^ {



}



但是这里我观察到的是,我得到一个崩溃,说
到未捕获异常NSGenericException,原因:'* 集合<__ NSCFSet:0x1557ecd0>在枚举时发生了变化。

解决方案

这是一个常见的错误,这意味着你不能循环一个集合并修改它在同一时间。说你想循环和查找对象删除。不要在循环中删除,plAce事情在临时集合中删除,并在循环后删除它们。


I am working on app which uses coreData, I have two entities called folder and notes. So their is one to many relationship between folders-> notes. i.e 1 folder = many notes.

I have used fetch results controller for folders, and it is displayed on the UITableView, when user clicks a folder, he goes to notes table, which displays notes inside the particular folder.

User can add and delete folders/Notes.

Everything works fine. Everything here is offline.

Now I am trying to synchronise this data with server, so that I can sync this to multiple devices. I dont do a blind sync, I only sync the data which is changed.

I am using NSURLConnection which is by default asynchronous and it has delegate functions which gives json data from server.

Now I am trying to process this JsonData and I am following a FindOrCreate methodology. So if the folder/Notes exists, then just update it or else create the new one and save it the coreData. I am using the same managedObject and post process happens on the main thread.

Problems faced. (which are real world cases)

1) When the pull happens, user might be adding new folders/notes and the UI should be responsive, but in my case UI gets stuck. So I want to know how can we fluidly create/update new data coming from server and the UI is also responsive.

2) I tried doing the post process in background queue, by using

dispatch_async(dispatch_get_global_queue(0,0), ^{

}

But here what I observed is that, I get a crash, which says * Terminating app due to uncaught exception 'NSGenericException', reason: '* Collection <__NSCFSet: 0x1557ecd0> was mutated while being enumerated.'

解决方案

This is a common error. It means that that you cannot loop a collection and modify it at the same time. Say you wish to loop and find objects to delete. Don't delete in the loop, plAce things to be deleted in a temporary collection, and delete them after the loop.

这篇关于处理来自服务器的JSON数据,并在CoreData中添加/更新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆