使用SharedData在iPhone和Watch之间传递更新 [英] Communicating Updates between iPhone and Watch with SharedData

查看:166
本文介绍了使用SharedData在iPhone和Watch之间传递更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个共享的核心数据支持我的iPhone应用程序和WatchKit扩展。

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.

在扩展中,我有一个 UITableView 具有数据数组。现在,在 -awakeWithContext 期间,它只是从共享核心数据中获取该数组。

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.

创建/更新/删除记录时,应用程序和扩展程序之间的通信可以立即复制(而不是下一次查询Core Data)。

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).

此问题,如何从中发送数据iphone to watchkit swift ,进入iPhone有一个处理程序,当一些事情发生在手表,但我更关心它的另一种方式。

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.

推荐答案

一个非常常见的解决方案和一个非常常见的解决方案的唯一方法是使用 MMWormhole

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

MMWormhole使用CFNotificationCenter通过应用程序和扩展程序即时传递更改,通过共享应用程序组传递信息。

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

通过 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];
}];

这篇关于使用SharedData在iPhone和Watch之间传递更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆