CoreData,NSManagedObject fetch或create如果不存在 [英] CoreData, NSManagedObject fetch or create if not exists

查看:159
本文介绍了CoreData,NSManagedObject fetch或create如果不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析大量的文本文件,并将其内容组织为托管对象。在文本文件中有很多重复,所以抵押任务之一是摆脱它们。

I am trying to parse a lot of text files and organize their contents as managed objects. There are a lot of duplicates in the text files, so one of the "collateral" tasks is to get rid of them.

我想在这方面做的是检查一个具有给定内容的实体是否存在,如果没有,我创建一个。但是,我有不同的实体具有不同的属性和关系。我想要的是一种函数,它需要一些属性作为输入,并返回一个新的NSManagedObject实例,我不会担心,如果它被插入到数据存储或从中获取。

What i am trying to do in this respect is to check whether an entity with the given content exists, and if it doesn't, i create one. However, i have different entities with different attributes and relationships. What i want is a kind of function that would take a number of attributes as an input and return a new NSManagedObject instance, and i wouldn't have to worry if it was inserted into the data store or fetched from it.

有没有?

我还必须说我是核心数据的noob。

I must also say that i am a noob at core data.

更多细节,如果你愿意:

我想写一种字典。我有单词(字{NSString *单词,< - > - 规则规则}),规则(规则{NSString名称,< - >>词语,< - > PartOfSpeech partOfSpeech,< > Ending endings}),词性(PartOfSpeech {NSString name,< - Rule Rule})(我希望符号清楚)。

I am trying to write a sort of dictionary. I have words (Word{NSString *word, <<-> Rule rule}), rules (Rule{NSString name, <->>Word word, <<->PartOfSpeech partOfSpeech, <<-> Ending endings}), parts of speech (PartOfSpeech{NSString name, <<-> Rule rule}) (i hope the notation is clear).

如果它们具有相同的字属性,并且链接到相同的规则。两个规则是相同的,如果他们有相同的结局和词性。

Two words are equal, if they have the same word property, and "linked" to the same rule. Two rules are the same, if they have the same endings and part of speech.

到目前为止,我写了一个方法,它接受NSPredicate,NSManagedObjectContext和NSEntityDescription作为输入,首先查询数据存储,并返回一个实体,如果它找到一个,一个新的,将其插入到数据存储区并返回。然而,在这种情况下,我不能填充新的实体与必要的数据(在该方法),所以我必须传递一个NSDictionary的属性及其值的名称,并插入它们,或通过引用返回一个标志关于是否我创建一个新的对象或返回一个旧的,所以我可以填充它的数据外面。

So far i've written a method that takes NSPredicate, NSManagedObjectContext and NSEntityDescription as an input, and first queries the datastore and returns an entity if it finds one, or creates a new one, inserts it into the datastore and returns it. However, in this case I cannot populate the new entity with the necessary data (within that method), so i have to either pass an NSDictionary with the names of attributes and their values and insert them, or return by reference a flag as to whether i created a new object or returned an old one, so that i could populate it with the data outside.

但它看起来很丑陋。我肯定有一些比这更优雅的东西,我只是找不到它。请,帮助我,如果可以的话。

But it looks kind of ugly. I'm sure there must be something more elegant than that, i just couldn't find it. Please, help me if you can.

推荐答案

您的路径基本正确。核心数据是一个对象图。没有很多的动态内置。也没有upsert。像你推测,你必须获取,如果它不存在,你插入一个。

Your basically on the right path. Core Data is an object graph. There not a lot of dynamic built in. There's also no "upsert". like you surmise, you have to fetch and if it doesn't exist, you insert one.

这篇关于CoreData,NSManagedObject fetch或create如果不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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