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

Saving UserInfo to CoreData

Forums > watchOS

Hi I was wonder if anyone can help. I have watch app that does a check then passes two bit data to main app (a String and a Int). Then I want it to save them to CoreData. I have tried a few things. The Comment out code is want I need to save them to CoreData.

#if os(iOS)
// With This get *SwiftUI:0: Fatal error: No ObservableObject of type DataController found. A View.environmentObject(_:) for DataController may be missing as an ancestor of this view.*
@EnvironmentObject var dataController: DataController

// This does saved to CoreData but then crashes the iOS app!
//    @ObservedObject var dataController = DataController()

func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
    DispatchQueue.main.async {

    print("Data received \(userInfo)")
//            let viewContext = dataController.container.viewContext

    if let isDefect = userInfo["isDefect"] as? Bool {
        if isDefect {
// This is another data that save to different entity
//                let defect = Defect(context: viewContext)
        } else {
            print("Vehicle check")
//                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()


// other Protocol methods that are required
// watch Protocol method


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.