CoreData:注释:未能在路径(Xcode 11->;iOS 12)处加载优化模型

CoreData:注释:未能在路径(Xcode 11->;iOS 12)处加载优化模型,ios,core-data,xcode11,Ios,Core Data,Xcode11,在iOS 12设备上使用Xcode 11(beta 3)构建和运行核心数据项目(使用Xcode 10创建)时,我收到警告: CoreData:注释:无法在路径处加载优化模型 “/var/containers/Bundle/Application/7908B3F7-66BC-4931-A578-6A740CBFB37D/TestOMO.app/TestOMO.momd/TestOMO.omo” 如果 我在iOS 12设备上使用Xcode 10构建并运行它 我在iOS 13设备上使用Xcode 1

在iOS 12设备上使用Xcode 11(beta 3)构建和运行核心数据项目(使用Xcode 10创建)时,我收到警告:

CoreData:注释:无法在路径处加载优化模型 “/var/containers/Bundle/Application/7908B3F7-66BC-4931-A578-6A740CBFB37D/TestOMO.app/TestOMO.momd/TestOMO.omo”

如果

  • 我在iOS 12设备上使用Xcode 10构建并运行它
  • 我在iOS 13设备上使用Xcode 11构建并运行它
我的应用程序似乎工作正常,没有崩溃,所以我不知道该如何认真对待这个警告。尽管如此,我还是更愿意摆脱它

有许多帖子与这个核心数据注释相关,但大多数要么与谷歌地图相关,要么没有得到回复

我创建了一个新项目,以消除与我自己的项目相关的问题的其他原因,并使其易于复制,如下所示:

  • 使用Xcode 10创建新项目(单视图应用程序,使用核心数据)
  • 在AppDelegate.swift中,添加 打印(“psc=(persistentContainer)”) to func应用程序(u,didFinishLaunchingWithOptions:) (只是为了强制惰性var初始化)
  • 在iOS12设备上构建和运行:无问题
  • 在Xcode 11中打开相同的项目,在iOS13设备上构建并运行:无问题
  • 在Xcode 11中,在iOS12设备上构建并运行:您将得到上面引用的警告

  • 清理构建文件夹或删除派生数据没有任何帮助。

    多亏了@ChaitanyaKhurana上面评论中的提示,我才能够解决这个问题

    下面是我实现的swift代码,它取代了原来的单行代码

    let container = NSPersistentContainer(name: "ModelName")
    
    检索模型版本字符串(从.momd包中的.plist文件)需要部分代码,以避免每次有新的模型版本时都必须更新代码

    还请注意,新代码/替代代码仅在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的答案):


    我也有同样的问题。您找到解决方案了吗?@ChaitanyaKhurana不幸地没有
    NSURL*modelURL=[[NSBundle mainBundle]URLForResource:@“test”带扩展名:@“momd”]//解决omo问题的小技巧
    modelURL=[modelUrlUrlbyAppendingPathComponent:@“test 24.mom”]我使用了名称“test”而不是真实的应用程序名称。试试这个,它对我有用@同样的问题已经在@ChaitanyaKhurana中解决了,谢谢你的提示。我现在发布了一个全面的答案,如下所示。
    
    - (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;
    }