SwiftUI Preview crashes with Core Data 'NSInvalidArgumentException'

0

I have the problem that I can't preview the View of my app with Canvas, because I always get the error:

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An NSManagedObject of class 'App.MPG_A' must have a valid NSEntityDescription."

Now more detailed: In the view I want to preview I have a @Binding of the abstract class MultiplayerGame (MPG A or MPG B (subclasses) and the MultiplayerGame is a subclass of Game (picture below)

App Model

Comment: MultiplayerGame is also an abstract entity (like Game) and "Class" -> "MPG B"

And when I want to preview the class the following class by making a moc Object in the Preview my App crashes but I don't know why:

    import SwiftUI
import CoreData
import Combine

struct StandingEditView: View {
    
    //multiplayer game with MPG A or MPG B class
    @Binding var game : MultiplayerGame
    
    @State private var invokeFunction : Bool = false
    
    var body: some View {
        
        VStack{
            
            List{
                
                ForEach(game.players!, id: \.self){ player in
                    
                    HStack{
                        Text("\(player.name)")
                    }
                    
                }
                
            }
            
            Button(action: {
                invokeFunction.toggle()
            }, label: {
                Text("Button")
            })
            
        }
        
    }
}

struct StandingEditView_Previews: PreviewProvider {
    
    static let moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    
    static var previews: some View {
        
        let game : MultiplayerGame = Standing(context: moc)
        
        let p1 : Player = Player(context: moc)
        let p2 : Player = Player(context: moc)
        let p3 : Player = Player(context: moc)
        
        p1.name =  "Player One"
        p2.name =  "Player Two"
        p3.name =  "Player Three"
        
        game.players = [p1,p2,p3]
        
        return StandingEditView(game: .constant(game))
    }
}

And the only (not default) attributes of Player is -> var name : String

I also tried with AppDelegate but that didn't work either...

The error must occur because of the Preview and moc-Object because the Code itself compiles and the other Views are visible without an error.

Thank you for your help!

core-data preview swift swiftui
2021-11-24 01:22:48
2
0

You will need to setup a stack for your context. The NSManagedObjectContext must be aware of your model.

The following StackBuilder class offers a temporary version suitable for previews.

private final class StackBuilder {
    
    private let modelName = "Model" // must match your model filename
    
    public enum Configuration {
        case temporary
        case permanent
    }
    
    let configuration: Configuration
    
    public init(_ configuration: Configuration = .permanent) {
        self.configuration = configuration
    }
    
    public func load() throws -> NSPersistentContainer {
        var errors = [Error]()
        let persistentContainer = NSPersistentContainer(name: modelName)
        persistentContainer.persistentStoreDescriptions = [description(for: configuration)]
        persistentContainer.loadPersistentStores { (value, error) in
            if error != nil {
                errors.append(error!)
            }
        }
        if errors.count > 0 {
            throw errors.first!
        }
        return persistentContainer as NSPersistentContainer
    }
    
    private func description(for configuration: Configuration) -> NSPersistentStoreDescription {
        let desc = NSPersistentStoreDescription(url: NSPersistentContainer.defaultDirectoryURL())
        switch configuration {
        case .temporary:
            desc.type = NSInMemoryStoreType
        case .permanent:
            desc.shouldInferMappingModelAutomatically = true
            desc.shouldMigrateStoreAutomatically = true
            desc.type = NSSQLiteStoreType
        }
        return desc
    }
}

It may be necessary to pass along the context as an environment object.

static var previews: some View {
    return StandingEditView(game: .constant(game))
            .environment(\.managedObjectContext, moc)
}
2021-11-24 06:05:30

Do I have to call the StackBuilder class anywhere? The same error "NSInvalidArgumentException" appears - but the project is build correctly and the other Views are visible
ProgrammingAverage

and do I have to do that only for the model name like "MPG A" or also "MPG A+CoreDataClass" and Property?
ProgrammingAverage

0

Since I have tried all other possibilities and nothing has helped, I have created a new project and checked CoreData.

Then I created the main class in the CoreData file and replaced Item with Game in "ContentView".

Also in the PersistenceController I replaced the Item with Game under the var preview : PersistenceController and created all classes with codegen "Manual/None".

Now everything works.

2021-11-29 07:01:36

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................