0

To speed up the loading of profile images, I created an Isolate and cached it in it.

Code to load profile image

influencers.forEach(
    (influencer) {
        Isolate.spawn(
            FileDownloadController.imagePreload,
            influencer.profileImgUrl,
        );
    },
);

imagePreload in FileDownloadController

static void imagePreload(String url) {
    CachedNetworkImage(imageUrl: url);
}

My intention

  • When the runApp() is executed to start the app, the influencers.forEach() statement works to cache the image.
  • When approaching the influencer screen, pre-cached images are used.

Is this right?
I was told that 'Isolate' cannot be involved in other 'Isolate'. Wouldn't it be futile to cache images this way?

I want to know if I understood 'Isolate' properly.

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
  • Why do you need an isolate? – Christopher Moore Feb 16 '21 at 01:29
  • @ChristopherMoore Because the app loads many images. It's like loading multiple images with multi threads. – haedong-jeon Feb 16 '21 at 01:32
  • Isolates are **not** threads. They do not have shared memory. I don't know what the CachedNetworkImage implementation is, but this likely isn't possible. – Christopher Moore Feb 16 '21 at 01:34
  • Also, the way you've implemented the Isolate is likely very inefficient. The cost of starting an isolate is probably too high to justify making a new one for every image you want to cache. – Christopher Moore Feb 16 '21 at 01:38
  • @ChristopherMoore __OH....._ If I work in multiple 'Isolate', aren't multiple jobs going on at the same time? – haedong-jeon Feb 16 '21 at 01:39
  • They are multiple jobs going at the same time. But that doesn't make them equivalent to threads. Isolates are meant for heavy work. In flutter it's usually used for work that would be too heavy for the main thread and would block the UI. Simply caching a network image is not heavy work. Most of the time involved is waiting for the image to download. – Christopher Moore Feb 16 '21 at 01:40
  • @ChristopherMoore Now I understand what you are saying. Then I should use async/await, not Isolate. – haedong-jeon Feb 16 '21 at 01:44
  • You should use asynchrony. You don't have to use async/await, but essentially yes that's all you should need. If you're having performance issues, then start trying stuff like this. If you don't have problems yet, then this isn't something you should worry about. – Christopher Moore Feb 16 '21 at 01:46

0 Answers0