2012-01-16 8 views
40

Ho un ordine che ha una relazione "to-many" con le unità. Quando provo ad accedere alle unità (nsset) in ordine, ottengo un errore di:Errore di relazione CoreData?

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]; 

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

Le unità non sono stampati. Dice "<relationship fault: 0x6dacf20 'units'>";

Inoltre, perché stampa l'intero oggetto quando voglio solo unità?

risposta

80

Questo non è un errore: è una funzionalità di Core Data chiamata "in errore". Ecco Apple 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.

Se volete vedere ogni oggetto Unità allora si dovrà accedervi specifico. Prova il seguente:

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. 
    } 
}  
+2

Grazie per la risposta, ma non funziona. L'ho provato prima. – 0xSina

+0

Cosa mostra nel registro quando lo fai in questo modo? – sosborn

+0

Niente. non itera attraverso il secondo ciclo. Controllo il conteggio e restituisce zero. – 0xSina

3

Sì, ho avuto anche lo stesso problema. Prova basta stampare qualche campo di questa tabella, nell'esempio:

for(Category *category in fetchedObjects) 
    { 
     for(Cards *card in category.cards) 
     { 
      NSLog(@"Question %@", card.question); 
      NSLog(@"Card %@", card); 
     } 
    } 
8

Per vedi tutti gli oggetti di una "a-molti" rapporto, si può chiamare il metodo allObjects della relazione NSSet.

Nel vostro caso specifico, il codice per stampare tutte le Unità del primo dell'Ordine in fetchedObjects sarebbe come questo:

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

In Swift sarebbe andata in questo modo:

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

Questo è molto contro intuitivo. Ho graffiato la mia testa per quasi un giorno intero fino a quando mi sono reso conto che non riesco a stampare l'intero oggetto NSSet o ogni NSManagedObject nel set in questo modo:

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

Questo genererà colpa in Xcode .. Ma se stampo fuori ogni attributo in ogni NSMangedObject, va bene. Immagino che Apple abbia problemi di memoria per il caricamento automatico su molti oggetti. In uno scenario in cui esiste una catena di relazioni to-many, consumerà molta memoria. Quindi è necessario caricare pigro ogni attributo.

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

che stamperà il nome dell'unità ..

Problemi correlati