I use Apollo iOS 0.8 with Xcode 9.3, Swift 4.1 and iOS 11, and initialise Apollo client instance like this:
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))
The application works well with all queries and mutations sent to the GraphQL server without a problem, except when the app is in background. As far as I know, with common NSURLSession
instance it can be easily solved by switching session configuration to URLSessionConfiguration.background(withIdentifier: "your-session-id")
.
But when I replace the line
let configuration = URLSessionConfiguration.default
with
let configuration = URLSessionConfiguration.background(withIdentifier: "your-session-id")
the app starts crashing with this error: Terminating app due to uncaught exception 'NSGenericException', reason: 'Completion handler blocks are not supported in background sessions. Use a delegate instead.'
What's the best way to resolve this error when using Apollo GraphQL or is there any other way to communicate with a GraphQL server in background?