'NativeScript 8.2 : [Presentation] Attempt to present UIAlertController on UIImagePickerController whose view is not in the window hierarchy

Blockquote

I am using NativeScript 8.2, I am trying to display an alert after capturing a photo in IOS.

capturePhoto(): void {
                this.photoService.requestPermissions()
                .then(result =>  camera.takePicture({
                    height: this.imageHeightWidth, width: this.imageHeightWidth,
                    keepAspectRatio: true, saveToGallery: true
                })).then(picture => { ImageSource.fromAsset(this.imageProvider.current).then((imageSource) => {
                        if(---"some condition"----) {
                            Dialogs.alert({title: "Error",message: "Error Message", okButtonText: "OK",}).then(() => {});
                            return;
                            }      
                    });
                });
            
        
    }

First time when i call the function capturePhoto() , the camera opens and after taking the picture, the alert displays fine.

But when i try it for more than once, the camera opens and i am able to take the picture but i get this error message and the alert does not display.

Not sure what i am missing and how can i show the alert every time i call the function.

 [Presentation] Attempt to present UIAlertController on  UIImagePickerController whose view is not in the window hierarchy.


Solution 1:[1]

I was able to fix the issue by Wrapping it in the setTimeout Function

                                                
                                                capturePhoto(): void {
                this.photoService.requestPermissions()
                .then(result =>  camera.takePicture({
                    height: this.imageHeightWidth, width: this.imageHeightWidth,
                    keepAspectRatio: true, saveToGallery: true
                })).then(picture => { ImageSource.fromAsset(this.imageProvider.current).then((imageSource)                      => {
                        if(---"some condition"----) {
                              setTimeout(()=>{
                                                    Dialogs.alert({
                                                        title: "Error",
                                                        message: "Error Message",
                                                        okButtonText: "OK",
                                                    }).then(() => {});
                                                },1000);
                                                
                            }      
                    });
                });
            
        
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pein