English 中文(简体)
核心数据关系有错?
原标题:CoreData relationship fault?

我有一项命令,即与单位建立“对手”关系。 当我试图在命令中记录这些单位时,我有错误:

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;
})

页: 1 它说:<relshipfalls: 0x6dacf20 Unit >”;

而且,为什么在我只想要单位的时候,它要印刷整个物体?

最佳回答

This isn t an error - it is a feature of Core Data called faulting . Here is Apple s description:

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.

如果你想要看到每个单位的反对,那么你将不得不具体接触它们。 如下:

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.
    }
}     
问题回答

为了,见to-many> relationship的所有物体,您可将其与>> 之间关系方法NSSet

在您的具体情况下,在<条码>上打印第一期Units。 希望:

for (Order *order in fetchedObjects) {
    NSLog(@"%@", [order.units allObjects]);
    break;
}

尽快这样做:

for order in fetchedObjects {
    for unit in order.units.allObjects as [Unit] {
        println(unit)
    }
}

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);
        }
    }

这非常反省。 我将我的头盔整整整天,直到我认识到,我无法勾画整个NSSet物体或每个NSManaged。 宗旨:

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.

for (Order *order in fetchedObjects) {
   for (Unit *unit in [order units]) {
      NSLog(@"Unit Name : %@", unit.name);
   }
}

这将印刷单位名称。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签