Search code examples
iosreact-nativeapp-store-connectexpotestflight

Data on FileSystem.documentDirectory not persisting over app store updates in ios


I was working on a project that need some files to be downloaded into storage and later works offline . I am using Expo's Filesystem api to download the file and then saving the file in Expo FileSystem.documentDirectory

let directory = FileSystem.documentDirectory + 'media/';
await FileSystem.downloadAsync(imageurl,
                        directory + 'filename.jpg'
                    )
                        .then(({ uri }) => {
                            console.log(uri,'uri of image')
                        });

The problem is when i was updating app in itunesconnect the data is not persisting.


Solution

  • I guess you are trying to recover that file with absolute path after the update. Use a relative path instead.

    iOS will update UUID of directory each time you change your build (ex. different builds on TestFlight or different version on AppStore).
    ref: https://github.com/expo/expo/issues/4261#issuecomment-494072115

    What you need to do is to store the filename into some persistent storage and combine that with FileSystem.documentDirectory to recover that file later, because your file is saved and the relative path works fine but the absolute path has changed.

    Here is an example of copying an image into the document directory and recover that file later.

    const to = FileSystem.documentDirectory + filename
    await FileSystem.copyAsync({
      from: uri, // uri to the image file
      to
    })
    dispatch({ type: 'STORE_FILENAME', filename }) // Anything that persists. I use redux-persist 
    
    // ...after you updated the app to a different build
    const filename = props.filename // recovered from the previous build anyhow
    const uri = FileSystem.documentDirectory + filename
    
    // maybe you want to render that image
    return <Image uri={uri} />