gpt4 book ai didi

ios - 使用 WatchConnectivity 获取核心数据请求

转载 作者:行者123 更新时间:2023-11-28 12:13:15 26 4
gpt4 key购买 nike

我目前正在尝试将 CoreData 数据从我的 iOS 应用程序获取到 watchOS 扩展程序。我正在使用 WatchConnectivity 框架通过 sendMessage(_ message: [String : Any], replyHandler: (([String : Any]) -> Void)?, errorHandler: ((Error) -> Void)? = nil) 函数。基本连接工作正常。可以访问 iOS 应用程序,如果我尝试回复示例词典,一切正常。

到目前为止一切顺利,但当我开始在后台对 iOS 应用程序执行提取请求时,Watch 应用程序从未接收到数据。过了一会儿,我收到了这个错误: Error while requesting data from iPhone: Error Domain=WCErrorDomain Code=7012 “Message reply too long.” UserInfo={NSLocalizedFailureReason=发生回复超时。, NSLocalizedDescription=消息回复时间过长。}

如果我在 iPhone 上打开 iOS 应用程序并重新启动 Watch 应用程序,回复处理程序就会得到结果。但是强制用户在 iPhone 上主动打开 iOS 应用是没有用的。

有人可以解释为什么会这样吗?正确的方法是什么?自 watchOS 2 以来,应用组似乎已过时。
我正在使用 Swift 4 顺便说一句……

在 Apple Watch 上:

import WatchConnectivity

class HomeInterfaceController: WKInterfaceController, WCSessionDelegate {

// (…)

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {

session.sendMessage(["request": "persons"],
replyHandler: { (response) in
print("response: \(response)")
},
errorHandler: { (error) in
print("Error while requesting data from iPhone: \(error)")
})
}

在 iPhone 上:

import CoreData
import WatchConnectivity

class ConnectivityHandler: NSObject, WCSessionDelegate {

var personsArray:[Person] = []

// (…)

func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {

// only using the next line is working!
// replyHandler(["data": "test"])

if message["request"] as? String == "persons" {
fetchAllPersons()

var allPersons: [String] = []
for person in personsArray {
allPersons.append(person.name!)
}

replyHandler(["names": allPersons])
}
}

// this seems to be never executed (doesn't matter if it's in an extra function or right in the didReceiveMessage func)
func fetchAllPersons() {

do {
// Create fetch request.
let fetchRequest: NSFetchRequest<Person> = Person.fetchRequest()

// Edit the sort key as appropriate.
let sortDescriptor = NSSortDescriptor(key: #keyPath(Person.name), ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]

personsArray = try DatabaseController.getContext().fetch(fetchRequest)
} catch {
fatalError("Failed to fetch: \(error)")
}
}

最佳答案

在研究了这个问题后,我自己找到了解决方案。问题是我正在使用 sendMessage(_:replyHandler:errorHandler:) 协议(protocol)。这仅用于在两个应用都处于事件状态时传输数据。

Use the sendMessage(_:replyHandler:errorHandler:) or sendMessageData(_:replyHandler:errorHandler:) method to transfer data to a reachable counterpart. These methods are intended for immediate communication between your iOS app and WatchKit extension. The isReachable property must currently be true for these methods to succeed.

如果您想在后台传输数据,您必须根据需要使用 updateApplicationContext(_:)transferUserInfo(_:)。这正是我所需要的!

Use the updateApplicationContext(_:) method to communicate recent state information to the counterpart. When the counterpart wakes, it can use this information to update its own state. For example, an iOS app that supports Background App Refresh can use part of its background execution time to update the corresponding Watch app. This method overwrites the previous data dictionary, so use this method when your app needs only the most recent data values.

Use the transferUserInfo(_:) method to transfer a dictionary of data in the background. The dictionaries you send are queued for delivery to the counterpart and transfers continue when the current app is suspended or terminated.

现在,如果 iPhone 应用对应方打开 ApplicationContext 或 UserInfo 队列,我可以将数据添加到我的核心数据库。

关于ios - 使用 WatchConnectivity 获取核心数据请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com