UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

5      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot 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.