2016-01-25 9 views
6

मैं बहुत की तरह एक डाटा मॉडल बनाया है कई रिश्ते कोर डेटा के लिए एक से एक से डेटा प्राप्त करने में:बचत, हटाने और

func roundFetchRequest() -> NSFetchRequest { 
    let fetchRequest = NSFetchRequest(entityName: "Customer") 
    print("Check here: \(myRoundIndexPath)") 
    //let predicate : NSPredicate = NSPredicate(format: "custRoundRel = %@", frc2.objectAtIndexPath(myRoundIndexPath!) as! RoundName) //ASSUME THIS IS CORRECT 
    let sortDescriptor = NSSortDescriptor(key: "c2fna", ascending: true) 
    //fetchRequest.predicate = predicate 
    fetchRequest.sortDescriptors = [sortDescriptor] 
    return fetchRequest 
} 

मेरे बाहर टिप्पणी की कोड:

मैं एक लाने का अनुरोध करने के लिए इस कोड है कोई त्रुटि नहीं देता है, लेकिन मैं किसी ग्राहक को RoundName उदाहरण में सहेजने के लिए प्रतीत नहीं कर सकता। जब मैं अपने गुणों के साथ एक ग्राहक को बचाता हूं, तो मैंने इस कोड का उपयोग किया है:

func newCust() { 
    let cont = self.context 
    let newCustomer = NSEntityDescription.entityForName("Customer", inManagedObjectContext: cont) 
    let aCust = Customer(entity: newCustomer!, insertIntoManagedObjectContext: cont) 

    aCust.c2fna = firstName.text 
    aCust.c3lna = lastName.text 
    aCust.c4tel = tel.text 
    aCust.c5mob = mob.text 
    aCust.c6ema = email.text 
    aCust.c7hsn = houseNo.text 
    aCust.c8fir = street.text 
    aCust.c9sec = secondLine.text 
    aCust.c10ar = area.text 
    aCust.c11pc = postcode.text 
    aCust.c12cos = cost.text 
    aCust.c13fq = frequencyNumber.text 
    aCust.c14fqt = frequencyType.text 
    let DF = NSDateFormatter() 
    aCust.c15das = DF.dateFromString(startDate.text!) 
    //Do Pics in a minute & next date in a minute 
    aCust.c17notes = notes.text 

    //print("Desc = \(picRound.image?.description)") 

    do { 
     try context.save() 
     print("Save Successful") 
    } catch { 
     print("Save Unsuccessful") 
    } 
} 

इस ग्राहक को सही दौर से जोड़ने का कोड क्या है?

धन्यवाद, मैं कोर डेटा के लिए बहुत नया हूं और वास्तव में किसी भी मदद की सराहना करता हूं।

उत्तर

1

हाँ, आप एक विधेय अपने लाने का अनुरोध पर एक प्रारूप के साथ की तरह

NSPredicate(format:"custRoundRel = %@", xxxx) 

उपयोग करते हैं, जहां xxxxRound उदाहरण है।

आप संबंधों का उपयोग भी कर सकते हैं जो आप Customer उदाहरणों के साथ और कितने हैं के संबंध में करना चाहते हैं।

+0

धन्यवाद @Wain, इसलिए जब मैं एक ग्राहक इकाई को इसके संबंधित गुणों से सहेजता हूं तो मैं इसे सही दौर के नाम से कैसे जोड़ूं? – agf119105

+0

आपको उस 'राउंड' उदाहरण की आवश्यकता है, या तो पहले से ही इसे नाम से लाएं, फिर इसे रिश्ते में सेट करें: 'yyyy.custRoundRel = xxxx' (जहां 'yyyy' आपका नया' ग्राहक' उदाहरण है – Wain

-1

आप Customer ऑब्जेक्ट्स बनाते हैं उसी तरह आप अन्य प्रबंधित ऑब्जेक्ट्स बनाते हैं। किसी ग्राहक को सही Round ऑब्जेक्ट से लिंक करने के लिए, केवल एक-एक रिश्ते को सेट करें (कोर डेटा स्वचालित रूप से आपके लिए रिवर्स रिलेशनशिप सेट करेगा)।

newCustomer.round = round 
// or, with your arcane attribute names 
newCustomer.custRoundRel = theDesiredRoundObject 

एक विशिष्ट दौर के ग्राहकों को प्राप्त करने के लिए, आपको अनुरोध अनुरोध या पूर्वानुमान की आवश्यकता नहीं है।

round.customers 
// or, with your arcane attribute names 
round.roundCustRel 
संबंधित मुद्दे