File.readAsArrayBuffer promise issue


#1

Hi all,

I have code as below.

If this.file.readAsArrayBuffer in hasFile() cannot resolve a file, an err is caught successfully. This is as what I expect.

however, if the file is resolved in hasFile(), there is a chance that console.log('then: ' + values); does not call. It means by random chances (like 50%) in ionic run android in my real android tablet, the console.log('then: ' + values); will not print out anything.

Why is this, really does not make sense for me.

process() {
    Promise.all([this.hasFile(1)]).then(values => {
      console.log('then: ' + values);
    }).catch(err => console.log('err: ' + err));
  }

  hasFile(i: number): Promise<ArrayBuffer> {
    console.log('in: ' + i);
    let fileName: string = i + this.suffix;
    return this.file.readAsArrayBuffer(this.file.externalDataDirectory, fileName);
  }

#2

Which version of ionic-native are you using?


#3
{
    "name": "firebase-email-auth",
    "version": "0.0.1",
    "author": "Ionic Framework",
    "homepage": "http://ionicframework.com/",
    "private": true,
    "scripts": {
        "clean": "ionic-app-scripts clean",
        "build": "ionic-app-scripts build",
        "lint": "ionic-app-scripts lint",
        "ionic:build": "ionic-app-scripts build",
        "ionic:serve": "ionic-app-scripts serve"
    },
    "dependencies": {
        "@angular/common": "4.1.0",
        "@angular/compiler": "4.1.0",
        "@angular/compiler-cli": "4.1.0",
        "@angular/core": "4.1.0",
        "@angular/forms": "4.1.0",
        "@angular/http": "4.1.0",
        "@angular/platform-browser": "4.1.0",
        "@angular/platform-browser-dynamic": "4.1.0",
        "@ionic-native/core": "3.7.0",
        "@ionic-native/facebook": "^3.10.3",
        "@ionic-native/file": "^3.10.3",
        "@ionic-native/keyboard": "^3.10.3",
        "@ionic-native/media": "^3.10.3",
        "@ionic-native/splash-screen": "^3.7.0",
        "@ionic-native/status-bar": "3.7.0",
        "@ionic-native/transfer": "^3.10.3",
        "@ionic/storage": "2.0.1",
        "angularfire2": "^4.0.0-rc.0",
        "cordova-android": "^6.2.3",
        "cordova-plugin-compat": "^1.1.0",
        "cordova-plugin-console": "^1.0.5",
        "cordova-plugin-crosswalk-webview": "^2.3.0",
        "cordova-plugin-device": "^1.1.4",
        "cordova-plugin-facebook4": "^1.9.0",
        "cordova-plugin-file": "^4.3.3",
        "cordova-plugin-media": "^3.0.1",
        "cordova-plugin-splashscreen": "^4.0.3",
        "cordova-plugin-statusbar": "^2.2.2",
        "cordova-plugin-whitelist": "^1.3.1",
        "cordova-plugin-wkwebview-engine": "git+https://github.com/driftyco/cordova-plugin-wkwebview-engine.git",
        "firebase": "3.9.0",
        "ionic-angular": "3.2.1",
        "ionic-plugin-keyboard": "^2.2.1",
        "ionicons": "3.0.0",
        "plyr": "^2.0.13",
        "rxjs": "5.1.1",
        "sw-toolbox": "3.6.0",
        "zone.js": "0.8.10",
        "cordova-plugin-file-transfer": "~1.6.3"
    },
    "devDependencies": {
        "@ionic/app-scripts": "1.3.7",
        "@ionic/cli-plugin-cordova": "1.2.1",
        "@ionic/cli-plugin-ionic-angular": "1.0.0",
        "typescript": "2.2.1"
    },
    "description": "An Ionic project",
    "cordova": {
        "plugins": {
            "cordova-plugin-facebook4": {
                "APP_ID": "229997797493525",
                "APP_NAME": "test-mobile"
            },
            "cordova-plugin-console": {},
            "cordova-plugin-device": {},
            "cordova-plugin-splashscreen": {},
            "cordova-plugin-statusbar": {},
            "cordova-plugin-whitelist": {},
            "ionic-plugin-keyboard": {},
            "cordova-plugin-crosswalk-webview": {
                "XWALK_VERSION": "23+",
                "XWALK_LITEVERSION": "xwalk_core_library_canary:17+",
                "XWALK_COMMANDLINE": "--disable-pull-to-refresh-effect",
                "XWALK_MODE": "embedded",
                "XWALK_MULTIPLEAPK": "true"
            },
            "cordova-plugin-wkwebview-engine": {},
            "cordova-plugin-media": {},
            "cordova-plugin-file-transfer": {}
        },
        "platforms": [
            "android"
        ]
    }
}

#4

How come you could use ionic-native 3.10.3, last version isn’t 3.10.2?

Anyway not what I thought, sorry I don’t know :frowning:


#5

It is 3.10.3. We missed to update the changelog, package.json and release page.


#6

I decided to cave…

My purpose is to implement a multiple-file upload in one POST request, but this part is too weird and wasted too much of my time…
I am using upload by sequence request now… it serves the purpose anyway…


#7

Maybe with observables?

import 'rxjs/add/observable/forkJoin';
 
let promises = new Array();
promises.push(myFirstPromise());
promises.push(mySecondPromise());
// etc.
 
// forkJoin run promises in parallels, wait for all promise resolve before subscribe result is resolved
Observable.forkJoin(promises).subscribe(
        (data: any[]) => {
           // Do stuffs
       },
      (err: any) => {
          // In case of at least one error
      }
    );

#8

I am not familar with observables. Need to pay the tech debt before using it :). I might come back and check your solution later. Thanks Reedrichards