joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.81k stars 772 forks source link

Image downloaded cannot be loaded from storage on next boot on IOS #719

Closed joshua-augustinus closed 3 years ago

joshua-augustinus commented 3 years ago

I'm downloading an image then saving the filepath to storage. then on next bootup check the filepath; if it exists then use that as the source for the image. Example:

export const preloadImage = (images: ImageInfo, callback?) => {
    const shouldCache = true;
    //Init
    return RNFetchBlob
        .config({
            // add this option that makes response data to be stored as a file,
            // this is much more performant.
            fileCache: shouldCache,
            // by adding this option, the temp files will have a file extension
            appendExt: 'jpg'
        })
        .fetch('GET', images.url, {
            //some headers ..
        })
        .then((res) => {
            let source;
            if (Platform.OS === 'android') {
                source = { uri: 'file://' + res.path() }

            } else {
                source = { uri: '' + res.path() }

            }
            const key = images.id.toString();
            localImages[key] = source;

            if (callback) {
                callback(source);
            }

            saveImageKeysToStorage();
        })
}

On Android it works fine. But on ios it doesn't work.

I have a sample repo here: https://github.com/joshua-augustinus/training-storage

albmin commented 3 years ago

are you storing it somewhere persistent? or in a temporary storage location that's prone to purging?

joshua-augustinus commented 3 years ago

I didn't set the filepath so am using whatever the default is.

I tried again using DocumentDir

    const shouldCache = true;
    const dirs = RNFetchBlob.fs.dirs

    //Init
    return RNFetchBlob
        .config({
            // add this option that makes response data to be stored as a file,
            // this is much more performant.
            fileCache: shouldCache,
            // by adding this option, the temp files will have a file extension
            path: dirs.DocumentDir + '/path-to-file.jpg'
        })

And it worked on a real device but not on simulator.

After further testing it seems the issue is just the simulator. It works with unspecified filepath on real device but not simulator.

joshua-augustinus commented 3 years ago

For anyone who has the same issue also note that the DocumentDir on IOS is dynamic on building and installing the app a second time. So you have to store the filename and add that to the document dir rather than saving the whole path.