How to share data using Watch Connectivity when working with Core Data
Asked Answered
M

3

12

In my iOS application I use Core Data to store data and a fetch request to create an array of NSManagedObjects to display in a UITableView.

On the Watch OS I check if WCSession is supported and active a session, then send the iOS application a message from the watchOS extension.

When the iOS application receives the message from the watchOS it should send the array of Objects to the watchOS extension to display the data in the WKInterfaceTable, but I am unsure how to do this. Ultimately what I am trying to achieve is;

  • How to share the array of Objects with the watchOS extension?

  • If the user adds/edits/deletes objects in the array on the Watch, how can we update the data on the iPhone ?

  • Also, the iOS application is embedded within a UITabBarController so does it matter which view controller I communicate with?

Watch OS FavouritesInterfaceController

var session : WCSession!

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()

    //Check if session is supported and Activate
    if (WCSession.isSupported()) {
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
}

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)
    // Interface Objects

    //Send Message
    sendmessagetoiphone()   
}

func sendMessageToIphone() {
    if(WCSession.isSupported()){
        session.sendMessage(["b":"goodBye"], replyHandler: nil, errorHandler: nil)
    }
}

IOS Application : FavouritesViewController

var objects = [Objects]()

func loadData() { 

    let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    let request = NSFetchRequest(entityName: "Objects")
    request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
    do {
        try
            self.objects = moc.executeFetchRequest(request) as! [Objects]
        // success ...
    } catch {
        // failure
        print("Fetch failed")
    }
 }

   func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
    //handle received message   
    let value = message["Value"] as? String
    dispatch_async(dispatch_get_main_queue()) {
        self.messageLabel.text = value
    }
    //send a reply
    replyHandler(["Value":"Hello Watch"])
   }
Maneuver answered 18/12, 2015 at 22:51 Comment(0)
T
7
  • How to share the array of Objects with the Watch OS Extension ? Since you are using WatchConnectivity framework, send the array of objects from iPhone using sendMessage method and in your FavoritesInterfaceController implement the func session(session: WCSession, didReceiveMessage method in order to get the response or you can get the array in replyhandler to.

  • If the user adds/edits/deletes objects in the array on the Watch OS how can we update the data on the iPhone ?

    Send the objectId along the with new changes in your sendMessage method from watch to phone, on receiving on phone made the changes in your database save it and send the updated value in your replyHandler so that your watch content will be updated accordingly.

  • Also the iOS application is embedded within a UITabBarController so does it matter which view controller I communicate with ?

    You desired viewController to which you are communicating OR the one that is responsible for doing changes should be alive. If multiple ViewControllers are listening to WCSessionDelegates then when you send any message from watch all of the live controllers will receive that message. You should include some kind of identifier in your sendMessage dictionary so you can know which operation to perform. Like if you want to delete an object then when watch sends a message the identifier will contain delete so that on receiving you can check the identifier value and perform the delete operation.

Tunable answered 22/12, 2015 at 6:52 Comment(2)
It seems I have to have both the iOS application and Watch app open for this method to work. Which defeats the point of quickly accessing data on your watch without pulling out your phone. Am I correct ?Maneuver
If you see the comments about this method in WCSession.h it says If the counterpart app is not running the counterpart app will be launched upon receiving the message (iOS counterpart app only). The message dictionary can only accept the property list types. So even if your counter iOS app is closed and you send a msg from watch to iPhone it will launched the app and recived the message but the reverse is not possible.Tunable
E
3

You can use the replyHandler in the sendMessage to do this. Make sure you implement the reply handler on both Watch and iOS App to get this.

Basically, if you get it right, your reply handler can ensure what your iOS app does in response for a watch app's message.

Also, speaking of your response (of sending an array of objects) - you should send it as a dictionary and fetch it on the watch.

Elegy answered 21/12, 2015 at 15:23 Comment(0)
B
3

First of, this is a really good question. For starters I'd recommend that you watch this session from the WWDC 2015: Session 713 - Introducing Watch Connectivity. This can be found here.

Now to your actual question. There is a great tutorial and Github repo that show you how to communicate Core Data between your Apple Watch app and the container app using App Groups, as this enables you to access all shared content, such as Core Data and even NSUSerdefaults.

You can then find the complete tutorial on how to do this under the following link.

Hope that helps, Julian.

Boaz answered 22/12, 2015 at 5:34 Comment(1)
I am pretty sure that github example doesn't work well with Xcode 7, or better said, WatchSDK 2.Hamburger

© 2022 - 2024 — McMap. All rights reserved.