Skip to content
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

storeCachedData: callback once stored in cache #750

Closed
AndrewSB opened this issue Jan 18, 2024 · 5 comments
Closed

storeCachedData: callback once stored in cache #750

AndrewSB opened this issue Jan 18, 2024 · 5 comments
Labels

Comments

@AndrewSB
Copy link
Contributor

AndrewSB commented Jan 18, 2024

hey, i was using the 2 APIs ImagePipeline.shared.cache.storeCachedData(imageData, for: ImageRequest(url: remoteURL) + ImagePipeline.shared.loadImage(with: url).

i'm using them in pretty quick succession, when a user captures an image that is being uploaded. i'd like to await the completion of storeCachedData before i allow the call to loadImage, but afaict there's no callback/notification once the cached image has been saved. the 2 objects that make the calls are quite disconnected, and i'd prefer to not have them depend on oneanother.

how can i ensure that i don't make that loadImage call until storeCachedData has made the imageData available in the cache?

@kean
Copy link
Owner

kean commented Jan 19, 2024

If you are using the built-in DataCache, it has a so-called staging area where it stores the data while it's being saved on disk asynchronously, so there is no need to wait until it finishes. It's a documented behavior, so you can rely on it continuing working this way.

@AndrewSB
Copy link
Contributor Author

i believe i am, but i'm noticing a slight delay. my flow is

  1. cache the image
  2. in parallel, fetch the image and show it in a view that's presented modally

the image shows up black (i.e., i think it fails to load).

if i dismiss the modal, then re-present it, i think it works. so likely, there's something with threading/waiting for the staging area to accept the value before i try to fetch it

is there some way i can syncronize that? by submitting the logic to present the modal to a certain thread, or waiting for a notification to fire?

@kean
Copy link
Owner

kean commented Feb 18, 2024

Hey, can you please share the code that you are using?

There is a public API: flush(for:) that clears the staging area and puts the data on disk immediately. But there should be no need in using it, so I'm not sure it's going to help in this case. The root cause is likely somewhere else.

@AndrewSB
Copy link
Contributor Author

#755 is how i'm using the API. from running the test i just wrote, it looks like it doesn't result in a cache hit

@kean kean added the question label Feb 24, 2024
@AndrewSB
Copy link
Contributor Author

solved by the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants