2012-01-16 13 views
40

मेरे पास एक ऑर्डर है जिसमें इकाइयों के साथ "से-कई" संबंध हैं। जब मैं आदेश में इकाइयों (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'>";

साथ ही, जब मैं केवल इकाइयों को चाहता हूं तो यह पूरी वस्तु क्यों प्रिंट कर रहा है?

उत्तर

80

यह कोई त्रुटि नहीं है - यह कोर डेटा की एक विशेषता है जिसे 'दोषपूर्ण' कहा जाता है। यहाँ एप्पल के 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. 
    } 
}  
+2

उत्तर के लिए धन्यवाद, लेकिन यह काम नहीं करता। मैंने पहले यह कोशिश की। – 0xSina

+0

जब आप इसे इस तरह करते हैं तो यह लॉग में आपको क्या दिखाता है? – sosborn

+0

कुछ भी नहीं। यह दूसरे लूप के माध्यम से पुनरावृत्ति नहीं करता है। मैं गिनती की जांच करता हूं और यह शून्य लौटाता है। – 0xSina

3

हाँ, मैं भी यही समस्या थी। इस तालिका में से कुछ क्षेत्र प्रिंट, उदाहरण में प्रयास करें बस:

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

आदेश में करने के लिए "करने के लिए कई" एक के सभी वस्तुओं को देखने संबंध, आप इसे allObjects विधि के साथ कॉल कर सकते हैं रिश्ते के NSSet

अपने विशिष्ट मामले में, कोड fetchedObjects में सभी इकाइयों पहले आदेश की मुद्रित करने के लिए इस तरह होगा:

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

स्विफ्ट में यह इतना की तरह जाना होगा:

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

यह बहुत काउंटर सहज है। मैं एक पूरे दिन के बारे में के लिए मेरे सिर खरोंच तक मुझे एहसास हुआ कि मैं पूरी 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); 
    } 
} 

बाहर इकाई नाम प्रिंट होगा कि ..

संबंधित मुद्दे