I'm working on an independent watchOS app and I'm testing on the Apple Watch Ultra simulator as well as a couple of real Apple Watch Ultra devices (both have active cellular subscriptions on AT&T, are within 3 feet of their paired iPhones and connected to WiFi, as well).
My app has an application delegate which implements the applicationDidFinishLaunching() method and in that method, I register with APNs for remote notifications. When I receive the token in didRegisterForRemoteNotifications(withDeviceToken:), I send the token on to the server that is going to send notifications to the app.
When I test this code in the Apple Watch Ultra simulator, it works 100% of the time. When I test the same code on a real Apple Watch Ultra, about 70% of the time, I get the following error message in the Xcode debug console:
2023-05-12 08:32:30.779560-0400 Watch App Prototype[569:586139] PDTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2> finished with error [9] Error Domain=NSPOSIXErrorDomain Code=9 "Bad file descriptor" UserInfo={_kCFStreamErrorCodeKey=9, _kCFStreamErrorDomainKey=1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataPDTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>",
"LocalDataTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataPDTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>}
2023-05-12 08:32:30.780401-0400 Watch App Prototype[569:586139] Task <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2> finished with error [9] Error Domain=NSPOSIXErrorDomain Code=9 "Bad file descriptor" UserInfo={_kCFStreamErrorCodeKey=9, _kCFStreamErrorDomainKey=1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>",
"LocalDataPDTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>",
"LocalDataTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>}
The operation couldn’t be completed. Bad file descriptor
I ran across a post from someone else about the same problem on StackOverflow.
I'm not sure what to make of this error but here's the code that I'm using to send the URL request:
func perform(_ urlRequest: URLRequest) async throws -> Data {
let (data, response) = try await urlSession.data(for: urlRequest)
guard let httpResponse = response as? HTTPURLResponse else {
throw NetworkError.serverSideError(response: nil)
}
guard (200...299).contains(httpResponse.statusCode) else {
throw NetworkError.serverSideError(response: httpResponse)
}
return data
}
For what it's worth, urlSession is the shared URLSession instance. Again, this error never occurs in the simulator but it happens on both of the Apple Watch Ultra devices and I would say it occurs about 70% of the time. Can anyone help me understand this error message?