Self-relation messes up contents in fetching

Posted by holographix on Stack Overflow See other posts from Stack Overflow or by holographix
Published on 2011-02-08T16:10:11Z Indexed on 2011/02/22 15:25 UTC
Read the original article Hit count: 238

Hi folks,

I'm dealing with an annoying problem in core data I've got a table named Character, which is made as follows

Character table structure

I'm filling the table in various steps:
1) fill the attributes of the table
2) fill the Character Relation (charRel)

FYI charRel is defined as follows

charRel structure

I'm feeding the contents by pulling the data from an xml, the feeding code is this

curStr = [[NSMutableString stringWithString:[curStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]] retain];
NSLog(@"Parsing relation within these keys %@, in order to get'em associated",curStr);
NSArray *chunks = [curStr componentsSeparatedByString: @","];

for( NSString *relId in chunks ) {
    NSLog(@"Associating %@ with id %@",[currentCharacter valueForKey:@"character_id"], relId);
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"character_id == %@", relId];
    [request setEntity:[NSEntityDescription entityForName:@"Character" inManagedObjectContext:[self managedObjectContext] ]];
    [request setPredicate:predicate];

    NSerror *error = nil;
    NSArray *results = [[self managedObjectContext] executeFetchRequest:request error:&error];

    // error handling code
    if(error != nil)
    {
    NSLog(@"[SYMBOL CORRELATION]: retrieving correlated symbol error: %@", [error localizedDescription]);
    } else if([results count] > 0) {
    Character *relatedChar = [results objectAtIndex:0]; // grab the first result in the stack, could be done better!
    [currentCharacter addCharRelObject:relatedChar];

    //VICE VERSA RELATIONS
    NSArray *charRels = [relatedChar valueForKey:@"charRel"];
    BOOL alreadyRelated = NO;
    for(Character *charRel in charRels) {
        if([[charRel valueForKey:@"character_id"] isEqual:[currentCharacter valueForKey:@"character_id"]])    
        {
        alreadyRelated = YES;
        break;
        }
    }

    if(!alreadyRelated)
    {
        NSLog(@"\n\t\trelating %@ with %@", [relatedChar valueForKey:@"character_id"], [currentCharacter valueForKey:@"character_id"]);
        [relatedChar addCharRelObject:currentCharacter];
    }

    } else {
    NSLog(@"[SYMBOL CORRELATION]: related symbol was not found! ##SKIPPING-->");
    }
    [request release];
}

NSLog(@"\t\t### TOTAL OF REALTIONS FOR ID %@: %d\n%@", [currentCharacter valueForKey:@"character_id"], [[currentCharacter valueForKey:@"charRel"] count], currentCharacter);

error = nil;
/* SAVE THE CONTEXT */
if (![managedObjectContext save:&error]) {
    NSLog(@"Whoops, couldn't save the symbol record: %@", [error localizedDescription]);
    NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
    if(detailedErrors != nil && [detailedErrors count] > 0) {
    for(NSError* detailedError in detailedErrors) {
        NSLog(@"\n################\t\tDetailedError: %@\n################", [detailedError userInfo]);
    }
    }
    else {
    NSLog(@"  %@", [error userInfo]);
    }
}

at this point when I print out the values of the currentCharacter, everything looks perfect. every relation is in its place. in example in this log we can clearly see that this element has got 3 items in charRel:

<Character: 0x5593af0> (entity: Character; id: 0x55938c0 <x-coredata://67288D50-D349-4B19-B7CB-F7AC4671AD61/Character/p86> ; data: {
    catRel = "<relationship fault: 0x9a29db0 'catRel'>";
    charRel =     (
        "0x9a1f870 <x-coredata://67288D50-D349-4B19-B7CB-F7AC4671AD61/Character/p74>",
        "0x9a14bd0 <x-coredata://67288D50-D349-4B19-B7CB-F7AC4671AD61/Character/p109>",
        "0x558ba00 <x-coredata://67288D50-D349-4B19-B7CB-F7AC4671AD61/Character/p5>"
    );
    "character_id" = 254;
    examplesRel = "<relationship fault: 0x9a29df0 'examplesRel'>";
    meaning = "\n  Left";
    pinyin = "\n  zu\U01d2";
    "pronunciation_it" = "\n  zu\U01d2";
    strokenumber = 5;
    text = "\n  \n    <p>The most ancient form of this symbol";
    unicodevalue = "\n  \U5de6";
})

then when I'm in need of retrieving this item I perform an extraction, like this:

// at first I get the single Character record
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSError *error;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"character_id == %@", self.char_id ];
[request setEntity:[NSEntityDescription entityForName:@"Character" inManagedObjectContext:_context ]];
[request setPredicate:predicate];
NSArray *fetchedObjs = [_context executeFetchRequest:request error:&error];

when, for instance, I print out in NSLog the contents of charRel

NSArray *correlations = [singleCharacter valueForKey:@"charRel"];
NSLog(@"CHARACTER OBJECT \n%@", correlations);

I get this

Relationship fault for (<NSRelationshipDescription: 0x5568520>), name charRel, isOptional 1, 
isTransient 0, entity Character, renamingIdentifier charRel, validation predicates (), warnings (), 
versionHashModifier (null), destination entity Character, inverseRelationship (null), minCount 1, 
maxCount 99 on 0x6937f00

hope that I made myself clear.
this thing is driving me insane, I've googled all over world, but
I couldn't find a solution (and this make me think to as issue related to bad coding somehow :P).

thank you in advance guys.
k

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about core-data