Search code examples
ioswatchkitwatchos-2watchoswatchconnectivity

Keeping Core Data in sync with multiple watches


I'm writing an iPhone and Watch app. I'm planning on supporting the ability to pair multiple watches to the phone.

The iPhone and Watch app will both read and write to a Core Data datastore, and I'll use WatchConnectivity to keep them in sync (using transferUserInfo:). The user will write/dictate something on one device, and it will appear on the other.

I'm struggling to figure out how to support multiple watches. Given the following scenario:

  • User is using Phone/WatchA
    • Over the course of the day, user adds 10 items
  • End of the day, they switch to WatchB

How will WatchB get in sync with the Phone/WatchA?

  • Will WKSession automatically replay the transferUserInfo calls that were made when WatchA was paired?
  • Do I need to somehow keep track of everything WatchB needs and replay everything myself?
  • Do I just send the entire sqlite database using the transferFile API (that seems a bit much)?

Solution

  • Will WKSession automatically replay the transferUserInfo calls that were made when WatchA was paired?

    No it won't. The data only gets transferred to the paired watch.

    When you switch back to the other watch, you'd have to specifically arrange to update its store.

    Do I need to somehow keep track of everything WatchB needs and replay everything myself?

    In short, yes, if that's the approach that you take.

    One edge case would be if the user replaces an old/broken watch with a new/replacement watch, yet didn't unpair the old one. You wouldn't want to keep track of a growing number of changes for a watch that won't ever be paired again.

    You'd also have to handle the case where the user upgrades their phone, and pairs the existing watches with the new phone. Your device tracking and syncing should continue to work across a different device pair.

    Do I just send the entire sqlite database using the fileTransfer API (that seems a bit much)?

    It really depends on the size of the database, versus the complexity of journaling and syncing data between three or more stores.

    What new features would help me keep my watch up to date?

    If you must maintain multiple stores, you should definitely take advantage of the background refresh task feature in watchOS 3 to keep your watch(es) up-to-date before the user launches the app, so the user won't have to wait for anything to sync.

    This answer might be helpful, even if you aren't using complications.

    What are my other options?

    Apple recommends that you design everything around the different ways of interacting with the devices. A user might just want to glance at the watch for a couple of seconds to review some items, but rely on the phone for more complex tasks.

    In that case, you could maintain a single store on the iPhone, and transfer any needed data from the iPhone to be displayed on the watch. If anything changes, push the updated data back to the phone.

    A "Handoff" approach works best, where the phone and the watch know what the most recent items were, and the user can switch between the phone and the watch during the day.

    Of course, this is contingent on whether the watch must operate independently or not while out-of-range of the phone.