sho*_*hin 9 cocoa core-data objective-c nsmanagedobject nsentitydescription
当你编写一个使用CoreData的静态库时,包含一个普通的.xdatamodeld文件会进入项目,因为你不能只将它的编译版本(.momd)链接到你的二进制文件中,所以最好NSManagedObjectModel
在代码中创建整个这个:
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];
Run Code Online (Sandbox Code Playgroud)
一切都很完美......
但!等等,如果我有多个实体NSManagedObjectModel
并且它们是相关的(多对数,反向等等),那么在世界上我将如何在代码中连接它们,就像上面的例子一样,没有那个漂亮的Xcode编辑器,你通过几次鼠标点击建立关系?
例
想象一下,我们有一个MyCustomElement类,它与上面代码中的MyCustomEntry几乎相同.现在,这是他们的界面,如果我使用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
Run Code Online (Sandbox Code Playgroud)
我需要为他们创建什么NSRelationshipDescription以及如何初始化它?
Mar*_*n R 19
关系由NSRelationshipDescription
对象描述.以下代码为"MyCustomEntry","MyCustomElement"创建了两个关系实体描述
entries
(MyCustomElement - > MyCustomEntry,to-many),element
(MyCustomEntry - > MyCustomElement,to-one),反之entries
.两个实体只有一个字符串属性"identifier"(用于保存一些代码行).
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]];
Run Code Online (Sandbox Code Playgroud)
Swift(现在更新为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]
Run Code Online (Sandbox Code Playgroud)
我已经测试了这段代码,它似乎有效,所以我希望它对你有用.