GO FURTHER, FASTER: Try the Swift Career Accelerator 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?

5      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.

Learn more here

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.