7

पर NSManagedObjectModel में संबंध जोड़ना जब आप कोरडाटा का उपयोग करते हुए एक स्थिर लाइब्रेरी लिखते हैं तो परियोजना में सामान्य .xdatamodeld फ़ाइल सहित एक बड़ी गड़बड़ी होती है क्योंकि आप बस अपने बाध्यकारी संस्करण (.momd) को अपनी बाइनरी में लिंक नहीं कर सकते हैं, तो यह इस तरह पूरे NSManagedObjectModel कोड में बनाने के लिए बेहतर है:प्रोग्रामिंग रूप से बनाए गए NSEntityDescription

NSAttributeDescription *dateAttribute = NSAttributeDescription.new; 

dateAttribute.name = @"timestamp"; 
dateAttribute.attributeType = NSDoubleAttributeType; 
dateAttribute.optional = NO; 
dateAttribute.indexed = YES; 

NSAttributeDescription *payloadAttribute = NSAttributeDescription.new; 

payloadAttribute.name = @"payload"; 
payloadAttribute.attributeType = NSBinaryDataAttributeType; 
payloadAttribute.optional = NO; 
payloadAttribute.indexed = NO; 

NSEntityDescription *entry = NSEntityDescription.new; 

entry.name = entry.managedObjectClassName = NSStringFromClass(MyCustomEntry.class); 
entry.properties = @[dateAttribute, payloadAttribute]; 

NSManagedObjectModel *mom = NSManagedObjectModel.new; 

mom.entities = @[entry]; 

और सब कुछ एकदम सही है ....

लेकिन! रुको, अगर मेरे पास NSManagedObjectModel में एक से अधिक इकाइयां हैं और वे संबंधित हैं (कई, उलटा, और इसी तरह), दुनिया में मैं उन्हें कोड में कैसे जोड़ूंगा, उदाहरण के लिए, उदाहरण के लिए, उस अच्छे एक्सकोड संपादक के बिना , जहां आप कई माउस क्लिक के साथ संबंध बनाते हैं?

उदाहरण

कल्पना कीजिए, हम एक वर्ग MyCustomElement उपरोक्त कोड से MyCustomEntry में के रूप में लगभग एक ही है। अब, here're उनके इंटरफेस अगर मैं संस्थाओं के लिए Xcode पीढ़ी के लिए इस्तेमाल किया है कि वे कैसे दिखेगी:

@interface MyCustomEntry : NSManagedObject 

@property (nonatomic, retain) NSNumber *timestamp; 
@property (nonatomic, retain) NSData *payload; 
@property (nonatomic, retain) MyCustomElement *element; 

@end 

@interface MyCustomElement : NSManagedObject 

@property (nonatomic, retain) NSNumber * timestamp; 
@property (nonatomic, retain) NSString * identifier; 
@property (nonatomic, retain) NSSet *entries; 

@end 

@interface MyCustomElement (CoreDataGeneratedAccessors) 

- (void)addEntriesObject:(MyCustomEntry *)value; 
- (void)removeEntriesObject:(MyCustomEntry *)value; 
- (void)addEntries:(NSSet *)values; 
- (void)removeEntries:(NSSet *)values; 

@end 

क्या NSRelationshipDescription मैं उनके लिए बनाने की जरूरत है और यह कैसे init के लिए?

उत्तर

16

संबंधों का वर्णन NSRelationshipDescription वस्तुओं द्वारा किया गया है। ,> MyCustomElement टू-वन -, - (> MyCustomEntry, से-अनेक MyCustomElement)

  • element (MyCustomEntry निम्नलिखित कोड "MyCustomEntry", "MyCustomElement" रिश्तों

    • entries साथ के लिए दो इकाई वर्णन बनाता है), entries के विपरीत।

    दोनों इकाइयों में केवल एक स्ट्रिंग विशेषता "पहचानकर्ता" (कोड की कुछ पंक्तियों को सहेजने के लिए) है।

    ऑब्जेक्टिव-सी:

    NSEntityDescription *entry = [[NSEntityDescription alloc] init]; 
    [entry setName:@"MyCustomEntry"]; 
    [entry setManagedObjectClassName:@"MyCustomEntry"]; 
    
    NSAttributeDescription *entryIdAttribute = [[NSAttributeDescription alloc] init]; 
    entryIdAttribute.name = @"identifier"; 
    entryIdAttribute.attributeType = NSStringAttributeType; 
    
    NSEntityDescription *element = [[NSEntityDescription alloc] init]; 
    [element setName:@"MyCustomElement"]; 
    [element setManagedObjectClassName:@"MyCustomElement"]; 
    
    NSAttributeDescription *elementIdAttribute = [[NSAttributeDescription alloc] init]; 
    elementIdAttribute.name = @"identifier"; 
    elementIdAttribute.attributeType = NSStringAttributeType; 
    
    // To-many relationship from "Element" to "Entry": 
    NSRelationshipDescription *entriesRelation = [[NSRelationshipDescription alloc] init]; 
    
    // To-one relationship from "Entry" to "Element": 
    NSRelationshipDescription *elementRelation = [[NSRelationshipDescription alloc] init]; 
    
    [entriesRelation setName:@"entries"]; 
    [entriesRelation setDestinationEntity:entry]; 
    [entriesRelation setMinCount:0]; 
    [entriesRelation setMaxCount:0]; // max = 0 for to-many relationship 
    [entriesRelation setDeleteRule:NSCascadeDeleteRule]; 
    [entriesRelation setInverseRelationship:elementRelation]; 
    
    [elementRelation setName:@"element"]; 
    [elementRelation setDestinationEntity:element]; 
    [elementRelation setMinCount:0]; 
    [elementRelation setMaxCount:1]; // max = 1 for to-one relationship 
    [elementRelation setDeleteRule:NSNullifyDeleteRule]; 
    [elementRelation setInverseRelationship:entriesRelation]; 
    
    [entry setProperties:@[entryIdAttribute, elementRelation]]; 
    [element setProperties:@[elementIdAttribute, entriesRelation]]; 
    
    NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] init]; 
    [mom setEntities:@[entry, element]]; 
    

    स्विफ्ट:

    let entry = NSEntityDescription() 
    entry.name = "MyCustomEntry" 
    entry.managedObjectClassName = "MyCustomEntry" 
    
    let entryIdAttribute = NSAttributeDescription() 
    entryIdAttribute.name = "identifier"; 
    entryIdAttribute.attributeType = .StringAttributeType; 
    
    let element = NSEntityDescription() 
    element.name = "MyCustomElement" 
    element.managedObjectClassName = "MyCustomElement" 
    
    let elementIdAttribute = NSAttributeDescription() 
    elementIdAttribute.name = "identifier" 
    elementIdAttribute.attributeType = .StringAttributeType 
    
    // To-many relationship from "Element" to "Entry": 
    let entriesRelation = NSRelationshipDescription() 
    
    // To-one relationship from "Entry" to "Element": 
    let elementRelation = NSRelationshipDescription() 
    
    entriesRelation.name = "entries" 
    entriesRelation.destinationEntity = entry 
    entriesRelation.minCount = 0 
    entriesRelation.maxCount = 0 // max = 0 for to-many relationship 
    entriesRelation.deleteRule = .CascadeDeleteRule 
    entriesRelation.inverseRelationship = elementRelation 
    
    elementRelation.name = "element" 
    elementRelation.destinationEntity = element 
    elementRelation.minCount = 0 
    elementRelation.maxCount = 1 // max = 1 for to-one relationship 
    elementRelation.deleteRule = .NullifyDeleteRule 
    elementRelation.inverseRelationship = entriesRelation 
    
    entry.properties = [entryIdAttribute, elementRelation] 
    element.properties = [elementIdAttribute, entriesRelation] 
    
    let mom = NSManagedObjectModel() 
    mom.entities = [entry, element] 
    

    मैं इस कोड का परीक्षण किया है और यह काम करने लगता है, इसलिए मुझे आशा है कि यह आपके लिए उपयोगी हो जाएगा।

  • +0

    कृपया, उदाहरण के साथ अद्यतन प्रश्न की जांच करें। – shoumikhin

    +0

    @shoumikhin: मैंने अपने उत्तर में नमूना कोड जोड़ा है। –

    +0

    बहुत बढ़िया, धन्यवाद! – shoumikhin