Delay in retrieving data from Ionic 2 storage

2019-06-04 02:40发布

问题:

Before I launches the app I will check with local storage if any user data available, If yes I will navigation to Home page else Login page.

Here I'm unable to retrieve stored data, Any inputs please...

Currently using Ionic 2 SQlite plugin.

Note: In browser it's working fine but on Android device it's not working.

app.component.ts : checking user data

loadUser() {
        this.userSettings.getUser().then(user => {
            this.userObj = JSON.stringify(user);
            if (user) {
                console.log('App : ', this.userObj);
                this.nav.setRoot(HomePage,this.userObj);

            } else {
                console.log('App : No user data');
                this.rootPage = LoginPage;
            }

        });

    }

login.ts : Saving user data

this.userSettings.addUser(
                            userData.employeeCode,
                            userData.password,
                            userData.role
                        );

user-settings.ts : Storage file in providers

getUser() {
        if (this.sql) {
            return this.sql.get('user').then(value => value);
        } else {
            return new Promise(resolve =>                 resolve(this.storage.get('user').then(value => value)));
        }
}

addUser(employeeCode, password, role) {
        let item = { employeeCode: employeeCode, password: password, role: role };

        if (this.sql) {
            this.sql.set('user', JSON.stringify(item)).then(data => {
                this.events.publish('userObj:changed');
            });
        } else {
            return new Promise(resolve => {
                this.storage.set('user', JSON.stringify(item)).then(() => {
                    this.events.publish('userObj:changed');
                    resolve();
                });
            });
        }
    }

app.module.ts:

providers: [
        { provide: ErrorHandler, useClass: IonicErrorHandler },
        AuthService,
        SqlStorage,
        UserSettings,
        Storage
    ]

Thanks in advance.

回答1:

Problem solved

After calling the sqlite operation in ngAfterViewInit it's working fine.

ngAfterViewInit() {
        this.storage.get('user').then((user: any) => {
            if (user) {

                this.userCredentials = JSON.parse(user);

                this.nav.setRoot(HomePage, this.userCredentials);

            }
            else {
                this.rootPage = LoginPage;
            }
        });
    }

[Source] (https://github.com/driftyco/ionic-conference-app/blob/master/src/pages/account/account.ts)

Cheers :)



回答2:

As you point out that your code is working in Chrome, but not on your device, you might be calling sqlite before cordova's device.ready() has fired.

In app.component.ts ensure you call this.loadUser() in the following manner:
(platform.ready() should already be in the constructor)

 platform.ready().then(() => {
      this.loadUser();
    });