View not updating after second plugin callback [Ionic2]


#1

For my Ionic 2 application I call a plugin. After its success callback I update the view (which works), and then I call another method of the same plugin. After that I also want to update the view, but that doesn’t work. Some code:

(<any>window).MyPlugin.firstMethod((result) => {
    this.visible = true; /* Updating view, works */

    (<any>window).MyPlugin.secondMethod((result) => {
         this.visible = false; /* Updating view, doesn't work! */
    });
});

The view only updates after I fire a completely other method (example when I press a button somewhere else on the template). So, the this.visible gets updated after the second method call, but the update is not visible in the view.

Does anyone has an idea? Thanks!

EDIT: Okay… solved after wrapping it in a NgZone :slight_smile:


#2

Nesting promises like that is an antipattern. Better to chain them.

.then(_ => something)
.then(_ => something else)

instead of putting the second then inside the first.


#3

Thanks @AaronSterling, but is a plugin in this way also a real promise?

Also I’m not sure how to use it with the two different method calls. Could you post an example of that?


#4

The structure is PromiseLike and asynchronous. That’s what you need for chain/nest to matter.