1

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

Community
  • 1
  • 1
b.dot
  • 109
  • 1
  • 11
  • It sounds to me like your data model may not be setup quite like you need. It sounds like your thinking of Core Data in SQL terms which will lead to grief. – TechZen Mar 08 '11 at 23:52

3 Answers3

3

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.

James Huddleston
  • 8,410
  • 5
  • 34
  • 39
  • Thank James. That was my first thought before discovering the nil object technique above and the right answer for my app right now. – b.dot Mar 10 '11 at 00:33
1

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.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • Thanks, Marcus. My app has a context. Previous to this, upon launch, the app would delete all objects in the entity and import the data into the two tables using insert with a join to the relationship. The problem is now I need to do a merge where I need to store the current record in an object and search for an existing record before I commit it. As shown in this posted link http://stackoverflow.com/questions/3868514/is-there-a-way-to-instantiate-a-nsmanagedobject-without-inserting-it – b.dot Mar 09 '11 at 00:40
  • Just to clarify things: I have an entity called events and a related many entity called categories. Right now I store the event in a temporary object (without a context) until I'm ready to commit it. If the record should be saved I assign a context using insertObject. I now have also imported all of the categories into temporary objects. What's the best way to retrieve them being that they are without a context? – b.dot Mar 09 '11 at 01:04
0

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.

b.dot
  • 109
  • 1
  • 11