获取过程中的自相关性弄乱了内容
我正在处理核心数据中的一个烦人的问题我有一个名为 Character 的表,它是由 如下
我正在按各个步骤填写表格:
1)填充表的属性
2)填写字符关系(charRel)
仅供参考,charRel定义如下
I' m 通过从 xml 中提取数据来提供内容, 馈送代码就是这样的
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]);
}
}
当我打印出 currentCharacter 的值时, ,一切看起来都很完美。每一种关系都各就其位。 在此日志的示例中,我们可以清楚地看到该元素在 charRel 中有 3 个项目:
<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";
})
然后,当我需要检索该项目时,我执行提取,如下所示:
// 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];
例如,当我在 NSLog 中打印出内容时charRel
NSArray *correlations = [singleCharacter valueForKey:@"charRel"];
NSLog(@"CHARACTER OBJECT \n%@", correlations);
我得到了这个
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
希望,我已经说清楚了。
这件事让我发疯,我用谷歌搜索了世界各地,但是
我找不到解决方案(这让我想到与错误编码相关的问题:P)。
提前谢谢你们。
k
I'm dealing with an annoying problem in core data I've got a table named Character, which is made
as follows
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在对托管对象进行更改之后、保存上下文之前尝试使用
[managementObjectContext processPendingChanges];
。Trying using
[managedObjectContext processPendingChanges];
after you've made changes to a managedobject, but before saving the context.好的,我解决了这个问题,
这个问题与一些事实有关:
1)关系必须是相反的,否则系统会由于某些(未知)原因而搞乱一切。 [如下图]
2) 为了让这个模型正常工作,将东西保存在真实中方式,我添加了
并且
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]
2) In order to have this model work properly, saving stuff in the real way, I added
and