fixing if import relationship is a single value for a multy relationship in CD #240

Closed
peterpaulis opened this Issue Aug 23, 2012 · 4 comments

Projects

None yet

4 participants

@peterpaulis

fixing if import relationship is a single value for a multy relationship in CD

ALREADY FIXED WITH adding line

if ([relationshipInfo isToMany] && ([relatedObjectData respondsToSelector:@selector(countByEnumeratingWithState:objects:count:)])))

[relationshipInfo isToMany] can be true, but also relatedObjectData can be a single nonenumerable value

here

- (void) MR_setRelationships:(NSDictionary *)relationships forKeysWithDictionary:(NSDictionary *)relationshipData withBlock:(void(^)(NSRelationshipDescription *,id))setRelationshipBlock
{
    for (NSString *relationshipName in relationships) 
    {
        if ([self MR_importValue:relationshipData forKey:relationshipName]) 
        {
            continue;
        }
        
        NSRelationshipDescription *relationshipInfo = [relationships valueForKey:relationshipName];
        
        NSString *lookupKey = [[relationshipInfo userInfo] valueForKey:kMagicalRecordImportRelationshipMapKey] ?: relationshipName;
        id relatedObjectData = [relationshipData valueForKey:lookupKey];
        
        if (relatedObjectData == nil || [relatedObjectData isEqual:[NSNull null]]) 
        {
            continue;
        }
        
        SEL shouldImportSelector = NSSelectorFromString([NSString stringWithFormat:@"shouldImport%@:", [relationshipName MR_capitalizedFirstCharacterString]]);
        BOOL implementsShouldImport = (BOOL)[self respondsToSelector:shouldImportSelector];
        void (^establishRelationship)(NSRelationshipDescription *, id) = ^(NSRelationshipDescription *blockInfo, id blockData)
        {
            if (!(implementsShouldImport && !(BOOL)[self performSelector:shouldImportSelector withObject:relatedObjectData]))
            {
                setRelationshipBlock(blockInfo, blockData);
            }
        };
        
        if ([relationshipInfo isToMany] && ([relatedObjectData respondsToSelector:@selector(countByEnumeratingWithState:objects:count:)]))
        {
            for (id singleRelatedObjectData in relatedObjectData) 
            {
                establishRelationship(relationshipInfo, singleRelatedObjectData);
            }
        }
        else
        {
            establishRelationship(relationshipInfo, relatedObjectData);
        }
    }
}
@blackgold9
Member

So is this fix compensating for bad data?

@peterpaulis

yes

@ryanjm
Contributor
ryanjm commented Oct 17, 2013

What do you mean by "single nonenumerable value"? Like the id of the object? Trying to figure out if this is related to the problem I'm running into with #591.

@tonyarnold
Contributor

I believe this is fixed in develop.

If you can still replicate the issue under the latest in-development version of MagicalRecord (3.0 at the time of writing), please feel free to re-open and one of @magicalpanda/team-magicalrecord will take another look. 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