apache / cordova-ios

Apache Cordova iOS
https://cordova.apache.org/
Apache License 2.0
2.15k stars 987 forks source link

Any possible reason why files in www folder load ok in emulator but not in device? #1293

Closed jfoclpf closed 3 months ago

jfoclpf commented 1 year ago

The files seem to load correctly from www folder when the app is run in emulator, but not when run from the device (iPhone SE 1gen).

Any possible hint why this is happening?

I am reading like this

  readFile(cordova.file.applicationDirectory + 'www/json/anomalies.json').then((res) => {
    const data = JSON.parse(res)

where readFile is

export function readFile (path, options) {
  options = options || {}

  return new Promise(function (resolve, reject) {
    window.resolveLocalFileSystemURL(path, (fileEntry) => {
      fileEntry.file((file) => {
        const reader = new FileReader()
        reader.onloadend = () => {
          resolve(reader.result)
        }

        const format = options.format
        switch (format) {
          case 'arrayBuffer':
            reader.readAsArrayBuffer(file)
            break
          case 'binaryString':
            reader.readAsBinaryString(file)
            break
          case 'dataURL':
            reader.readAsDataURL(file)
            break
          default:
            reader.readAsText(file)
        }

        reader.onerror = () => {
          reject(Error(`Error occurred reading file: ${path}`))
        }
      })
    }, (error) => {
      reject(Error('resolve error', error))
    })
  })
}

IMG_20230227_211127

Any help will be highly appreciated Thanks

breautek commented 1 year ago

If it's erroring at the FileReader level, you may not be seeing the result because you're only hooking the onerror callback after the readAs* call.

Otherwise it will be good to include the error, if any.

I don't think it's related, but you resolve during onloadend but also reject in onerror. This is a problem because on error, both will be called and you are going to try to resolve or reject a a promise that already has an resolution. Instead of onloadend, you can use onload instead which will only fire on successful, which should guarantee that you resolve/reject the promise only once.

Otherwise, I don't see any a problem code wise.

jfoclpf commented 1 year ago

Thank you very much @breautek

If it's erroring at the FileReader level, you may not be seeing the result because you're only hooking the onerror callback after the readAs* call.

Should I then declare the reader.onerror before the readAs* call? Interesting, I will do it

I don't think it's related, but you resolve during onloadend but also reject in onerror. This is a problem because on error, both will be called and you are going to try to resolve or reject a a promise that already has an resolution. Instead of onloadend, you can use onload instead which will only fire on successful, which should guarantee that you resolve/reject the promise only once.

Thank you very much, it makes sense, I will amend onloadend to onload

erisu commented 3 months ago

It seems that the discussion has reached a resolution. If the issue has been resolved, could you please close the ticket?

jfoclpf commented 3 months ago

thanks for all the support and great work