Skip to content
This repository

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

Closed
peterpaulis opened this Issue · 4 comments

4 participants

Peter Paulis Stephen Vanterpool Ryan Mathews Tony Arnold
Peter Paulis

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);
        }
    }
}
Stephen Vanterpool
Collaborator

So is this fix compensating for bad data?

Ryan Mathews

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.

Tony Arnold
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.