Unable to dismiss an Immersive Space since none is opened

I'm just starting with Immersive Spaces. I am able to view one immersive space but when I tried to open a different one (after restarting the app) I got an error that only one space can be opened (at a time). I added

before the openImmersiveSpace call. Now I see a warning shown in the post title. That is Unable to dismiss an Immersive Space since none is opened

The app still works but it would be nice to detect when a Space is open so I could check and only close the 'old' space when present before opening the 'new' space.

Any suggestions? Thanks.

David