核心数据故障消息
Posted
技术标签:
【中文标题】核心数据故障消息【英文标题】:coredata fault messages 【发布时间】:2012-01-26 00:30:36 【问题描述】:我正在将托管记录的内容打印到文件中。我有一个主要实体,例如 MyAppEntity 和几个相关实体(人物、地点等)。
我从文件中看到了这个输出:
/Users/david/Library/Application Support/iPhone Simulator/5.1/Applications/C9ACA218-F2D6-4623-8CAF-5FE763D10FC8/Documents/
MyApp-0001 12.01.25
Summary goes here...
Nothing here yet.
Relationship 'people' on managed object (0x79eaf7f0) <NSManagedObject: 0x79eaf7f0> (entity: MyAppEntity; id: 0x79eae950 <x-coredata:/B86C9CA8-EDE4-452C-BA56-FEF50B4F0FF9/MyAppEntity/p1> ; data:
audioName = 00010125121910;
audioNo = 0;
date = nil;
MyApp = "MyApp-0001 12.01.25";
interpret = "Nothing here yet.";
keyword = (
);
order = 0;
people = (
"0x79ee42f0 <x-coredata:/B86C9CA8-EDE4-452C-BA56-FEF50B4F0FF9/PeopleEntity/p1>",
"0x79ee4300 <x-coredata:/B86C9CA8-EDE4-452C-BA56-FEF50B4F0FF9/PeopleEntity/p2>"
);
place = (
"0x79ee6970 <x-coredata:/B86C9CA8-EDE4-452C-BA56-FEF50B4F0FF9/PlaceEntity/p2>",
"0x79ee6960 <x-coredata:/B86C9CA8-EDE4-452C-BA56-FEF50B4F0FF9/PlaceEntity/p1>"
);
recurring = 0;
summary = "Summary goes here...";
symbol = (
);
type = (
);
) with objects (
<NSManagedObject: 0x79ee47e0> (entity: PeopleEntity; id: 0x79ee42f0 <x-coredata:/B86C9CA8-EDE4-452C-BA56-FEF50B4F0FF9/PeopleEntity/p1> ; data:
definition = nil;
name = me;
order = 0;
people = "<relationship fault: 0x79ef5ce0 'people'>";
),
<NSManagedObject: 0x79ee4a70> (entity: PeopleEntity; id: 0x79ee4300 <x-coredata:/B86C9CA8-EDE4-452C-BA56-FEF50B4F0FF9/PeopleEntity/p2> ; data:
definition = nil;
name = you;
order = 1;
people = "<relationship fault: 0x79e60840 'people'>";
)
)
我有两个问题...首先,故障信息是什么意思?其次,如何检索 people.name 或 place.name.. 的值?
我可以贴出数据模型图,但基本上是这样的:
MyAppEntity:属性: 标题 日期 细节 关系人、地点等。
PeopleEntity:属性: 姓名 描述 命令 关系:人
PlaceEntity:属性: 姓名 描述 命令 关系:地点
关系是多对多的。这是因为一项任务可以有很多人,而同一个人可以处理多项任务。
感谢 Gerry 和 Marcus Zarra 在另一个 post。我终于明白了。如果它对其他人有帮助,这是我的代码:
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *mainEntity = [NSEntityDescription entityForName:@"MainEntity" inManagedObjectContext:context];
[fetchRequest setEntity:mains];
NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSLog(@"Record Count is: %i", [fetchedObjects count]);
// THIS ACTUALLY WORKS.....
for (MainEntity *mains in fetchedObjects)
NSLog(@"Main: %@", mains.name);
NSSet *peopleSet = [mains valueForKey:@"people"];
for (id person in peopleSet)
NSLog(@"name = %@", [person valueForKey:@"name"]);
NSSet *keywordSet = [mains valueForKey:@"keyword"];
for (id keyWord in keywordSet)
NSLog(@"Keyword = %@", [keyWord valueForKey:@"word"]);
NSSet *placeSet = [mains valueForKey:@"place"];
for (id places in placeSet)
NSLog(@"Place = %@", [places valueForKey:@"name"]);
NSSet *symbolSet = [mains valueForKey:@"symbol"];
for (id symbols in symbolSet)
NSLog(@"Symbol = %@", [symbols valueForKey:@"symbolName"]);
NSSet *typeSet = [mains valueForKey:@"type"];
for (id types in typeSet)
NSLog(@"Type = %@", [types valueForKey:@"typeName"]);
if (!fetchedObjects || error)
NSLog(@"[ERROR] COREDATA: Fetch request raised an error - '%@'", [error description]);
return;
【问题讨论】:
【参考方案1】:使用故障,以便仅在需要时加载数据。当您访问关系时,它将被加载。请参阅Core Data Programming Guide 中的Faulting and Uniquing。
一旦您访问到多对多关系,您将拥有一个集合。您可以枚举集合并访问各个托管对象的属性。例如(假设 appObject 是 MyAppEntity 的一个实例):
for (id person in [appObject valueForKey:@"people"])
NSLog(@"name = %@", [person valueForKey:@"name"]);
【讨论】:
他们真的应该选择一个错误的名字。它会引起很多混乱(从它出现在这里的次数来看,无论如何!) 谢谢格里。我还没有喝咖啡,所以请原谅...我输入了以下代码: NSManagedObjectContext *context = [self managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"AppEntity" inManagedObjectContext:context]; [fetchRequest setEntity:实体]; for (id person in entity.people) NSLog(@"name = %@", [person valueForKey:@"name"]); --- 但 entity.people 给了我一个错误 - 在 'NSEntityDescription *' 类型的对象上找不到属性 'people' 非常感谢格里。通过我的Intel 4004大脑消化后,我明白了你写的内容。非常感谢...我已经在上面发布了我的最终代码。如果您发现它有问题,请告诉我。再次,我非常感激。 我不知道我是否按照正确的程序提问,因为还有其他类似的问题。它确实有帮助。 您的代码看起来不错。我忘记使用valueForKey:
访问“人”,我已经更新了我的答案。我几乎总是为我的 Core Data 实体生成类(使用 mogenerator),所以我习惯于总是使用属性而不是 valueForKey:
。以上是关于核心数据故障消息的主要内容,如果未能解决你的问题,请参考以下文章