I'm building a widget for macOS, which is target for macOS only. And, it keeps crashing after an attempt to recreate another timeline (from getTimeline function)
It throws the following error:
Terminating app due to uncaught exception 'NSFileHandleOperationException', reason: '*** -[NSConcreteFileHandle fileDescriptor]: Invalid argument' terminating with uncaught exception of type NSException
I believe it is generated due to the lack of implementation of the method onBackgroundURLSessionEvents(matching:_:), which brought my attention to the following documentation ⬇️
From Apple doc :
When your widget extension is active like when providing a snapshot or timeline, it can initiate background network requests. The process is similar to how an app handles this type of request, which is described in Downloading Files in the Background. Instead of resuming your app, WidgetKit activates your widget’s extension directly. To handle the result of the network request, use the onBackgroundURLSessionEvents(matching:_:) modifier to your widget’s configuration, and do the following:
Store a reference to the completion parameter. You call the completion handler after processing all network events.
Use the identifier parameter to find the URLSession object you used when initiating the background request. If your widget extension was terminated, use the identifier to recreate the URLSession.
After invoking onBackgroundURLSessionEvents(), the system calls the urlSession(_:downloadTask:didFinishDownloadingTo:) method of the URLSessionDelegate you supplied to the URLSession. When all events have been delivered, the system calls the delegate’s urlSessionDidFinishEvents(forBackgroundURLSession:) method.
To refresh your widget’s timeline after the network request completes, call the WidgetCenter methods from your delegate’s implementation of urlSessionDidFinishEvents. Once you finish handling the events, call the completion handler that you previously stored in onBackgroundURLSessionEvents().
The problem is that urlSessionDidFinishEvents(forBackgroundURLSession:) is only available for:
OS 7.0+
Mac Catalyst 13.0+
tvOS 9.0+
watchOS 2.0+
It seems like there is no equivalent idea for macOS targets. So, how should we set it up?