Closed petey9891 closed 2 years ago
Hi, for me worked following:
`class ImageDownloader {
private var imageDataTask: URLSessionDataTask?
private let cache = ImageCache.cache
private(set) var isCancelled = false
func downloadPhoto(with url: URL, completion: @escaping ((UIImage?, Bool) -> Void)) {
guard imageDataTask == nil else { return }
isCancelled = false
if let cachedResponse = cache.cachedResponse(for: URLRequest(url: url)),
let image = UIImage(data: cachedResponse.data) {
completion(image, true)
return
}
let config = URLSessionConfiguration.default
config.urlCache = cache
let session = URLSession(configuration: config)
imageDataTask = session.dataTask(with: url) { [weak self] (data, response, error) in
guard let strongSelf = self else { return }
strongSelf.imageDataTask = nil
guard let data = data, let response = response, let image = UIImage(data: data), error == nil else { return }
let cachedResponse = CachedURLResponse(response: response, data: data)
strongSelf.cache.storeCachedResponse(cachedResponse, for: URLRequest(url: url))
DispatchQueue.main.async {
completion(image, false)
}
}
imageDataTask?.resume()
}
func cancel() {
isCancelled = true
imageDataTask?.cancel()
}
} `
this bit is important:
`let config = URLSessionConfiguration.default config.urlCache = cache let session = URLSession(configuration: config)
imageDataTask = session.dataTask(with: url) { [weak self]...`
the cache has to be asigned to the session.
hope it helps
There is an open pull request https://github.com/unsplash/unsplash-photopicker-ios/pull/34 related to this issue. @ocollet, what needs to be done to fix the caching issue?
... let config = URLSessionConfiguration.default config.urlCache = cache let session = URLSession(configuration: config) imageDataTask = session.dataTask(with: url) { [weak self] (data, response, error) in ...
This didn't work for me
Fixed with pull-request #34
When presenting the UnsplashPhotoPicker as described below, my application is throwing hundreds of these errors in the console. Any thoughts on what could be causing them and how to fix it?
[logging-persist] cannot open file at line 44515 of [02c344acea]
[logging-persist] os_unix.c:44516: (0) open(//unsplash//Cache.db) - Undefined error: 0
NetworkStorageDB:_openDBReadConnections: failed to open read connection to DB @ unsplash//Cache.db. Error=14. Cause=unable to open database file
The read-connection to the DB=unsplash//Cache.db is NOT valid. Unable to determine schema version.
[logging-persist] cannot open file at line 44515 of [02c344acea]
[logging-persist] os_unix.c:44516: (0) open(//unsplash//Cache.db) - Undefined error: 0
NetworkStorageDB:_openDBWriteConnections: failed to open write connection to DB @ unsplash//Cache.db. Error=14. Cause=unable to open database file
DEBUG: there is no SQL cache DB located at unsplash//Cache.db.
DEBUG: there is no SQL cache DB located at unsplash//Cache.db-shm.
DEBUG: there is no SQL cache DB located at unsplash//Cache.db-wal.
[logging] API call with unopened database connection pointer
[logging] misuse at line 130558 of [02c344acea]