【发布时间】:2023-03-20 02:39:01
【问题描述】:
我将Apollo iOS 0.8 与 Xcode 9.3、Swift 4.1 和 iOS 11 一起使用,并像这样初始化 Apollo 客户端实例:
import Apollo
// ... unrelated code skipped
let configuration = URLSessionConfiguration.default
if let token = keychain.accessToken {
// Add additional headers as needed
configuration.httpAdditionalHeaders = [
"Authorization": "Bearer \(token)"
]
}
let graphqlEndpoint = URL("https://sample-server-url/graphql")!
let client = ApolloClient(networkTransport:
HTTPNetworkTransport(url: graphqlEndpoint, configuration: configuration))
应用程序可以很好地处理发送到 GraphQL 服务器的所有查询和突变,除非应用程序在后台。据我所知,使用常见的NSURLSession 实例可以通过将会话配置切换到URLSessionConfiguration.background(withIdentifier: "your-session-id") 来轻松解决。
但是当我换行时
let configuration = URLSessionConfiguration.default
与
let configuration = URLSessionConfiguration.background(withIdentifier: "your-session-id")
应用程序开始崩溃并出现以下错误:Terminating app due to uncaught exception 'NSGenericException', reason: 'Completion handler blocks are not supported in background sessions. Use a delegate instead.'
在使用 Apollo GraphQL 时解决此错误的最佳方法是什么,或者是否有其他方法可以在后台与 GraphQL 服务器进行通信?
【问题讨论】:
标签: ios swift graphql apollo apollo-ios