ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

Ionic 2 - events subscription and nav.insertPages

Open efishman15 opened this issue 10 years ago • 8 comments

I've been hunting this "ghost" for the past 24 hours - and finally have been able to reproduce a very strange bug, with the following scenario:

Assume we have a small app with: MainPage, Page A, Page B. MainPage has a button that opens PAGE A, PAGE B (in this order) using nav.insertPages. Page A - has nothing special in the UI except a title "Page A". Page B - has a button to nav.pop (go back) to Page A.

The following illustrates a small piece of code for each page: (Assuming nav and events have been injected and stored somewhere globally):

MainPage:

openAB() { nav.insertPages([A,B]) }

A

onViewLoaded() { events.subscribe("b:closed") { console.log("B was closed..."); }
}

B

closeB() { nav.pop().then(()=> { events.publish("b:closed"); })

The scenario to reproduce:

  1. Press on the MainPage button to open pages A,B
  2. Press on the "Close" button in PAGE B going back to A (Console prints "B was closed")
  3. Go back from A to the Main Page.
  4. Press again on the MainPage button to open pages A,B
  5. Press on the "Close" button in PAGE B going back to A (Console prints "B was closed" TWICE !!!)

I've intentionally put the subscrive code in A in the onViewLoaded as documented that this event should occur once only (if the view is restored from cache).

It appears that because of the nav.insertPages that "skips" on Page A, the events.channels["b:closed"] contains now two array items that point to the subscription function and both of the are invoked - although we have closed Page A (step 3).

Please feel free to ask further questions if I was not clear enough.

Best Regards, Eddy Fishman

efishman15 avatar Aug 14 '16 00:08 efishman15

Hello! Thanks for opening an issue with us! Would you mind creating a repo that shows an example of your issue? Thanks for using Ionic!

jgw96 avatar Aug 15 '16 19:08 jgw96

Hi Jason,

Sure - here is a plunker demo reproducing the issue - this plunker works with angular 2 beta 15 and ionic 2 beta 5 - but as I said - it also happens on the latest and greatest ionic 2 beta 11 + angular rc4 on my dev box.

https://plnkr.co/edit/vLQeSnD4WCcWq4epZWmK?p=preview

Scenario: A. Press on "Open 1,2" in the home page - it will push Page1 and Page2 to the nav stack standing on Page2.

B. Press "Publish and Close" in Page 2 - it will close Page2 and publish an event which Page1 catches.

C. Page1 prints the catch to the log - ONCE.

D. Press on the back button in the nav bar to go back to the home page.

Repeat steps A,B,C,D clearing the console each time, and notice that in step C the number of prints increases by 1 each time :-)

Thanks, Eddy

efishman15 avatar Aug 17 '16 12:08 efishman15

Hey, efishman15, some of the driftyco guys will have to confirm this, but I believe this is intended behaviour.

The documentation at http://ionicframework.com/docs/v2/api/components/nav/NavController/ specifies that: By default, pages are cached and left in the DOM if they are navigated away from but still in the navigation stack (the exiting page on a push() for example). They are destroyed when removed from the navigation stack (on pop() or setRoot()).

The way I see it: Beginning: Stack only has Main page,

Step 1 - you push page A: Stack has Main page and A, with A being active on the top. As A was created in order to push it onto the stack, it fires its onViewLoaded event.

Step 2 - you push page B: Stack has Main page, A and B, with B active on the top. A fires its onViewWillLeave and onViewDidLeave events, as it is no longer active, and B fires onViewLoaded, onViewWillEnter and onViewDidEnter events as it was created and made active.

Step 4 - you pop page B: Stack has Main page and A (A active). B is no longer stored any where so it is not only inactive but destroyed, i.e. it fires onViewWillLeave, onViewDidLeave, (because it is no longer active) onViewWillUnload and onViewDidUnload (because it no longer exists).

Step 5 - you pop page A as well: Stack has only Main page. Same things happen to page A as happened to B in Step 4.

Step 6 - you push page A onto the navController again: A did not exist anywhere before, so it is created again and onViewLoaded does fire again. You end up having subscribed twice to the b:closed event. And I believe that's why you get the console.log() twice.

If what I am saying is correct, I would suggest unsubscribing from the event in onViewWillUnload.

There was some website that explained the navController events quite well, but I can't seem to find it right now.

Hope I helped,

ne6toto avatar Aug 17 '16 14:08 ne6toto

Thanks for taking a look at this @ne6toto, really appreciate it! @efishman15 what @ne6toto has found sounds and looks correct from what i can see with your plunker. Would you mind trying the solution he suggested?

jgw96 avatar Aug 17 '16 14:08 jgw96

Hi @jgw96

@ne6toto is right - I unsubscribed on the unload event and it stops "piling" and occurs only once. Just another question to clarify - my application is a tab based app in which both the Tabs container page and all the pages presented in the different tabs are subscribing to events. Do I have to unsubscribe those events also during unload - or because the Tabs container page is always on top of the stack (and never removed) and the tab pages showed in the tabs are not using nav.push but rather also appear at the root of each tab stack - are they removed at all or they are always "alive" during my app?

efishman15 avatar Aug 17 '16 16:08 efishman15

@efishman15 You should not have the same issue with your tabs page as it will never be destroyed unless you used setRoot(tabsPage) to set it as the root again from some other page. Also, partly because of this discussion we are working on implementing a new method on NavController (which should hopefully make it into beta.12) that will allow you to "set a root" but without destroying the page your setting the root too. Thanks! Also, because this issue is now fixed i will be closing this issue. Thanks again @ne6toto for taking a look at this!

jgw96 avatar Aug 17 '16 17:08 jgw96

@jgw96 I know this issue is closed but i'll be glad if you can help me! You mentioned you were working "on implementing a new method on NavController (which should hopefully make it into beta.12) that will allow us to "set a root" without destroying the page your setting the root too". I'am facing this exact problem now. I have tabs layout with a side menu and from the side menu i click (nav.setRoot) to the tabs component but it is loading and destroying the view i'm navigating to afterwards. Is there a workaround this? I could not find it in the Docs..

Actually I just found out that the ionic-conference-app (I based my application on it) was updated and it seems that this issue has been sorted!

tattivitorino avatar Jun 30 '17 15:06 tattivitorino

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Sep 02 '18 14:09 ionitron-bot[bot]