How to NSKeyedUnarchive subclasses of NSManagedObject to import into Core Data?

人盡茶涼 提交于 2019-12-11 09:17:19

问题


An existing document based application that saves and loads it's data using NSKeyedArchiver/NSKeyedUnarchiver is currently transformed to use Core Data as it's storage backend.

Now I am trying to have a kind of import of files saved using the former application. The object structure didn't change, but I changed all objects to be NSManagedObjects. Now the initWithCoder: calls [super initWithCoder:] and doesn't create the object using NSEntityDescription. That failes epically (as expected).

My question is: what is the best practice / a good advice to have such an import functionality?


回答1:


The correct way would be to implement a subclass of NSAtomicStore that unarchives the old data format into a Core Data representation. After loading the old data using that store, you can easily migrate the data using -[NSPersistentStoreCoordinator migratePersistentStore:toURL:options:withType:NSSQLiteStoreType error:]

OLD (WRONG) ANSWER:

After fiddeling together my own solution (with some banging-head-on-desk involved) I found the same solution described by The Mental Faculty:

KEYED-ARCHIVING TO CORE DATA MIGRATION

I did it the same way they do, but I created my own subclass of NSKeyedValueDecoder that stores the context. The initWithCoder: of my CBManagedObject superclass to all entities looks like this:

- (id) initWithCoder:(NSCoder *)inCoder {
    NSManagedObjectContext *context = ((CBNSKeyedUnarchiverCoreData*)inCoder).context;
    self = [super initWithEntity:[NSEntityDescription entityForName:[[self class] entityName] 
                                             inManagedObjectContext:context] insertIntoManagedObjectContext:context];
    if (!self) return nil;
    return self;
}

Each entity gives his entity name with the class method +entityName.

Seems like a good solution as it works very good by now.



来源:https://stackoverflow.com/questions/4776265/how-to-nskeyedunarchive-subclasses-of-nsmanagedobject-to-import-into-core-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!