Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:57:03+00:00 2026-06-15T14:57:03+00:00

When you write a static library which uses CoreData there’s a big mess including

  • 0

When you write a static library which uses CoreData there’s a big mess including a normal .xdatamodeld file into the project because you simply cannot just link its compiled version (.momd) into your binary, so it’s better to create the whole NSManagedObjectModel in the code like this:

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

And everything is just perfect….

But! Wait, if I have more than one entity in my NSManagedObjectModel and they are related (to-many, inversed, and so on), how in the world I gonna connect them in the code, like in example above, without that nice Xcode editor, where you make relationships with several mouse clicks?

Example

Imagine, we have a class MyCustomElement, which is almost the same as in MyCustomEntry from the code above. Now, here’re their interfaces how they would appear if I used Xcode generation for entities:

@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

What NSRelationshipDescription I need to create for them and how to init it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:57:05+00:00Added an answer on June 15, 2026 at 2:57 pm

    Relationships are described by NSRelationshipDescription objects. The following code creates two entity descriptions for “MyCustomEntry”, “MyCustomElement” with relationships

    • entries (MyCustomElement –> MyCustomEntry, to-many),
    • element (MyCustomEntry –> MyCustomElement, to-one), inverse of entries.

    Both entities have only a string attribute “identifier” (to save some lines of code).

    Objective-c:

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

    Swift (now updated for Swift 3/4):

    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]
    

    I have tested this code and it seems to work, so I hope that it will be useful to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wanting to write a static library which uses RestKit , for an
My application uses one big precompiled static library, and the linking stage takes a
I wrote a program which uses Cryptopp library. I have created the static library
Is it possible to have a C static library API, which uses C++ internally
Should I use <h:outputText value=static text/> or directly write static text into the xhtml
I am using this code to write asynchronously to a file public static void
I'm developing a small haskell program that uses an external static library I've developed
I'm having some problems with a large static library (.lib) file, and am suspecting
I'm attempting to write a small library which will convert Java objects, through reflection,
I have to write a library in python which was written in Java before.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.