1

I have a shared core data backing my iPhone app and WatchKit Extension. They both function well independently, but I'm worried about concurrent use.

In the Extension, I have a UITableView that has an array of data. Right now it just grabs that array from the shared core data during -awakeWithContext.

I want to have some sort of communication between the app and extension when a record is created/updated/deleted so that it can be duplicated on the other side immediately (instead of on the next query to Core Data).

This question, How to send data from iphone to watchkit in swift, goes into the iPhone having a handler for when something happens in the Watch, but I'm more concerned about it going the other way. Right now all I can think of is querying core data pretty often to take care of it.

Community
  • 1
  • 1
Tom Prats
  • 7,364
  • 9
  • 47
  • 77

2 Answers2

3

A very common solution and one of the only ways is to use MMWormhole.

MMWormhole uses the CFNotificationCenter to communicate changes instantaneously across the app and the extension, passing information through shared application groups.

Example of passing data through from the MMWormhole GitHub README:

// Sender (Watch Extension)
[self.wormhole passMessageObject:@{@"buttonNumber" : @(1)} identifier:@"button"];

// Receiver (Phone)
[self.wormhole listenForMessageWithIdentifier:@"button" 
  listener:^(id messageObject) {
    self.numberLabel.text = [messageObject[@"buttonNumber"] stringValue];
}];
Schemetrical
  • 5,506
  • 2
  • 26
  • 43
  • Interesting. I noticed that it's just using `CFNotificationCenter`, which I could use directly. Is there any downside to that? – Tom Prats May 08 '15 at 07:38
  • There is no upside either. MMWormhole provides a simple wrapper around CFNotificationCenter, eliminating the need to write [boilerplate code](https://github.com/mutualmobile/MMWormhole/blob/master/Source/MMWormhole.m). Its much better to simply use MMWormhole. – Schemetrical May 08 '15 at 07:40
  • I'm pretty new to iOS development, but to me, learning to use something directly is often more helpful than relying on an external library. Although it does look incredibly simple in your example. So maybe I'll give it a shot – Tom Prats May 08 '15 at 07:44
  • 1
    In this case, I highly highly recommend you to use an external library. There is no purpose rewriting MMWormhole as you likely won't learn anything from it. Focusing on building your application would teach you much more. – Schemetrical May 08 '15 at 07:45
  • 1
    I completely agree with Schemetrical. Using Darwin notifications can be tricky (as I pointed out here: http://stackoverflow.com/questions/28809226/notify-watchkit-app-of-an-update-without-the-watch-app-requesting-it/30030344#30030344) – John May 08 '15 at 07:48
1

There are different ways.

  1. You can use MMWormhole
  2. If you do not like to use a third-party library, you may use Darwin notifications directly. A good tutorial is already available on stackoverflow.
Community
  • 1
  • 1
John
  • 8,468
  • 5
  • 36
  • 61