Xcode核心数据:将现有XML更改为Sqlite(将NSXMLStoreType更改为NSSQLiteStoreType) [英] Xcode Core Data: Change existing XML to Sqlite (NSXMLStoreType to NSSQLiteStoreType)
本文介绍了Xcode核心数据:将现有XML更改为Sqlite(将NSXMLStoreType更改为NSSQLiteStoreType)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的第一个应用程序中,我在持久性存储协调器中使用了NSXMLStoreType.
On my first app I used within my persistant store coordinater a NSXMLStoreType.
[storeCooordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:storeURL options:options error:nil];
现在,我想更改为NSSQLiteStoreType:
Now, I like to change to a NSSQLiteStoreType:
[storeCooordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:nil];
如果我只是更改商店类型,则应用程序将崩溃.那我该怎么办?我可以做一次吗?
The app crashes, if I simply change the store type. So what I have to do? May I have to do once:
- 检查旧商店是否存在,并且
- 如果是,请将其转换为sqlite和
- 随后删除旧的xml存储吗?
我不知道如何将其转换为sqlite.型号是相同的.
I have no idea how to convert it to sqlite. The models are the same.
编辑&答案
我使用此解决方案来迁移数据库(感谢Volker )
I use this solution to migrate once the database (thanks to Volker)
//-> applicationFilesDirectory is the url to the documents directory
NSURL* oldURL = [applicationFilesDirectory URLByAppendingPathComponent:@"DBName1.xml"];
NSURL* newURL = [applicationFilesDirectory URLByAppendingPathComponent:@"DBName2.sqlite"];
NSError *error = nil;
NSFileManager * fileManager = [NSFileManager defaultManager];
//-> if file exists
if ([fileManager fileExistsAtPath:[oldURL path]]) {
NSLog(@"File is here");
NSManagedObjectModel* managedModel = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator* tempCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedModel];
id xmlStore = [tempCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:oldURL options:options error:nil];
[tempCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:newURL options:options error:nil];
if ( ![tempCoordinator migratePersistentStore:xmlStore toURL:newURL options:options withType:NSSQLiteStoreType error:&error] ) {
//-> delete the old file from directory
[fileManager removeItemAtURL:oldURL error:NULL];
}
}
推荐答案
If this should happen automatically, you will need to add the migration at startup.
这篇关于Xcode核心数据:将现有XML更改为Sqlite(将NSXMLStoreType更改为NSSQLiteStoreType)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文