Skip to content

Commit

Permalink
Fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymonMrozek committed May 21, 2018
1 parent ebb7561 commit 8f11e2d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
42 changes: 24 additions & 18 deletions Cedric/Cedric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,50 +180,56 @@ public class Cedric {

group.addAsyncOperation(operation: operation)

delegates.invoke({ [task = item.task!] in
$0.cedric(self, didStartDownloadingResource: item.resource, withTask: task)
delegates.invoke({ [task = item.task!, resource = item.resource] in
$0.cedric(self, didStartDownloadingResource: resource, withTask: task)
})
}

fileprivate func remove(downloadItem item: DownloadItem) {
guard let index = items.index(of: item) else { return }
let item = items[index]

items.remove(at: index)
item.releaseReferences()

guard items.isEmpty else { return }
delegates.invoke({ $0.cedric(self, didFinishWithMostRecentError: self.lastError) })
}
}

// MARK: - DownloadItemDelegate

extension Cedric: DownloadItemDelegate {
internal func item(_ item: DownloadItem, withTask task: URLSessionDownloadTask, didCompleteWithError error: Error?) {
delegates.invoke({ $0.cedric(self, didCompleteWithError: error, withTask: task, whenDownloadingResource: item.resource) })
delegates.invoke({ [resource = item.resource, task] in
$0.cedric(self, didCompleteWithError: error, withTask: task, whenDownloadingResource: resource)
})
item.delegate = nil
remove(downloadItem: item)
}

internal func item(_ item: DownloadItem, didUpdateStatusOfTask task: URLSessionDownloadTask) {
// single item progress report
delegates.invoke({ $0.cedric(self, didUpdateStatusOfTask: task, relatedToResource: item.resource) })
delegates.invoke({ [task, resource = item.resource] in
$0.cedric(self, didUpdateStatusOfTask: task, relatedToResource: resource)
})

// maybe should consider some groupped resources progress reporting ...
}

internal func item(_ item: DownloadItem, didFinishDownloadingTo location: URL) {
do {
let file = try DownloadedFile(absolutePath: location)
delegates.invoke({ $0.cedric(self, didFinishDownloadingResource: item.resource, toFile: file) })
delegates.invoke({ [resource = item.resource] in
$0.cedric(self, didFinishDownloadingResource: resource, toFile: file)
})
} catch let error {
delegates.invoke({ [task = item.task!] in
$0.cedric(self, didCompleteWithError: error, withTask: task, whenDownloadingResource: item.resource)
delegates.invoke({ [task = item.task!, resource = item.resource] in
$0.cedric(self, didCompleteWithError: error, withTask: task, whenDownloadingResource: resource)
})
}

item.delegate = nil
remove(downloadItem: item)
}

fileprivate func remove(downloadItem item: DownloadItem) {
guard let index = items.index(of: item) else { return }
let item = items[index]

items.remove(at: index)
item.releaseReferences()

guard items.isEmpty else { return }
delegates.invoke({ $0.cedric(self, didFinishWithMostRecentError: self.lastError) })
}
}
2 changes: 1 addition & 1 deletion Cedric/MulticastDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class MulticastDelegate<T> {
}

internal func addDelegate(_ delegate: T) {
queue.async { [weak self] in
queue.async { [weak self] in
self?.delegates.add(delegate as AnyObject)
}
}
Expand Down

0 comments on commit 8f11e2d

Please sign in to comment.