NSManagedObject 不插入它:相关很多

发布于 2024-10-20 09:29:29 字数 1733 浏览 9 评论 0原文

我的 XML 导入要求我在插入之前检查现有对象。换句话说,在确定是否保存之前,我需要将每条记录保存在临时托管数据对象中。 *** 注意:请参阅此线程中的最后一个答案:

有没有办法在不插入的情况下实例化 NSManagedObject?

我使用了上面链接中最后一个答案的方法,使用 insertIntoManagedObjectContext:nil 将传入的一条记录放入一个临时对象中,而无需插入语境。

在我的导入中,我有两个表:一个单表记录和紧随其后的多个相关多记录。这很有效,除了我有很多相关的记录。

现在我也将多表记录插入到它们自己的托管对象中。问题是当我要保存一条记录时,我还创建了多个相关的多个对象。这么多记录如何保存?我可以从零上下文中获取它们并循环它们吗?

这是新记录开始的代码:

        // Incoming record is for the one table.
    if ([elementName isEqualToString: self.xmlRecordTagDelimiter]) {
        NSEntityDescription *entity = [NSEntityDescription entityForName:self.xmlEntityName inManagedObjectContext:xmlManagedObjectContext];
        self.xmlCurrentRecordTempObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
        thisTagIsForManyTable = NO;
    }   
    // Incoming record is for the many table.
    if ([elementName isEqualToString: self.xmlManyRecordTagDelimiter]) {
        NSEntityDescription *entity = [NSEntityDescription entityForName:self.xmlRelatedManyEntityName inManagedObjectContext:xmlManagedObjectContext];
        self.xmlCurrentManyRecordTempObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];

        thisTagIsForManyTable = YES;
    }

以及我要将单表记录保存到内容中的代码:

        [self.managedObjectContext insertObject:self.xmlCurrentRecordTempObject];

        // Store what we imported already.
        if (![self.xmlManagedObjectContext save:&error]) {
                         ...... snip.....
        }

谢谢

My XML import requires that I check for an existing object before I insert. In other words I need to hold each record in a temporary managed data object before I determine whether to save it or not. *** NOTE: Please refer to the last answer in this thread:

Is there a way to instantiate a NSManagedObject without inserting it?

I took the approach of the last answer in the link above using insertIntoManagedObjectContext:nil which put the incoming one-record into a temporary object without a context.

Within my import I have two tables: a one-table record and multiple related-many record following right behind it. This works great except that I have related-many records following this.

Right now I'm inserting the many-table records into their own managed object with nil also. The question is when I'm about to save the one record, I also have multiple related many objects I've created. How do I save the many records? Can I fetch them from a nil context and loop through them?

Here is the code for the beginning of a new record:

        // Incoming record is for the one table.
    if ([elementName isEqualToString: self.xmlRecordTagDelimiter]) {
        NSEntityDescription *entity = [NSEntityDescription entityForName:self.xmlEntityName inManagedObjectContext:xmlManagedObjectContext];
        self.xmlCurrentRecordTempObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
        thisTagIsForManyTable = NO;
    }   
    // Incoming record is for the many table.
    if ([elementName isEqualToString: self.xmlManyRecordTagDelimiter]) {
        NSEntityDescription *entity = [NSEntityDescription entityForName:self.xmlRelatedManyEntityName inManagedObjectContext:xmlManagedObjectContext];
        self.xmlCurrentManyRecordTempObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];

        thisTagIsForManyTable = YES;
    }

And the code where I'm about to save a one-table record into a contect:

        [self.managedObjectContext insertObject:self.xmlCurrentRecordTempObject];

        // Store what we imported already.
        if (![self.xmlManagedObjectContext save:&error]) {
                         ...... snip.....
        }

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素手挽清风 2024-10-27 09:29:29

听起来您正在将 nil 上下文视为另一个托管对象上下文。事实并非如此。当您将 nil 作为上下文传递给 initWithEntity:insertIntoManagedObjectContext: 时,您请求不要将创建的托管对象插入到任何上下文中。它没有插入到名为nil的托管对象上下文中。它不会插入到任何托管对象上下文中。

因此,当您询问是否可以从 nil 上下文中获取许多对象时,答案是“不”。这是因为没有 nil 上下文。

但是,NSManagedObject 是对象。您可以将许多对象存储在一个数组中,当您要保存时,只需循环遍历该数组,找到要保存的许多对象,然后仅将它们插入到您的上下文中。

It sounds like you're thinking of a nil context as yet another managed object context. This is not the case. When you pass nil as the context to initWithEntity:insertIntoManagedObjectContext: you are requesting that the created managed object not be inserted into any context. It is not inserted into a managed object context called nil. It is not inserted into any managed object context.

So, when you ask whether you can fetch your many objects from the nil context, the answer is "no." This is because there is no nil context.

However, NSManagedObjects are objects. You could store your many objects in an array and, when you're about to save, just loop through the array, find the many objects that you want to save, and only insert those into your context.

Hello爱情风 2024-10-27 09:29:29

如果它们不在上下文中,它们就不会被持久化。如果您在创建它们后释放它们,它们将永远消失。

如果你想创建一个关系,你需要将它们插入到 NSManagedObjectContext 中,然后通过关系加入它们。

从评论更新

除非您通过其他机制(例如字典)保留它们,否则无法检索它们。就我个人而言,我会使用唯一的 key* 作为键将它们存储在字典中。

*当然,唯一的密钥取决于您和您的数据。

If they are not in a context they are not persisted. If you release them after creating them they are gone forever.

If you want to create a relationship you need to insert them into a NSManagedObjectContext then join them via the relationship.

Update from comments

There is no way to retrieve them unless you are holding onto them via some other mechanism such as a dictionary. Personally I would store them in a dictionary using the unique key* as the key.

*The unique key of course is up to you and your data.

素食主义者 2024-10-27 09:29:29

我所做的是使用一个实体的 nil 对象上下文和一个数组来保存所有多表对象,直到我需要保存它们。

感谢大家。

What I've done is use the nil object context for the one entity and an array to hold all of the many-table objects until I need to save them.

Thanks to All.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文