Jun
Jun

Reputation: 3442

How do I improve my image download speed?

I am using react-native-fetch-blob for download image.

Here's my source code.

downloadThumb(element) {
    return new Promise((resolve, reject) => {
      RNFetchBlob.config({
        // add this option that makes response data to be stored as a file,
        // this is much more performant.
        fileCache: true,
        path: `${DocumentDirectoryPath}/thumbs/${element}`,
      })
        .fetch(
          'GET',
          `https://www.searchforsites.co.uk/images/thumbs/${element}`,
          {
            // some headers ..
          },
        )
        .then((res) => {
          resolve(true);
        })
        .catch((err) => {
          // console.log('Error: ', err);
          // this.setState({ modalOpen: false, regionUpdated: true });
          // this.findUser();
          reject(true);
        });
    });
  }

///////////////////////////////////////
  recursive_thumb(element) {
    this.downloadThumb(element).then((response) => {
      if (this.state.count > this.state.total_thumbs) {
        this.setState({ modalOpen: false });
        setThumbDownloaded();
        return true;
      }
      this.state.count += 1;
      // console.log(this.state.count);
      this.setState({ totalCount: this.state.total_thumbs, fetchedCount: this.state.count });
      this.recursive_thumb(this.state.thumbs[this.state.count]);
    });
  }

filesize is around 8KB. Now I am donwloading one by one. And downloading one image(8KB) takes a second.

Is there any solution to improve downloading speed?

Thanks for your time.

Upvotes: 0

Views: 466

Answers (1)

Hespen
Hespen

Reputation: 1452

If you'd like to increase the download of a sequence of images: Just create a Promise.all, which will fetch them all parallel

Promise.all([
    downloadThumb('data.jpeg'),
    downloadThumb('users.jpeg'),
    downloadThumb('products.jpeg')
])
.then(function(data) {
    console.log('Parallel promises >>>', data);
});

Check out this github page: https://github.com/jaydson/es7-async

Upvotes: 1

Related Questions