-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Implement image caching functionality for iOS #4515
Conversation
using UIKit; | ||
|
||
namespace Microsoft.Maui | ||
{ | ||
public partial class UriImageSourceService | ||
{ | ||
internal string CacheDirectory = Essentials.FileSystem.CacheDirectory + "/com.microsoft.maui.sample/MauiUriImages/"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if CachDirectory has a trailing /, but we could be extra safe and use Path.Combine() here to build up the path and let the sdk figure out the path separators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
if (imageData == null) | ||
throw new InvalidOperationException("Unable to load image stream data."); | ||
|
||
await CacheImage(imageData, pathToImageCache); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put a .ConfigureAwait(false) on the end of this call?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed the Task / await stuff since it's not needed
using UIKit; | ||
|
||
namespace Microsoft.Maui | ||
{ | ||
public partial class UriImageSourceService | ||
{ | ||
internal string CacheDirectory = Path.Combine(Essentials.FileSystem.CacheDirectory, "com.microsoft.maui.sample", "MauiUriImages"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, there.
I don't know the context behind this name, but do we want to keep the sample
in the path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be cache
instead of sample
and in the future use this path as a root directory for other caches (?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated, thanks for the feedback!
if (imageData == null) | ||
throw new InvalidOperationException("Unable to load image stream data."); | ||
|
||
CacheImage(imageData, pathToImageCache); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need a check here to ensure caching is enabled like in Android:
maui/src/Core/src/ImageSources/UriImageSourceService/UriImageSourceService.Android.cs
Line 32 in 12f17cf
if (!imageSource.CachingEnabled) |
|
||
NSData? imageData; | ||
|
||
if (IsImageCached(pathToImageCache)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check here to see if caching is enabled for the image source... Android shows how to check the setting:
maui/src/Core/src/ImageSources/UriImageSourceService/UriImageSourceService.Android.cs
Line 32 in 12f17cf
if (!imageSource.CachingEnabled) |
Probably faster to check this than to look if the file exists, if caching is disabled anyway.
Just happened to come across this: how does a developer clear the cache if they want to force retrieving a new image? Of course they can set CachingEnabled=false and get a new image, but the cache will be stale, and will forever be tainted, no? I'm worried given that the caching seems enabled by default, that maybe this could lead to weird bugs in apps? Caching is a very hard problem and I am concerned this could lead to hard-to-find and hard-to-fix bugs. |
Came across this and thought its for all platforms. Can it be for all platforms? Why is currently only iOS in plan? |
Description of Change
Implements #1966
PR Checklist
Does this PR touch anything that might affect accessibility?
If any of the above checkboxes apply to your PR, then the PR will need to provide testing to demonstrate that accessibility still works.