Process JSON data coming from server and add/updat

2019-09-10 09:59发布

问题:

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.'

回答1:

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.