didFinishUserInfoTransfer 成功完成 - 但仍然有 outstandingUserInfoTransfers 对象
didFinishUserInfoTransfer finished successfully - but still with outstandingUserInfoTransfers Objects
首先,我使用 "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 种方法的示例代码!
(即
session.transferUserInfo(dicty)
didFinishUserInfoTransfer
session.outstandingUserInfoTransfers)
似乎在委托回调返回之前,触发 didFinishUserInfoTransfer
回调的 userInfoTransfer 不会从 outstandingUserInfoTransfers
中删除。要获得您想要的行为(计数可以下降到 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,只是 re-queue 它们(有问题的地方由 ***
)
有一点误解,据我阅读文档:只有在发生错误时才重新发送。如果没有出现错误,则拥有出色的 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
}
}
}
}
}
首先,我使用 "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 种方法的示例代码! (即
session.transferUserInfo(dicty)
didFinishUserInfoTransfer
session.outstandingUserInfoTransfers)
似乎在委托回调返回之前,触发 didFinishUserInfoTransfer
回调的 userInfoTransfer 不会从 outstandingUserInfoTransfers
中删除。要获得您想要的行为(计数可以下降到 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,只是 re-queue 它们(有问题的地方由 ***
)
有一点误解,据我阅读文档:只有在发生错误时才重新发送。如果没有出现错误,则拥有出色的 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
}
}
}
}
}