BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

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?

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

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.