What's the proper way to provide a default closure that gets set on a stored property? I'm investigating a crash which I think is related to a default closure value initialized in an init. I generally provide a default value on the parameter itself. I saw the default closure created within the init which doesn't feel right to me.
Example of the crash report.
MyModule
partial apply for closure #1 in DownloadOperation.getData ...
NSGenericException: Task created in a session that has been invalidated
0 CoreFoundation __exceptionPreprocess +220
2 CFNetwork CFURLRequestSetRequestPriority + 10688
3 Embrace swizzledDataTaskWithRequest + 492
4 MyModule DownloadOperation.swift line 22
partial apply for closure #1 in DownloadOperation.executeRequest(_:) + 11
Here's the gist of what the class structure looks like:
01 class DownloadOperation: Foundation.Operation {
02 typealias ResultType = Result<Data, Error>
03 typealias CompletionType = (ResultType) -> ()
04
05 let request: URLRequest
06 var onCompletion: CompletionType
07
08 init(request: URLRequest) {
09 ...
10 self.request = request
11 self.onCompletion = {_ in}
12 super.init()
13 }
14
15 override func execute() {
16 ...
17 executeRequest(self.request)
18 }
19
20 private func executeRequest(_ request: URLRequest) {
21 self.workQueue.async {
22 let task = self.session.dataTask(with: self.request)
23 task.resume()
24 }
25 }
26 }
27
28 // gets used like so
29 let op = DownloadOperation()
30 op.onCompletion = { (result) in
31 ...
32 }
I think it makes more sense to pass in an escaping closure to the init and provide a default if needed. The reason why I've narrowed it down to this is line 22 and 11 mentioned in the crash.
// make the onCompletion property private and a let.
private let onCompletion: CompletionType
init(request: URLRequest, completion: @escaping CompletionType = {_ in}) {
...
self.onCompletion = completion
}
// then of course update the references
let op = DownloadOperation { (result) in
...
}