加载核心数据数据(iOS和swiftUI)

时间:2020-09-12 02:19:34

标签: swift core-data swiftui iphonecoredatarecipes swiftui-environment

我正在开发一个应用程序,我需要保存数据,因此我正在使用Core Data,但不幸的是,不是Core Data专家

当我第一次运行该应用程序时,它可以工作并保存数据(我检查了* .sqlit文件),但是如果重新运行该应用程序,则会出现此错误

.match()

我有一个环境对象

SceneDelegate.swift

    2020-09-12 02:52:02.539123+0100 photoUniq_ios_v3[6106:338281] -[photoUniq_ios_v3.PUWorkspace initWithCoder:]: unrecognized selector sent to instance 0x600000a562e0
    2020-09-12 02:52:02.539730+0100 photoUniq_ios_v3[6106:338281] [error] fault: exception raised during multi-threaded fetch -[photoUniq_ios_v3.PUWorkspace initWithCoder:]: unrecognized selector sent to instance 0x600000a562e0 ((null))
    CoreData: fault: exception raised during multi-threaded fetch -[photoUniq_ios_v3.PUWorkspace initWithCoder:]: unrecognized selector sent to instance 0x600000a562e0 ((null))
    2020-09-12 02:52:02.539958+0100 photoUniq_ios_v3[6106:338281] [error] CoreData: exception raised during multi-threaded fetch -[photoUniq_ios_v3.PUWorkspace initWithCoder:]: unrecognized selector sent to instance 0x600000a562e0 ((null))
    2020-09-12 02:52:02.540290+0100 photoUniq_ios_v3[6106:338281] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLFetchRequestContext: 0x60000174c2a0> , -[photoUniq_ios_v3.PUWorkspace initWithCoder:]: unrecognized selector sent to instance 0x600000a562e0 with userInfo of (null)
    CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLFetchRequestContext: 0x60000174c2a0> , -[photoUniq_ios_v3.PUWorkspace initWithCoder:]: unrecognized selector sent to instance 0x600000a562e0 with userInfo of (null)
    2020-09-12 02:52:02.589331+0100 photoUniq_ios_v3[6106:338281] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[photoUniq_ios_v3.PUWorkspace initWithCoder:]: unrecognized selector sent to instance 0x600000a562e0'
...
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[photoUniq_ios_v3.PUWorkspace initWithCoder:]: unrecognized selector sent to instance 0x60000344d3e0'
terminating with uncaught exception of type NSException

PU.swift

if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView
                                                               .environmentObject(PU(ctx: context))
                                                                .environmentObject(PartialSheetManager())
            
            )

PUWorkpace.swift

class PU: NSManagedObject{
    

    let id = UUID()
    var defaultWorkspace:PUWorkspace{
        self.cd_xWorkspaces!.first!
    }
    

     init(ctx:NSManagedObjectContext?) {
        
        super.init(entity: PUPhotouniq.entity(), insertInto: ctx)

        self.cd_xGallary = PUGallary(ctx: self.managedObjectContext)
        self.cd_xWorkspaces = Array()
        self.cd_xWorkspaces!.add(PUWorkspace(ctx: self.managedObjectContext))
   
    }
}

和一些功能

获取数据

import Foundation
import SwiftUI
import CoreData

class PUWorkspace:NSManagedObject {
        
    let id = UUID()
    @NSManaged var cd_nome:String?
    @NSManaged  var cd_porcessedxFoldres:[PUFolder]?
    @NSManaged var cd_xFoldres:[PUFolder]?
    @NSManaged var cd_originalxFoldres:[PUFolder]?
    
  
    @Published var cd_cover:UIImage = UIImage() //Init
    
    
    var defaultFolder:PUFolder{
        cd_xFoldres!.first ?? PUFolder(ctx: self.managedObjectContext)
    }
    
    init(ctx:NSManagedObjectContext?) {
        super.init(entity: Self.entity(), insertInto: ctx)
        self.cd_cover = UIImage.defaultImage
        self.cd_nome = String.defaultName
        self.cd_xFoldres = []
        self.cd_xFoldres!.add(PUFolder(ctx: self.managedObjectContext))
        self.cd_originalxFoldres = []
        self.cd_porcessedxFoldres = []
    }

   }
    

什么原因会导致错误以及如何解决?

0 个答案:

没有答案