iOS:从JSON更新核心数据数据库 [英] iOS: update a core data DB from JSON

查看:323
本文介绍了iOS:从JSON更新核心数据数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,以这种方式存储来自JSON文件的数据(它是身份的示例)

In my app I store data from a JSON file in this way (it's a example for an identity)

NSManagedObjectContext *context = [self managedObjectContext];

for (id element in array){

    NSLog(@"element:%@", element);

Str *str = [NSEntityDescription insertNewObjectForEntityForName:@"Str" inManagedObjectContext:context];
Id_loc *loc = [NSEntityDescription insertNewObjectForEntityForName:@"Id_loc" inManagedObjectContext:context];
loc.id_loc = @"id_1";
[str addLocObject:loc];
}

问题是每天从web服务解析这个JSON;所以每天都可以更改其Str身份(我的JSON的对象)的数据和数量。
问题是,更新我的核心数据DB更好的解决方案是什么?
我有两种可能:

The problem is that that I parse this JSON every day from a web service; so every day it can change data and number of its "Str" identity (an object of my JSON). The question is, what's the better solution to update my core data DB? I have two possibilities:

1-使用NSFetchRequest更新我的数据库,检查一个元素是否存在,并更新它;如果不存在,我应该创建它。最后,如果一个对象不存在于JSON中,但它在我的核心数据DB中,我应该删除它,这是一个复杂的系列控制。

1- update my DB with NSFetchRequest, check if an element exist, and update it; if it not exist, I should create it. And finally if an object is not present in JSON but it's inside my core data DB, I should delete it, it's a complex series of controls.

2-删除所有核心数据db并再次使用新的JSON文件填充。

2- delete all core data db and populate again with new JSON file.

在你看来,更好的选择是什么?

In your opinion, what's the better choice?

我说你在我的应用程序中,我只能读我的数据库,我不需要删除或修改它,当我使用它。

I say you that in my app I must only read my db, and I don't need to delete or modify it when I use it.

推荐答案

对于我的应用程序 - 我使用CoreData以及 - 我决定使用选项1,由于几个原因。

For my app - where I use CoreData as well - I decided to go with option 1, for several reasons.

首先,它只是一个更多的编码,但性能是这么好的方法。如果你打算使用NSFetchedResultController你应该肯定去与更新选项。否则,它将监听您的更改,它将重新加载所有的视图这么多次,因为您删除所有对象和readd他们。

First on all it's just a bit more coding, but performance is so much better with that approach. If you are planning to use NSFetchedResultController you should definitely go with update option. Otherwise it will listen to your changes and it will reload all the views so many times, because you delete all objects and readd them.

最重要的是删除所有对象是非常昂贵的操作在CoreData,因为你必须先获取所有的对象,比删除它们一个一个!这是真的很糟糕的想法:)
对于几百个条目,如果你计划在后台更新,而不是在更新过程中阻止屏幕,它真的使应用程序性能下降。

Most important removing all objects is really expensive operation in CoreData because you have to fetch all objects first, than delete them one by one !!! It's really bad idea :) For several hundreds entries it's really taking application performance down if you are planning to do updates in background not blocking a screen during update process.

作为一个优化,我建议保持每个对象的服务器端时间戳,并只从最后更新的时间移动端的增量更新。

As an optimization I would recommend to keep on your server side timestamp for each object and pull only changes from last updated time on mobile side - incremental updates.

这篇关于iOS:从JSON更新核心数据数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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