如何通过多个词典/多个 'application updates' 和 swift 发送 2.2 手表连接

how to send through multiple dictionaries/ multiple 'application updates' with swift 2.2 watch connectivity

如何使用 Watch Connectivity 将多个 'application updates' 从我的 phone 发送到我的手表(例如数组中的几个不同值)?

我的应用程序更新成功地通过我的 table 视图中选定单元格的 numberItem 值发送,但我还想通过选定单元格数组中的用户 ID 值发送。

现在,它只能识别一个值,不会更新另一个值,但会显示 'please retry' 作为我的标签文本。

如何通过两个或多个应用程序更新发送其他附加值(例如用户 ID 和用户名)。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let numberItem = number[indexPath.row]
    print("tableview select #:")
    print(numberItem)
    do {
        try WatchSessionManager.sharedManager.updateApplicationContext(["number" : numberItem])
    } catch {
        let alertController = UIAlertController(title: "Oops!", message: "Looks like your \(numberItem) got stuck on the way! Please send again!", preferredStyle: .Alert)
        presentViewController(alertController, animated: true, completion: nil)
    }
    let uidItem = 15
    //let uidDict = ["uidValue":uidItem]
    print("the send UID is")
    //print(uidItem)
    do {
        try WatchSessionManager.sharedManager.updateApplicationContext(["uidValue" : uidItem])
    } catch {
        let alertController = UIAlertController(title: "Oops!", message: "Looks like your \(uidItem) got stuck on the way! Please send again!", preferredStyle: .Alert)
        presentViewController(alertController, animated: true, completion: nil)
    }
}

我的 datasource.swift 文件是:

    enum Item {
    case Number(String)
    case uidValue(String)
    case Unknown
}

init(data: [String : AnyObject]) {
    if let numberItem = data["number"] as? String {
        item = Item.Number(numberItem)
        print("enum item is")
        print(numberItem)
    } else if let uidItem = data["uidValue"] as? String {
        item = Item.uidValue(uidItem)
        print("enum item is")
        print(uidItem)
    } else {
        item = Item.Unknown
    }
}

我手表上的接口控制器(连接到我的数据标签)包括:

func dataSourceDidUpdate(dataSource: DataSource) {
   switch dataSource.item {

   // the first application update- commented out to try the 2nd update
   //case .Number(let numberItem):
    //    titleLabel.setText(numberItem)
    //    print(numberItem)

   // the second application update
   case .uidValue(let uidItem):
       uidLabel.setText(uidItem)
       print(uidItem)
   case .Unknown:
       nidLabel.setText("please retry")
   default:
    print("default")
   }
}

您不能单独发送这些项目,因为 updateApplicationContext 会用最新数据替换任何较早的应用程序上下文数据。 the documentation:

中的两个不同位置简要提到了这一点

This method overwrites the previous data dictionary, ...

This method replaces the previous dictionary that was set, ...

自然地,Apple 优化了整个过程以提高 energy/memory 效率。在这种情况下,如果在第二数据排队等待传输时较早的应用程序上下文数据仍在待传输队列中,则可以丢弃较早的数据以避免不必要地transmit/store它。您的手表甚至不会收到第一个数据。

由于您的手表只会收到两条数据中的一条,这就解释了为什么当您检查接收到的字典中的一个键时会看到“请重试”,但它只包含另一个键的数据关键。

如何一次传输多个项目

将两个项目包含在同一个字典中,并使用单次传输传输该字典。

let data = ["number" : numberItem, "uidValue" : uidItem]
try WatchSessionManager.sharedManager.updateApplicationContext(data)
...

在手表端,您可以简单地同时更新title标签和uid标签,而不是有条件地只更新一个或另一个。

if let numberItem = data["number"] as? String {
    titleLabel.setText(numberItem)
}
if let uidItem = data["uidValue"] as? String {
    uidLabel.setText(uidItem)
}