Download picked GIF faster: cancel pending request

// FREEBIE
This commit is contained in:
Michael Kirk 2017-10-19 14:12:50 -07:00
parent b8ce636afc
commit 3939e8a6ac
3 changed files with 16 additions and 10 deletions

View File

@ -208,7 +208,7 @@ class GifPickerCell: UICollectionViewCell {
self.backgroundColor = nil
}
public func fetchRenditionForSending() -> Promise<GiphyAsset> {
public func requestRenditionForSending() -> Promise<GiphyAsset> {
guard let renditionForSending = self.renditionForSending else {
owsFail("\(TAG) renditionForSending was unexpectedly nil")
return Promise(error: GifPickerCellError.assertionError(description: "renditionForSending was unexpectedly nil"))
@ -218,15 +218,16 @@ class GifPickerCell: UICollectionViewCell {
// We don't retain a handle on the asset request, since there will only ever
// be one selected asset, and we never want to cancel it.
_ = GiphyDownloader.sharedInstance.requestAsset(rendition: renditionForSending,
priority: .high,
success: { _, asset in
fulfill(asset)
_ = GiphyDownloader
.sharedInstance.requestAsset(rendition: renditionForSending,
priority: .high,
success: { _, asset in
fulfill(asset)
},
failure: { _ in
// TODO GiphyDownloader API shoudl pass through a useful failing error
// so we can pass it through here
reject(GifPickerCellError.fetchFailure)
failure: { _ in
// TODO GiphyDownloader API shoudl pass through a useful failing error
// so we can pass it through here
reject(GifPickerCellError.fetchFailure)
})

View File

@ -333,7 +333,8 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
self.collectionView.isUserInteractionEnabled = false
cell.fetchRenditionForSending().then { (asset: GiphyAsset) -> Void in
GiphyDownloader.sharedInstance.cancelAllRequests()
cell.requestRenditionForSending().then { (asset: GiphyAsset) -> Void in
let filePath = asset.filePath
guard let dataSource = DataSourcePath.dataSource(withFilePath: filePath) else {
owsFail("\(self.TAG) couldn't load asset.")

View File

@ -237,6 +237,10 @@ extension URLSessionTask {
return assetRequest
}
public func cancelAllRequests() {
self.assetRequestQueue.forEach { $0.cancel() }
}
private func assetRequestDidSucceed(assetRequest: GiphyAssetRequest, asset: GiphyAsset) {
DispatchQueue.main.async {
self.assetMap.set(key:assetRequest.rendition.url, value:asset)