Relationship import from array of IDs, doesnt create attribute for related object #223

Closed
peterpaulis opened this Issue Jul 27, 2012 · 4 comments

3 participants

@peterpaulis

By Import

If the relationship is a as an array of IDs, than the corresponding relationship object is created, but its (relatedByAttribute) attribute is not set!

Fixed this with

- (BOOL) MR_importValuesForKeysWithObject:(id)objectData
{
    void (^importRelationshipLogic)(NSRelationshipDescription *, id) = ^(NSRelationshipDescription *relationshipInfo, id localObjectData) {
        
        NSManagedObject *relatedObject = [self MR_findObjectForRelationship:relationshipInfo withData:localObjectData];
        
        if (relatedObject == nil)
        {
            NSEntityDescription *entityDescription = [relationshipInfo destinationEntity];
            relatedObject = [entityDescription MR_createInstanceInContext:[self managedObjectContext]];
        }
        
        [relatedObject MR_importValuesForKeysWithObject:localObjectData];
        
        if ((localObjectData) && (![localObjectData isKindOfClass:[NSDictionary class]])) 
        {
            NSString * relatedByAttribute = [[relationshipInfo userInfo] objectForKey:kMagicalRecordImportRelationshipLinkedByKey];
                
            if (relatedByAttribute) 
            {

                if (![self MR_importValue:localObjectData forKey:relatedByAttribute])
                {
                    [relatedObject setValue:localObjectData forKey:relatedByAttribute];
                }
                    
            }
        }
        
        [self MR_addObject:relatedObject forRelationship:relationshipInfo];            
    };

    return [self MR_performDataImportFromObject:objectData relationshipBlock:importRelationshipLogic];
}
@peterpaulis

also

- (NSManagedObject *) MR_findObjectForRelationship:(NSRelationshipDescription *)relationshipInfo withData:(id)singleRelatedObjectData
{
    NSEntityDescription *destinationEntity = [relationshipInfo destinationEntity];
    NSManagedObject *objectForRelationship = nil;
    id relatedValue;
    if ([singleRelatedObjectData isKindOfClass:[NSDictionary class]]) 
    {
        relatedValue = [singleRelatedObjectData MR_relatedValueForRelationship:relationshipInfo];
    } 
    else 
    {
        relatedValue = singleRelatedObjectData;
    }
    
    if (relatedValue) 
    {
        NSManagedObjectContext *context = [self managedObjectContext];
        Class managedObjectClass = NSClassFromString([destinationEntity managedObjectClassName]);
        NSString *primaryKey = [relationshipInfo MR_primaryKey];
        objectForRelationship = [managedObjectClass MR_findFirstByAttribute:primaryKey
                                                               withValue:relatedValue
                                                               inContext:context];
    }

    return objectForRelationship;
}

@Vortec4800

I'm not sure which code is better (the pull request code or the code from the thread), but I was running into this same issue and used the code from the thread to fix it. I did make one change however, in the MR_importValuesForKeysWithObject: method I replaced this line:

NSString * relatedByAttribute = [[relationshipInfo userInfo] objectForKey:kMagicalRecordImportRelationshipLinkedByKey];

with this line:

NSString * relatedByAttribute = [[relationshipInfo userInfo] objectForKey:kMagicalRecordImportRelationshipLinkedByKey] ?: primaryKeyNameFromString([[relationshipInfo destinationEntity] name]);

so that if you didn't set the relatedByAttribute key it would generate one from the entity name.

I'll let you guys fight over which code is better though, I'm not sure which covers all the bases. The code in the thread seemed more thorough, but sometimes a simpler implementation is better.

@Vortec4800

The old pull request was closed because the issue stemmed from a server issue in that case. I took the code from this thread, cleaned it up a bit and made it match closely to what was there already and created a new pull request from it.

@tonyarnold tonyarnold was assigned Dec 29, 2013
@tonyarnold

This appears to have been added in 46e31b1. It'll be in MagicalRecord 2.3 and 3.0. Thanks!

@tonyarnold tonyarnold closed this Apr 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment