NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to have different userDataInfo?

Forums > watchOS

I have a watch app that will transfer a small amount of data to phone and which then save in a entity(CheckData) in CoreData. This is the code I have which I think will work! (have not tested yet)

func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
    let viewContext = dataController.container.viewContext
    let checkData = CheckData(context: viewContext)

    if let bonnet = userInfo["bonnet"] as? String {
        checkData.bonnet = bonnet
    }

    if let numberDefect = userInfo["numberDefect"] as? Int {
        checkData.numberDefect = Int64(numberDefect)
    }

    checkData.dateCreated = Date()

    dataController.save()
}

However the thing is that I what to have is the didReceiveUserInfo would be able to put data in a different enitity (Defect). How would this be possible as the depends on the user on which form they fill in?

The only solution that I can think is to send a boolean over eg

if let isDefect = userInfo["isDefect"] as? Bool {
    if isDefect {
        let defect = Defect(context: viewContext)
        // rest of save code
    } else {
        let checkData = CheckData(context: viewContext)
        // rest of save code
    }
}

This does not feel right. Does if anyone has a better idea?

2      

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.