Tob*_*aar 2 entity core-data ios
我想使用核心数据来保存某些实体,比如事件.
因此我使用类DSManagedObject和Event
该类DSManagedObject扩展NSManagedObject并具有所有实体可以使用的一般方法.课程Event延伸DSManagedObject.
以下代码是DSManagedObject.h和.m.这里的相关代码.m只是getContext- 方法.
@interface DSManagedObject : NSManagedObject
+ (NSManagedObjectContext *)getContext;
- (NSArray*)getEntitiesForName:(NSString*)_entityName context:(NSManagedObjectContext*)_context;
- (Event*)getEntityForName:(NSString*)_entityName forEventId:(NSInteger)_eventId context:(NSManagedObjectContext*)_context;
- (bool)deleteEntityForName:(NSString*)_entityName forEventId:(NSInteger)_eventId context:(NSManagedObjectContext*)_context;
@end
@implementation DSManagedObject
+ (NSManagedObjectContext *)getContext {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES],
NSInferMappingModelAutomaticallyOption, nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSURL *storeUrl = [NSURL fileURLWithPath:[basePath stringByAppendingFormat:@"DesertStorm.sqlite"]];
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]];
NSError *error = nil;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
NSLog(@"error loading persistent store..");
[[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:persistentStoreCoordinator];
return context;
}
Run Code Online (Sandbox Code Playgroud)
现在在Event我要调用的类中initWithEntity,然后[Event managedObjectModel] unrecognized selector sent to instance发生错误.什么原因 ?:(
@interface Event : DSManagedObject
@property (assign) NSInteger eventId;
@end
@implementation Event
@dynamic eventId;
- (id)init {
NSEntityDescription *entity = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:[DSManagedObject getContext]];
self = [self initWithEntity:entity insertIntoManagedObjectContext:[DSManagedObject getContext]]; // error occurs
self = [super init];
if (self) {
}
return self;
}
...
@end
Run Code Online (Sandbox Code Playgroud)
我是使用核心数据的新手,所以表示理解;)感谢您的帮助
PS:如果你想知道我为什么要覆盖init-method ...复杂的原因^^
来自Core Data doc:
在典型的Cocoa类中,通常会覆盖指定的初始化程序(通常是init方法).在NSManagedObject的子类中,有三种不同的方法可以自定义初始化 - 通过覆盖initWithEntity:insertIntoManagedObjectContext:,awakeFromInsert或awakeFromFetch.你不应该覆盖init.不鼓励您覆盖initWithEntity:insertIntoManagedObjectContext:由于此方法中所做的状态更改可能无法与undo和redo正确集成.另外两个方法awakeFromInsert和awakeFromFetch允许您区分两种不同的情况:
所以灵魂是覆盖initWithEntity:insertIntoManagedObjectContext:或利用awakeFromInsert或awakeFromFecth.如果你想,ovveride前者由于调用后调用initWithEntity:insertIntoManagedObjectContext:或insertNewObjectForEntityForName:inManagedObjectContext:.
你想实现一个特定的目标吗?
编辑
尝试覆盖initWithEntity:insertIntoManagedObjectContext:而不是init
- (id)initWithEntity:(NSEntityDescription*)entity insertIntoManagedObjectContext:(NSManagedObjectContext*)context
{
self = [super initWithEntity:entity insertIntoManagedObjectContext:context];
if (self != nil) {
// Perform additional initialization.
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
该方法是NSManagedObject的指定初始化程序.您不能仅通过发送init来初始化托管对象.有关NSManagedObject详细信息,请参阅课程