Alex
Alex

Reputation: 1581

Create a copy of a NSManagedObject

I need to temporarily store the content of a NSManagedObject into a dictionary. Because core data has its own memory management procedures, I don't want to keep any strong pointers to the NSManagedObject's fields, only the values are of interest at this point (values are passed between view controllers, the MOCs are different). I can't create weak pointers either because I want to control when the memory reclaim is done.

I tried a few things, all failed or did not fit the purpose.

  1. a duplicate [[myNSMO alloc] initWithEntity:[NSEntityDescription entityForName:entity inManagedObjectContext:myNSMO.managedObjectContext] insertIntoManagedObjectContext:nil]; It's working, but does not fit into my app design (without getting into details)

  2. generate a NSDictionary from the NSManagedObject, using [myNSMO dictionaryWithValuesForKeys:<#(NSArray *)#>]. That's not ok because it returns a dictionary with the addresses of the NSManagedObject fields.

  3. create a NSDictionary populating each key-value using a copyWithZone, like this [myDictionary setObject:[myNSMO.field copyWithZone:nil] forKey:@"Key"]; Doesn't work either, I still get the field address...

  4. Manually enter each field with [myDictionary setObject:[NSString stringWithFormat:@"%@",myNSMO.field ] forKey:@"Key"]; It's fine this time, I do get new memory allocation. But that's highly time consuming to code this manually...

Any chance that someone found clever way to do that? the reason option 1) did not work is because I use the dictionary as a queue. I first store a copy of the object, then pop the entry out when required. A copy of that particular dictionary entry is then returned to the asking method. The problem is that I can't create a copy of an NSManagedObject that was created using [[...] insertIntoManagedObjectContext:nil];

Any solutions?

Upvotes: 3

Views: 2956

Answers (1)

Tommy
Tommy

Reputation: 100642

It's safe to keep strong pointers to the fields of a managed object in most senses — relationships are special but the actual Foundation objects of dates, strings and numbers are ordinary objects that'll stay in memory if you have a strong reference.

That being said, to create a dictionary copy containing all the properties of an entity you could do something like:

NSArray *properties = [[object entity] properties];
NSMutableDictionary *dictionaryRepresentation = [NSMutableDictionary dictionary];
for(NSAttributeDescription *attribute in properties)
{
    // we want only actual attributes, not relationships
    // or fetched properties
    if([attribute isKindOfClass:[NSAttributeDescription class]])
    {
        [dictionaryRepresentation
                 setObject:[object valueForKey:attribute.name]
                 forKey:attribute.name];
    }
}

So you're using the fact that managed objects expose a description of their entities which includes a list of properties, whittling those properties down to just the attributes, then using key-value coding to fetch the current value of each property and finally inserting it into the dictionary.

If for some reason you did want copies of the properties — though, as I say, there's absolutely no reason to do so — you'd copy (and autorelease if you're not using ARC) each property when inserting it into the dictionary.

Upvotes: 9

Related Questions