2011-12-22 9 views
5

में डेटा मॉडल कोर डेटा के साथ बिल्कुल नया, मैं अपना डेटा मॉडल बना रहा हूं। मेरे पास 33 संस्थाएं हैं, और उनके बीच कुछ कठोर संबंध हैं लेकिन विदेशी-महत्वपूर्ण संबंधों के बहुत सारे हैं।कोर डेटा में विदेशी कुंजी संबंधों का प्रतिनिधित्व कैसे करें - एक्सकोड

मैं उन रिश्तों को कैसे प्रबंधित कर सकता हूं जो वास्तव में 1-एकाधिक या 1-1 या कई नहीं हैं लेकिन कोर डेटा मॉडल में विदेशी कुंजी हैं?

पूर्व में, मेरे पास एक संपर्क इकाई है, जिसका संपर्क contact_x_mail से संबंध है, और साथ ही contact_x_mail का मेल मेल के साथ संबंध है, जिसमें सभी ईमेल शामिल हैं। यह संबंध 1-बहुत से या कई हैं। लेकिन इंस्टीट्यूशन जैसे अन्य लोग हैं (एक संपर्क में कई संस्थान हो सकते हैं) और मेल, यह 1-बहुत या 1-1 रिश्ते नहीं है, संस्थान के पास विदेशीकी_मेल_आईडी है।

मैं उस विदेशी कुंजी संबंधों का प्रतिनिधित्व कैसे कर सकता हूं? इंडेक्स?

आपको बहुत धन्यवाद, उम्मीद है कि मेरा प्रश्न स्पष्ट है।

+0

मुझे यकीन है कि मैं वास्तव में समझ में नहीं हूँ। यदि कोई रिश्ते 1-एम, एम-एम, या 1-1 नहीं है तो यह क्या है? – paulbailey

उत्तर

8

आप डीबीएमएस के संदर्भ में कोरडाटा के बारे में सोच रहे हैं जो यह नहीं है। CoreData में संबंध बनाने के लिए आपको विदेशी कुंजी सेट अप करने की आवश्यकता नहीं है। यदि आप किसी उपयोगकर्ता को ईमेल असाइन करना चाहते हैं तो आप दोनों के बीच एक रिश्ता बनाते हैं और आप किसी उपयोगकर्ता की विशेषता "ईमेल" या ईमेल के "उपयोगकर्ता" विशेषता सेट कर सकते हैं। विदेशीकी और लिंकिंग पृष्ठभूमि में कोरडाटा द्वारा किया जाता है।

एक और बिंदु पर, प्रत्येक संबंध परिभाषा, 1-1, 1- *, या - द्वारा है। मुझे यकीन नहीं है कि कोई अन्य विकल्प है ...

जब आप कोरडाटा में संबंध बनाते हैं तो आप प्रभावी रूप से इस आइटम के लिए नए विशेषताओं को बना रहे हैं।

@interface User : NSManagedObject 

#pragma mark - Attributes 
@property (nonatomic, strong) NSString *name; 
@property (nonatomic, strong) NSString *emailAddress; 

#pragma mark - Relationships 
//All to-many relationships are saved as Sets. You can add to the "emails" relationship attribute to add email objects 
@property (nonatomic, strong) NSSet  *emails; 
//All to-one relationships are saved as types of NSManagedObject or the subclass; in this case "Institution" 
@property (nonatomic, strong) Institution *institution; 

स्थापना के रूप में इन के रूप में सरल है::

User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:[self.fetchedResultsController managedObjectContext]]; 
[user setName:@"Matt"]; 
[user setEmailAddress:@"[email protected]"]; 

//...Maybe i need to query my institution 
NSFetchRequest *query = [[NSFetchRequest alloc] initWithEntityName:@"Institution"]; 
    [bcQuery setPredicate:[NSPredicate predicateWithFormat:@"id == %@",  institutionId]]; 
    NSArray *queryResults = [context executeFetchRequest:query error:&error]; 
[user setInstitution:[queryResults objectForId:0]]; 

//Now the user adds a email so i create it like the User one, I add the proper 
//attributes and to set it to the user i can actually set either end of the 
//relationship 
Email *email = ... 
[email setUser:user]; 

//Here i set the user to the email so the email is now in the user's set of emails 
//I could also go the other way and add the email to the set of user instead. 

आशा यह स्पष्ट बातें मदद करता है एक सा यहाँ एक उदाहरण है! यह सुनिश्चित करने के लिए प्रलेखन पर पढ़ें कि कोरडाटा आपके लिए सही है!

http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/CoreData.pdf

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