从CoreData中简单检索值会为实体返回null [英] Simple retrieving values from CoreData returns null for entities
问题描述
我有两个实体。它们被命名为SP和代码。我分别有10和5属性。我有两个实体的反向关系。我使用 firefox addon sqlite manager 将数据输入模型。所以我不写WRITE 一行像这样
sp.coderelationship = codeObj;或code.sprelationship = spObj;代码中的任何位置。现在在viewcontroller中我检索数据。 $($)
self.spArray = [self.managedObjectContext executeFetchRequest:fR error:& error];
I have two entities. They are named SP and Code. I have 10 and 5 attributes in each respectively. I have inverse relationships on both the entities. I input the data into the model using the firefox addon sqlite manager. So I DID NOT WRITE a line like this sp.coderelationship = codeObj; or code.sprelationship = spObj; anywhere in the code. Now in a viewcontroller i am retrieving data. self.spArray = [self.managedObjectContext executeFetchRequest:fR error:&error];
for(SP *spObj in self.spArray)
{
NSLog(@"description is %@",spObj.sPDescription);
Code *codObj = spObj.coderelationship;
NSLog(@"it has value %@",codObj);
NSLog(@" unique name is %@",codObj.uniqueCodeName);
}
输出的描述正确。它打印一个值xyz。但是对于最后2个日志它的null。
我的实体设计
The output is correct for description. It prints a value "xyz". But for the last 2 logs its null. My Entities design
-sPDescription
-sPName
-sPID
关系
-coderelationship
-sPDescription -sPName -sPID Relationship -coderelationship
代码
-codeName
-codeID
-codeDate
关系
-sprelationship
Code -codeName -codeID -codeDate Relationship -sprelationship
信息需要让我知道。
推荐答案
执行提取请求之前,请添加以下代码:
Before executing the fetch request add this code:
NSString *relationshipKeyPath = @"coderelationship"; // Set this to the name of the relationship on "SP" that points to the "Code" objects;
NSArray *keyPaths = [NSArray arrayWithObject:relationshipKeyPath];
[fR setRelationshipKeyPathsForPrefetching:keyPaths];
此外,您是否已验证关系已正确插入db?
Also, have you verified that the relationships have correctly been inserted into the db?
这篇关于从CoreData中简单检索值会为实体返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!