ios CoreData 关系故障?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8876234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CoreData relationship fault?
提问by 0xSina
I have an Order which has a "to-many" relationship with Units. When I try to log the units (NSSet) in Order, I get a fault error:
我有一个与单位有“对多”关系的订单。当我尝试按顺序记录单位 (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];
results in:
结果是:
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;
})
The units aren't printed. It says "<relationship fault: 0x6dacf20 'units'>";
不打印单位。它说"<relationship fault: 0x6dacf20 'units'>";
Also, why is it printing the entire object when I only want units?
另外,当我只需要单位时,为什么要打印整个对象?
回答by sosborn
This isn't an error - it is a feature of Core Data called 'faulting'. Here is Apple's description:
这不是错误 - 它是 Core Data 的一个特性,称为“故障”。这是苹果的描述:
Faulting reduces the amount of memory your application consumes. A fault is a placeholder object that represents a managed object that has not yet been fully realized, or a collection object that represents a relationship:
A managed object fault is an instance of the appropriate class, but its persistent variables are not yet initialized. A relationship fault is a subclass of the collection class that represents the relationship. Faulting allows Core Data to put boundaries on the object graph. Because a fault is not realized, a managed object fault consumes less memory, and managed objects related to a fault are not required to be represented in memory at all.
故障会减少应用程序消耗的内存量。故障是一个占位符对象,代表尚未完全实现的托管对象,或代表关系的集合对象:
托管对象故障是适当类的实例,但其持久变量尚未初始化。关系错误是代表关系的集合类的子类。Faulting 允许 Core Data 在对象图上设置边界。由于没有实现故障,因此托管对象故障消耗的内存较少,并且根本不需要在内存中表示与故障相关的托管对象。
If you want to see each Unit object then you will have to specifically access them. Try the following:
如果您想查看每个 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.
}
}
回答by veducm
In order to see all the objects of a "to-many"relationship, you can call it with the allObjects
method of the relationship's NSSet
.
为了查看“对多”关系的所有对象,可以使用allObjects
关系的NSSet
.
In your specific case, the code to print all the Unitsof the first Orderin fetchedObjects
would be like this:
在特定情况下,代码打印所有单位第一顺序中fetchedObjects
会是这样的:
for (Order *order in fetchedObjects) {
NSLog(@"%@", [order.units allObjects]);
break;
}
回答by Louie Bertoncin
In Swift it would go like so:
在 Swift 中,它会像这样:
for order in fetchedObjects {
for unit in order.units.allObjects as [Unit] {
println(unit)
}
}
回答by Ned
Yep, I had also the same problem. Try just print some field of this table, in example:
是的,我也遇到了同样的问题。尝试只打印此表的某些字段,例如:
for(Category *category in fetchedObjects)
{
for(Cards *card in category.cards)
{
NSLog(@"Question %@", card.question);
NSLog(@"Card %@", card);
}
}
回答by Lin
This is very counter intuitive. I scratched my head for about a whole day until I realised that I cannot print out the whole NSSet object or each NSManagedObject in the set like so :
这是非常反直觉的。我挠了整整一天,直到我意识到我无法打印出整个 NSSet 对象或集合中的每个 NSManagedObject ,如下所示:
for (Order *order in fetchedObjects) {
for (Unit *unit in [order units]) {
NSLog(@"%@", unit);
}
}
This will generate fault in xcode .. But If I print out each attribute in each NSMangedObject, it is fine. I guess Apple has memory concern over Auto-loading to-many objects. In a scenario where there is a chain of to-many relationships, it will consume a lot of memory. So need to lazy-load each attribute.
这将在 xcode 中产生错误 .. 但是如果我打印出每个 NSMangedObject 中的每个属性,那就没问题了。我猜苹果对自动加载到许多对象有内存问题。在存在一对多关系链的场景下,会消耗大量内存。所以需要延迟加载每个属性。
for (Order *order in fetchedObjects) {
for (Unit *unit in [order units]) {
NSLog(@"Unit Name : %@", unit.name);
}
}
That will print out the unit name..
这将打印出单位名称..