我有一个与单位有“多对多”关系的订单。当我尝试按顺序记录单位(NSSet)时,出现错误错误:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Order"
inManagedObjectContext:mainContext];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [mainContext executeFetchRequest:fetchRequest
error:nil];
for (Order *order in fetchedObjects) {
NSLog(@"%@", [order units]);
break;
}
[fetchRequest release];
结果:
Relationship 'units' fault on managed object (0x6d9dd00) <Order: 0x6d9dd00> (entity: Order; id: 0x6d88e40 <x-coredata://97A3F3D5-ABA7-499A-A460-5E25CF49C528/Order/p1> ; data: {
composition = Hemlock;
condition = "";
consignee = "";
consigneeCompanyName = "";
contactAlternatePhone = "";
contactEmail = "";
contactFirstName = "";
contactLastName = "";
contactPhone = "";
customer = "Havard Empire";
customerCompanyName = "";
customerNotes = "";
dateDue = "1/13/2012 12:00:00 AM";
dateEntered = "1/6/2012 12:00:00 AM";
dateOrdered = "1/6/2012 12:00:00 AM";
deliveryAddress1 = "";
deliveryAddress2 = "";
deliveryCity = "";
deliverySpecialInstructions = "";
deliveryState = "";
deliveryZip = "";
depth = 01;
detail = "";
freightRate = "";
grade = Cull;
instructionsDirectionsNotes = "";
lastUpdated = "1/6/2012 3:00:43 PM";
length = 01;
location = "Lucedale, ms";
matsPerLoad = "";
memoLineNotes = "";
notes = "";
orderID = 201205134922479;
orderNumber = 01062012;
pUP = Cable;
pricePerItem = "";
purchaseOrderNumber = "";
pushToQuickBooks = True;
quantity = 0;
quickbooksCompany = 1;
salesman = "Accounting Adj";
separateRate = False;
taxRate = "";
totalLoads = "";
type = "2ply Mat";
units = "<relationship fault: 0x6dacf20 'units'>";
vendorID = 10;
width = 01;
})
不打印单位。它说"<relationship fault: 0x6dacf20 'units'>";
另外,为什么在我只需要单位时打印整个对象?
答案 0 :(得分:85)
这不是错误 - 它是Core Data的一个名为'faulting'的功能。这是Apple的description:
Faulting可减少应用程序消耗的内存量。一个 fault是占位符对象,表示托管对象 还没有完全实现,或者是一个集合对象 代表一种关系:
托管对象错误是相应类的实例,但是 它的持久变量尚未初始化。关系错误 是集合类的子类,表示 关系。错误允许核心数据在边界上设置边界 对象图。由于未实现故障,因此管理对象发生故障 消耗较少的内存,而不是与故障相关的管理对象 需要在记忆中表现出来。
如果要查看每个Unit对象,则必须专门访问它们。请尝试以下方法:
for (Order *order in fetchedObjects) {
for (Unit *unit in [order units]) {
NSLog(@"%@", unit);
//I am not at a computer, so I cannot test, but this should work. You might have to access each property of the unit object to fire the fault, but I don't believe that is necessary.
}
}
答案 1 :(得分:9)
为了查看“to-many”关系的所有对象,您可以使用 allObjects
关系的方法NSSet
。
在您的具体情况下,打印fetchedObjects
中第一个订单的所有单位的代码将如下所示:
for (Order *order in fetchedObjects) {
NSLog(@"%@", [order.units allObjects]);
break;
}
答案 2 :(得分:5)
在Swift中它会是这样的:
for order in fetchedObjects {
for unit in order.units.allObjects as [Unit] {
println(unit)
}
}
答案 3 :(得分:3)
for(Category *category in fetchedObjects)
{
for(Cards *card in category.cards)
{
NSLog(@"Question %@", card.question);
NSLog(@"Card %@", card);
}
}
答案 4 :(得分:1)
这非常直观。我抓了大约一整天,直到我意识到我无法打印整个NSSet对象或集合中的每个NSManagedObject,如下所示:
for (Order *order in fetchedObjects) {
for (Unit *unit in [order units]) {
NSLog(@"%@", unit);
}
}
这会在xcode中产生错误..但是如果我打印出每个NSMangedObject中的每个属性,那就没问题了。我猜Apple对自动加载多对象有内存关注。在存在多对多关系的情况下,它将消耗大量内存。所以需要延迟加载每个属性。
for (Order *order in fetchedObjects) {
for (Unit *unit in [order units]) {
NSLog(@"Unit Name : %@", unit.name);
}
}
这将打印出单位名称..