Load Core Data data (iOS and swiftUI)

179 views Asked by At

I'm developing an app, and I need save data, so im using Core Data, and unfortunately in not a Core Data expert

When I run the app for the first time, it works and saves the data (I checked the *.sqlit file), but if re-run the app give me this erros

    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

I have an environment object

SceneDelegate.swift

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

PU.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))
   
    }
}

PUWorkpace.swift

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 = []
    }

   }
    

and a few functions

to fetch the data

@FetchRequest(entity: PU.entity(), sortDescriptors: []) var photoUniq2:FetchedResults<PU >

what can cause the error and how I can solve it?

0

There are 0 answers