2012-07-09 49 views
1

我想使用核心數據來堅持一些實體,如事件。核心數據 - initWithEntity導致無法識別的選擇器發送到實例

所以我使用的類DSManagedObjectEvent

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; 


} 

現在在班上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 

我使用的核心數據,因此表現出理解是新) 感謝您的幫助

PS:如果你想知道爲什麼我重寫init - 方法......複雜的原因^^

回答

0

我解決了這個問題,在init-方法。 我還是覆蓋的方法(我知道我不應該,但是...無論如何)

下面的代碼

- (id)init { 
    return [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:[DSManagedObject getContext]]; 
} 

這將返回一個NSManagedObject,並沒有出現錯誤:)

5

Core Data DOC:

In a typical Cocoa class, you usually override the designated initializer (often the init method). In a subclass of NSManagedObject, there are three different ways you can customize initialization —by overriding initWithEntity:insertIntoManagedObjectContext:, awakeFromInsert, or awakeFromFetch. You should not override init. You are discouraged from overriding initWithEntity:insertIntoManagedObjectContext: as state changes made in this method may not be properly integrated with undo and redo. The two other methods, awakeFromInsert and awakeFromFetch, allow you to differentiate between two different situations:

所以soultions是覆蓋initWithEntity:insertIntoManagedObjectContext:○ r利用awakeFromInsertawakeFromFecth。如果你想,ovveride前,因爲你調用或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;  
} 

的方法是NSManagedObject指定初始化。您不能簡單地通過發送init來初始化管理對象。請參閱NSManagedObject class for further info。

+0

它帶我到目標;)你知道爲什麼錯誤'CoreData:error:無法調用NSManagedObject類'Event'上的指定初始值設定項''時,我不會調用'initWithEntity'?是否必須調用'initWithEntity'? – 2012-07-09 11:21:46

+0

@TobiWeißhaar我添加了一個編輯... – 2012-07-09 12:05:11

+0

感謝您的幫助。它幫助我更多地瞭解核心數據。我解決了這個問題(查看我的回答) – 2012-07-09 12:49:31

相關問題