Skip to content

Revert the changes to prefetch the image url from memory cache #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions SDWebImageSwiftUI/Classes/ImageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public final class ImageManager : ObservableObject {
var manager: SDWebImageManager
weak var currentOperation: SDWebImageOperation? = nil
var isFirstLoad: Bool = true // false after first call `load()`
var isFirstPrefetch: Bool = true // false after first call `prefetch()`

var url: URL?
var options: SDWebImageOptions
Expand Down Expand Up @@ -107,28 +106,6 @@ public final class ImageManager : ObservableObject {
}
}

/// Prefetch the initial state of image, currently query the memory cache only
func prefetch() {
isFirstPrefetch = false
var options = self.options
if options.contains(.fromLoaderOnly) {
// If user indeed ignore cache, don't do prefetch
return
}
// Use `.fromCacheOnly` to query cache only
options.insert(.fromCacheOnly)
var context = self.context ?? [:]
context[.queryCacheType] = SDImageCacheType.memory.rawValue
// Use `.queryCacheType` to query memory cache only
manager.loadImage(with: url, options: options, context: context, progress: nil) { (image, data, error, cacheType, finished, imageUrl) in
// This will callback immediately
self.image = image
if let image = image {
self.successBlock?(image, cacheType)
}
}
}

}

// Completion Handler
Expand Down
9 changes: 4 additions & 5 deletions SDWebImageSwiftUI/Classes/WebImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ public struct WebImage : View {
}

public var body: some View {
// this prefetch the memory cache of image, to immediately render it on screen
// this solve the case when `onAppear` not been called, for example, some transaction indeterminate state, SwiftUI :)
if imageManager.isFirstPrefetch {
imageManager.prefetch()
// This solve the case when WebImage created with new URL, but `onAppear` not been called, for example, some transaction indeterminate state, SwiftUI :)
if imageManager.isFirstLoad {
imageManager.load()
}
return Group {
if imageManager.image != nil {
Expand Down Expand Up @@ -100,7 +99,7 @@ public struct WebImage : View {
setupPlaceholder()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.onAppear {
// load remote image when first appear
// Load remote image when first appear
if self.imageManager.isFirstLoad {
self.imageManager.load()
return
Expand Down