CoreData关系错误?
我有一个与单位有“一对多”关系的订单。 当我尝试按顺序login单元(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'>";
另外,当我只想要单位时,为什么要打印整个对象呢?
这不是一个错误,它是核心数据的一个称为“错误”的特性。 这是苹果公司的描述 :
错误减less了应用程序消耗的内存量。 错误是代表尚未完全实现的pipe理对象的占位符对象,或表示关系的集合对象:
托pipe对象错误是适当类的实例,但其持久variables尚未初始化。 关系错误是表示关系的集合类的子类。 错误允许核心数据在对象图上放置边界。 由于没有实现故障,pipe理对象故障消耗的内存较less,而与故障相关的pipe理对象根本不需要在内存中表示。
如果你想看到每个单元对象,那么你将不得不专门访问它们。 尝试以下操作:
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. } }
为了查看“一对多”关系的所有对象 ,可以使用关系的NSSet
的allObjects
方法来调用它。
在你的具体情况下,在fetchedObjects
打印第一个Order的所有单元的代码如下所示:
for (Order *order in fetchedObjects) { NSLog(@"%@", [order.units allObjects]); break; }
在Swift中它会这样:
for order in fetchedObjects { for unit in order.units.allObjects as [Unit] { println(unit) } }
是的,我也有同样的问题。 试试只打印这个表的一些字段,例如:
for(Category *category in fetchedObjects) { for(Cards *card in category.cards) { NSLog(@"Question %@", card.question); NSLog(@"Card %@", card); } }
这是非常直观的。 我挠了脑袋大概一整天,直到我意识到,我不能打印出整个NSSet对象或每个NSManagedObject的集合像这样:
for (Order *order in fetchedObjects) { for (Unit *unit in [order units]) { NSLog(@"%@", unit); } }
这将在xcode中产生错误..但是如果我打印出每个NSMangedObject中的每个属性,那就好了。 我想苹果有关于自动加载多对象的内存问题。 在存在多对多关系链的情况下,它将消耗大量内存。 所以需要延迟加载每个属性。
for (Order *order in fetchedObjects) { for (Unit *unit in [order units]) { NSLog(@"Unit Name : %@", unit.name); } }
这将打印出单位名称..