iOS Core Data获取新插入的,尚未保存的对象 [英] iOS Core Data fetch newly inserted, not yet saved objects

查看:147
本文介绍了iOS Core Data获取新插入的,尚未保存的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在批处理(导入过程)中创建一些数据在CoreData实体,我想提交结束或回滚的错误(所以保存之间将无法工作)。



问题是,我需要创建一个实体Person,稍后我需要重新使用该实体。



所以我试图用一个谓词(personId == 4711)来获取它,但是这个过程可能已经存在, 。但是,虽然我已经设置
[fetchRequest setIncludesPendingChanges:YES]; 它找不到新创建的Person对象。



我阅读了此此问题这个回答哪个状态,它不可能?

解决方案



div>

从我所知道的CoreData,这是不可能的(如果我错了,请纠正我)。



这是可能的,你永远不想在每个对象的基础上查询您的商店。
对大型(超过几十个)导入的性能影响是巨大的。



我的建议是在导入阶段创建一个由您的唯一标识符键入的字典(从存储现有实体预取,并为不存在的实体创建新的实体)。



注意:您应该注意不要在多线程环境中从不同的上下文执行多个插入。



存储内容:1 - > P1,3 - > P3

服务回应:1 - > Data1,2 - > Data2



算法:
响应完成时,从响应中获取所有唯一的ID - > recievedIds = @ [1,2] >
创建 recievedIds 集创建personId - >数据的映射/字典:

@ {1:Data1, 2:Data2}



通过谓词从商店提取:

[NSPredicate predicateWithFormat:@personId IN%@ ,recievedIds]

从结果数组中创建字典。

在这种情况下: existingItems = @ {1:P1}



传递 recievedIds 中的所有ID: b $ b 1)如果id存在于 existingItems 中更新现有对象\\ b $ b 2)否则创建一个新人并将数据插入到新的



这只会从商店获取一次。
$ $ $ $
您只保存一次。 $ b ==>只有2次到商店,而不是每个对象的旅行


I have to create some data in CoreData entities in batch (import process) and I would like to "commit" at the end or "rollback" on an error (so saving inbetween won't work).

The problem is that I for example need to create an entity "Person" and later in that progress I need to re-use that entity. BUT it can already exists BEFORE this process or may be created WHILE this import process.

So I'm trying to fetch it with a predicate "(personId == 4711)". But although I have set [fetchRequest setIncludesPendingChanges:YES]; it doesn't find the newly created Person object.

I read this this question and this answer which state, that it is not possible? Am I right?

If so, how can I workaround / handle this?

解决方案

From what I know of CoreData, this is indeen impossible (please correct me if I'm wrong).

However, even if this was possible, you never want to query your store on a "per object" basis.
The performance impact on large (more then a few dozens) imports is huge.

My suggestion for you is to create a dictionary keyed by your unique identifier during the import stage (prefetch from the store existing entities and create new ones for ones that aren't).

Note: You should be careful not to perform multiple inserts from different contexts in a multithreaded environment. in such cases you will need a coordinator to prevent duplications.

example:

store content: 1 --> P1, 3 --> P3
service response: 1 --> Data1, 2 --> Data2

Algorithm:
on response completion, get all unique ids from response --> recievedIds = @[1,2]
during the creation of recievedIds set create a mapping/dictionary of personId --> data:
@{1 : Data1, 2 : Data2}

fetch from the store by predicate:
[NSPredicate predicateWithFormat:@"personId IN %@",recievedIds]
Create a dictionary from the resulting array.
in this case: existingItems = @{1 : P1}

Pass over all ids in recievedIds:
1) if the id exist in existingItems update the existing object
2) else create a new person and insert the data to the new record.

This will only fetch from the store once.
And you only save once.
==> only 2 trips to the store instead of a trip per object

这篇关于iOS Core Data获取新插入的,尚未保存的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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