简体   繁体   English

SwiftUI 使用关系预览核心数据

[英]SwiftUI Preview Core Data with Relationships

I'm trying to work with Core Data & SwiftUI's previews and think I'm close but haven't been able to get this to work.我正在尝试使用 Core Data 和 SwiftUI 的预览版,并认为我已经接近了,但无法让它发挥作用。

    static var previews: some View {
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        let newGame = Game.init(context: context)
        newGame.gameName = "Testy Game"
        newGame.gameDescription = "Wooo play the thing"
        newGame.goal = Goal.init(goalName: "Try Harder", goalComplete: false, goalOfGame: newGame)
        return GameGoalsDetail(game: newGame).environment(\.managedObjectContext, context)
    }
}

I'm getting a "Ambiguous reference to member 'init(entity:insertInto:)'" on the newGame.Goal = Goal.init... line.我在 newGame.Goal = Goal.init... 行上收到“对成员 'init(entity:insertInto:)' 的不明确引用”。

I'm trying to figure out how to initialize a new Game and then initialize a connected Goal to it right after.我试图弄清楚如何初始化一个新游戏,然后立即初始化一个连接的目标。

My Core Data Game class takes gameName, gameDescription, and a goal as a NSSet?.我的 Core Data Game 类将 gameName、gameDescription 和目标作为 NSSet?。 The Core Data Goal class has goalName, goalComplete, & goalOfGame: Game?. Core Data Goal 类具有goalName、goalComplete 和goalOfGame: Game?。 I think my issue is in connecting the two.我认为我的问题在于将两者联系起来。

Appreciate the help in advance and willing to share more code if requested.提前感谢您的帮助,并愿意在需要时分享更多代码。

If they are one-to-many or many-to-many relationship, you can't simply assign the Goal to the Game.如果它们是一对多或多对多的关系,则不能简单地将目标分配给游戏。 When you use core data with relationships you get free "add" methods with the name of your relationship.当您将核心数据与关系一起使用时,您将获得带有关系名称的免费“添加”方法。 You should be creating the Goal on one line您应该在一行上创建目标

let newGoal = Goal.init(goalName: "Try Harder", goalComplete: false)

and then add the newGoal by calling:然后通过调用添加 newGoal:

newGame.addToGoal(newGoal)

That should take care of any inverse relationship as well.这也应该处理任何反向关系。

The Goal is @NSManagedObject thats why you need to use its init with context. Goal@NSManagedObject这就是为什么你需要使用它的 init 和上下文。 When tou create an entity object, you must place it in some store - context is that store.当你创建一个实体对象时,你必须把它放在某个商店里——上下文就是那个商店。 After that you can cast all the properties you need including links.之后,您可以投射您需要的所有属性,包括链接。

let goal = Goal(context: context)
goal.goalName = "Try Harder"
goal.goalComplete = false
goal.goalOfGame = newGame
return GameGoalsDetail(game: newGame).environment(\.managedObjectContext, context)

if you set reversed links in data model, there is no need to use add-methods generated by CoreData, if you have at least 1 one-to-one link.如果您在数据模型中设置反向链接,则无需使用 CoreData 生成的 add-methods,如果您至少有 1 one-to-one链接。 You just set this link as a property value and CoreData do all the work for you.您只需将此链接设置为属性值,CoreData 就会为您完成所有工作。 goal.goalOfGame is that property - reverced link one-to-one to it parent. goal.goalOfGame是该属性 - 反向链接到它的父one-to-one

If you realy want to use init with all the property you want to cast, just override it in extention to your class like this:如果您真的想将 init 与您要转换的所有属性一起使用,只需将它覆盖到您的类中,如下所示:

extention Goal{
    init(of game: Game, name: String = "unnamed", complete: Bool = false, context: NSManagedObjectContext  
    ){
        super.init(context: context)
        self.goalOfGame = game
        self.goalName  = name
        self.complete = goalComplete 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM