TLDR: How can I listen to changes in SwiftData
and share the data via WatchConnectivity
?
I am developing an app for the Apple Watch where the iPhone can act as a remote for the Watch.
The App on the Watch should work independently from iPhone. The user can create elements on the Watch and start the action associated with the element. The iPhone user can see the elements created on the Watch and start the action on the Watch from the iPhone (if possible I want to add the functionality to let the user update the elements on both devices, but that's not a priority atm).
I tried to get it to work with CloudKit
and SwiftData
, but somehow I couldn't get CloudKit
to initialize correctly. After further research, I reconned that using WatchConnectivity
and ApplicationContext
should be a suitable approach.
To communicate with the iPhone App, I created a singleton class that manages the communication. Now I want this class to listen to the changes in SwiftData
to send the elements to the iPhone, but I don't know how I can get access to the ModelContext
in the Communicator
class. I read that passing the value from the initializer of a view is not good practice and it didn't work for me.
What would be a suitable way to achieve the desired behavior of my app? Any help and feedback is appreciated. TIA