开发者

Self-relation messes up contents in fetching

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

Self-relation messes up contents in fetching

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

Self-relation messes up contents in fetching

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


Trying using [managedObjectContext processPendingChanges]; after you've made changes to a managedobject, but before saving the context.


ok I solved this issue,
the problem was related to some facts:

1) the relation has to be inverse, otherwise the system for some (unknown) reasons messes up everything. [like the figure below]

Self-relation messes up contents in fetching

2) In order to have this model work properly, saving stuff in the real way, I added

[managedObjectContext refreshObject:relatedChar mergeChanges:YES];

and

[managedObjectContext processPendingChanges];
[managedObjectContext refreshObject:currentCharacter mergeChanges:YES];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜