gpt4 book ai didi

ios - 核心数据 : The model used to open the store is incompatible with the one used to create the store

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:13 25 4
gpt4 key购买 nike

我的应用包含 2 个数据库:

  • db1:一个读/写数据库(用于存储所有用户设置)
  • db2:只读数据库,预加载在另一个项目中(我在项目中复制了 .sqlite、.xcdatamodeld 和实体类)

如果我用 2 个 MOC 和 2 个 PSC(每个数据库一个)初始化核心数据:一切正常。但我只想为这两个数据库初始化 1 个 MOC/PSC。为此,我编写了以下代码:

- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *db1ModelURL = [[NSBundle mainBundle] URLForResource:@"db1" withExtension:@"momd"];
NSManagedObjectModel *db1Mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:db1ModelURL];
NSURL *db2ModelURL = [[NSBundle mainBundle] URLForResource:@"db2" withExtension:@"momd"];
NSManagedObjectModel *db2Mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:db2ModelURL];
NSAssert(db1 != nil, @"Error initializing Managed Object Model");
NSAssert(db2 != nil, @"Error initializing Managed Object Model");

_managedObjectModel=[NSManagedObjectModel modelByMergingModels:[NSArray db1Mom,db2Mom, nil]];

return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}

NSURL * db1URL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"db1.sqlite"];

NSURL *db2URL = [[NSBundle mainBundle] URLForResource:@"db2" withExtension:@"sqlite"];

NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

_persistentStoreCoordinator = [[self managedObjectContext] persistentStoreCoordinator];
NSMutableDictionary * db2Options=[NSMutableDictionary dictionaryWithObjectsAndKeys:
@YES,NSReadOnlyPersistentStoreOption,
nil];
NSPersistentStore *store = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"DB2" URL:db2URL options:db2Options error:&error];
NSAssert(store != nil, @"Error initializing PSC: %@\n%@", [error localizedDescription], [error userInfo]);

NSMutableDictionary * db1Options=[NSMutableDictionary dictionaryWithObjectsAndKeys:
@YES,NSMigratePersistentStoresAutomaticallyOption,
@YES,NSInferMappingModelAutomaticallyOption,
nil];
store = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"DB1" URL:db1URL options:db1Options error:&error];
NSAssert(store != nil, @"Error initializing PSC: %@\n%@", [error localizedDescription], [error userInfo]);

return _persistentStoreCoordinator;
}

当我启动该应用程序时,我在 DB2 数据库中收到以下错误:

The model used to open the store is incompatible with the one used to create the store

我认为问题来自对 modelByMergingModels 的调用,生成的模型包含 db2Model,但 Core Data 不将其识别为该数据库的基本模型...

建议?

最佳答案

发生这种情况是因为您最初使用其中一个数据库在手机中安装了该应用程序。现在您已经添加/更改了数据库。所以它不承认新的数据库。因为该应用程序最初是使用不同的数据库创建/安装的。

尝试从您的手机中删除该应用程序,然后重新安装。错误将消失。

希望这对您有所帮助。 :)

关于ios - 核心数据 : The model used to open the store is incompatible with the one used to create the store,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37323126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com