1

首先,我使用“transferUserInfo”方法将字典从 iPhone 发送到 Apple Watch:

let dicty = //...my dictionary of property-list values...
if WCSession.isSupported() {
    let session = WCSession.defaultSession()
    if session.paired == true {  // Check if your Watch is paired with your iPhone
        if session.watchAppInstalled == true {  // Check if your Watch-App is installed on your Watch
            session.transferUserInfo(dicty)
        }
    }
}

然后我使用以下委托回调方法“didFinishUserInfoTransfer”来检查传输状态:

func session(session: WCSession, didFinishUserInfoTransfer userInfoTransfer: WCSessionUserInfoTransfer, error: NSError?) {
    if error == nil {
        let session = WCSession.defaultSession()
        let transfers = session.outstandingUserInfoTransfers
        if transfers.count > 0 {  //--> is always > 0, why ?????????
            for trans in transfers {
                trans.cancel()  // cancel transfer that will be sent by updateApplicationContext
                let dict = trans.userInfo
                session.transferUserInfo(dict)  //--> creates enless-transfer cycle !!!!!
            }
        }
    }
    else {
        print(error)
    }
}

在 Apple 文档中,它是关于 didFinishUserInfoTransfer 方法的:

The session object calls this method when a data transfer initiated by the
current app finished, either successfully or unsuccessfully. Use this method
to note that the transfer completed or to respond to errors, perhaps by
trying to send the data again at a later time.

到目前为止一切顺利 - 我明白了。但现在 - 有一些我不明白:

如果输入了 didFinishUserInfoTransfer 并且错误 == nil,那么究竟为什么 session.outstandingUserInfoTransfers COUNT 可以大于零??????

根据 Apple 文档,didFinishUserInfoTransfer 的唯一非错误状态应该是传输结束时!有点它似乎没有结束......为什么?

感谢您对此的任何澄清。

而且,我很高兴有任何关于如何正确使用这 3 种方法的示例代码!(IE

  • session.transferUserInfo(dicty)

  • didFinishUserInfoTransfer

  • session.outstandingUserInfoTransfers)

4

2 回答 2

1

就我阅读文档而言,有一点误解:仅在发生错误时再次发送。如果没有引发错误,则具有出色的 userInfoTransfers 是预期的行为;它们尚未成功发送,仍在排队中。

顺便提一句。代码使用实际的 dispatchQueue。

func session(_ session: WCSession, didFinish userInfoTransfer: WCSessionUserInfoTransfer, error: Error?) {

if error != nil {  // resend if an error occured

        DispatchQueue.main.async {

            let transfers = session.outstandingUserInfoTransfers
            if transfers.count > 0 {

                // print("open transfers: \(transfers.count)")

                for trans in transfers {

                    // print("resend transfer")

                    trans.cancel()  // cancel transfer
                    let dict = trans.userInfo
                    session.transferUserInfo(dict)  // send again
                }
            }
        }
    }
}
于 2017-05-22T15:53:22.660 回答
1

似乎在委托回调返回之前,触发didFinishUserInfoTransfer回调的 userInfoTransfer 不会从 中删除。outstandingUserInfoTransfers要获得您想要的行为(其中 count 可以降至 0),您需要 dispatch_async 远离委托回调线程。所以这应该工作:

func session(session: WCSession, didFinishUserInfoTransfer userInfoTransfer: WCSessionUserInfoTransfer, error: NSError?) {
    if error == nil {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            let transfers = session.outstandingUserInfoTransfers
            if transfers.count > 0 {  //--> will in most cases now be 0
                for trans in transfers {
                    trans.cancel()  // cancel transfer that will be sent by updateApplicationContext
                    let dict = trans.userInfo
                    session.transferUserInfo(dict)  // ***
                }
            }
        });
    }
    else {
        print(error)
    }
}

也就是说,我不太明白为什么您要在其中任何一个完成时取消所有剩余的未完成的 userInfoTransfers,只是为了重新排队它们(有问题的地方用 表示***

于 2016-01-14T16:53:27.230 回答