CoreData:注释:无法在路径加载优化模型(Xcode 11 -> iOS 12)
CoreData: annotation: Failed to load optimized model at path (Xcode 11 -> iOS 12)
当在 iOS 12 设备上使用 Xcode 11 (beta 3) 构建和 运行ning 核心数据项目(使用 Xcode 10 创建)时,我收到警告:
CoreData: annotation: Failed to load optimized model at path
'/var/containers/Bundle/Application/7908B3F7-66BC-4931-A578-6A740CBFB37D/TestOMO.app/TestOMO.momd/TestOMO.omo'
没有警告如果
- 我在 iOS 12 设备
上使用 Xcode 10 构建并 运行 它
- 我在 iOS 13 设备上使用 Xcode 11 构建并 运行 它
我的应用程序似乎运行良好并且没有崩溃,所以我不确定是否认真对待此警告。尽管如此,我肯定更愿意摆脱它。
有很多帖子与此 Core Data 注释相关,但大多数帖子要么与 Google 地图相关,要么无人回答。
我创建了一个新项目,以消除与我自己的项目相关的问题的其他原因,并使其易于重现,如下所示:
- 使用 Xcode10(单视图应用,使用核心数据)创建一个新项目
- 在AppDelegate.swift中添加
打印("psc = (persistentContainer)")
到 func application(_, didFinishLaunchingWithOptions:)
(只是为了强制惰性变量初始化)
- 在 iOS12 设备上构建和 运行:没有问题
- 在 Xcode11 中打开同一个项目,在 iOS13 设备上构建和 运行:没有问题
- Sill in Xcode 11,在 iOS12 设备上构建和 运行:您将收到上面引用的警告
清理构建文件夹或删除派生数据没有帮助。
由于@ChaitanyaKhurana 上面的评论中的提示,我能够解决问题。
这里是我实现的swift代码,替换了原来的单行
let container = NSPersistentContainer(name: "ModelName")
需要部分代码来检索模型版本字符串(从位于 .momd 包中的 .plist 文件)以避免每次有新模型版本时都必须更新代码。
另请注意,new/alternative 代码仅在 13.0 之前的 iOS 版本上执行,因为在 iOS 13.
上没有问题
let modelName = "ModelName"
var container: NSPersistentContainer!
if #available(iOS 13.0, *) {
container = NSPersistentContainer(name: modelName)
} else {
var modelURL = Bundle(for: type(of: self)).url(forResource: modelName, withExtension: "momd")!
let versionInfoURL = modelURL.appendingPathComponent("VersionInfo.plist")
if let versionInfoNSDictionary = NSDictionary(contentsOf: versionInfoURL),
let version = versionInfoNSDictionary.object(forKey: "NSManagedObjectModel_CurrentVersionName") as? String {
modelURL.appendPathComponent("\(version).mom")
let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL)
container = NSPersistentContainer(name: modelName, managedObjectModel: managedObjectModel!)
} else {
//fall back solution; runs fine despite "Failed to load optimized model" warning
container = NSPersistentContainer(name: modelName)
}
}
由于旧代码通常可以在 Objective C 中,这里有一个 ObjC 版本(这是来自@Lobo 的答案):
- (NSPersistentContainer *)samplePersistentContainer {
// The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
@synchronized (self) {
if (_persistentContainer == nil) {
NSString *modelName = @"ModelName";
if (@available(iOS 13, *)) {
_persistentContainer = [NSPersistentContainer persistentContainerWithName: modelName];
[_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
if (error != nil) {
NSLog(@"Unresolved error %@, %@", error, error.userInfo);
abort();
}
}];
} else {
NSString *modelURLPath = [[NSBundle mainBundle] pathForResource: modelName ofType: @"momd"];
NSURL *modelURL = [NSURL fileURLWithPath: modelURLPath];
NSURL *versionInfoURL = [modelURL URLByAppendingPathComponent: @"VersionInfo.plist"];
NSDictionary *versionInfoNSDictionary = [NSDictionary dictionaryWithContentsOfURL: versionInfoURL];
NSString *version = versionInfoNSDictionary[@"NSManagedObjectModel_CurrentVersionName"];
modelURL = [modelURL URLByAppendingPathComponent:[NSString stringWithFormat: @"%@.mom", version]];
NSManagedObjectModel *mod = [[NSManagedObjectModel alloc] initWithContentsOfURL: modelURL];
_persistentContainer = [NSPersistentContainer persistentContainerWithName: modelName managedObjectModel: mod];
}
}
}
return _persistentContainer;
}
当在 iOS 12 设备上使用 Xcode 11 (beta 3) 构建和 运行ning 核心数据项目(使用 Xcode 10 创建)时,我收到警告:
CoreData: annotation: Failed to load optimized model at path '/var/containers/Bundle/Application/7908B3F7-66BC-4931-A578-6A740CBFB37D/TestOMO.app/TestOMO.momd/TestOMO.omo'
没有警告如果
- 我在 iOS 12 设备 上使用 Xcode 10 构建并 运行 它
- 我在 iOS 13 设备上使用 Xcode 11 构建并 运行 它
我的应用程序似乎运行良好并且没有崩溃,所以我不确定是否认真对待此警告。尽管如此,我肯定更愿意摆脱它。
有很多帖子与此 Core Data 注释相关,但大多数帖子要么与 Google 地图相关,要么无人回答。
我创建了一个新项目,以消除与我自己的项目相关的问题的其他原因,并使其易于重现,如下所示:
- 使用 Xcode10(单视图应用,使用核心数据)创建一个新项目
- 在AppDelegate.swift中添加 打印("psc = (persistentContainer)") 到 func application(_, didFinishLaunchingWithOptions:) (只是为了强制惰性变量初始化)
- 在 iOS12 设备上构建和 运行:没有问题
- 在 Xcode11 中打开同一个项目,在 iOS13 设备上构建和 运行:没有问题
- Sill in Xcode 11,在 iOS12 设备上构建和 运行:您将收到上面引用的警告
清理构建文件夹或删除派生数据没有帮助。
由于@ChaitanyaKhurana 上面的评论中的提示,我能够解决问题。
这里是我实现的swift代码,替换了原来的单行
let container = NSPersistentContainer(name: "ModelName")
需要部分代码来检索模型版本字符串(从位于 .momd 包中的 .plist 文件)以避免每次有新模型版本时都必须更新代码。
另请注意,new/alternative 代码仅在 13.0 之前的 iOS 版本上执行,因为在 iOS 13.
上没有问题let modelName = "ModelName"
var container: NSPersistentContainer!
if #available(iOS 13.0, *) {
container = NSPersistentContainer(name: modelName)
} else {
var modelURL = Bundle(for: type(of: self)).url(forResource: modelName, withExtension: "momd")!
let versionInfoURL = modelURL.appendingPathComponent("VersionInfo.plist")
if let versionInfoNSDictionary = NSDictionary(contentsOf: versionInfoURL),
let version = versionInfoNSDictionary.object(forKey: "NSManagedObjectModel_CurrentVersionName") as? String {
modelURL.appendPathComponent("\(version).mom")
let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL)
container = NSPersistentContainer(name: modelName, managedObjectModel: managedObjectModel!)
} else {
//fall back solution; runs fine despite "Failed to load optimized model" warning
container = NSPersistentContainer(name: modelName)
}
}
由于旧代码通常可以在 Objective C 中,这里有一个 ObjC 版本(这是来自@Lobo 的答案):
- (NSPersistentContainer *)samplePersistentContainer {
// The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
@synchronized (self) {
if (_persistentContainer == nil) {
NSString *modelName = @"ModelName";
if (@available(iOS 13, *)) {
_persistentContainer = [NSPersistentContainer persistentContainerWithName: modelName];
[_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
if (error != nil) {
NSLog(@"Unresolved error %@, %@", error, error.userInfo);
abort();
}
}];
} else {
NSString *modelURLPath = [[NSBundle mainBundle] pathForResource: modelName ofType: @"momd"];
NSURL *modelURL = [NSURL fileURLWithPath: modelURLPath];
NSURL *versionInfoURL = [modelURL URLByAppendingPathComponent: @"VersionInfo.plist"];
NSDictionary *versionInfoNSDictionary = [NSDictionary dictionaryWithContentsOfURL: versionInfoURL];
NSString *version = versionInfoNSDictionary[@"NSManagedObjectModel_CurrentVersionName"];
modelURL = [modelURL URLByAppendingPathComponent:[NSString stringWithFormat: @"%@.mom", version]];
NSManagedObjectModel *mod = [[NSManagedObjectModel alloc] initWithContentsOfURL: modelURL];
_persistentContainer = [NSPersistentContainer persistentContainerWithName: modelName managedObjectModel: mod];
}
}
}
return _persistentContainer;
}