IMANUL SIDDIQUE
IMANUL SIDDIQUE

Reputation: 133

App crashes, while removing an uiview from superview, when the app comes from background to foreground in swift

I have an IBOutlet for a UIView in my iOS Swift app, and I set its frame in viewDidLoad. when the app goes to background, we are taking snapshot of gmsmapview and setting it to the uiview. However, when the app transitions from background to foreground, the view is removed from its superview, causing the app to crash. The crash occurs specifically when the view is being removed. I have ensured that the IBOutlet is properly connected and that no other part of the code is explicitly removing the view from its superview. How can I prevent the view from being removed or handle this situation to avoid the crash? This issue is only occurring on release build. Everything's working as expected on debug build. I am sharing the code section where the issue is occurring.

  @IBOutlet weak var mapViewSnapShot: UIView!
  
       NotificationCenter.default.addObserver(self, selector: #selector(MapViewController.appDidEnterBackground), name: NSNotification.Name(rawValue: "SetSnapShot"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(MapViewController.appWillEnterForeground), name: NSNotification.Name(rawValue: "RemoveSnapShot"), object: nil)

  override func viewDidLoad() {
        super.viewDidLoad()

        mapViewSnapShot.frame = CGRect(x: 0, y: 0, width: self.view.frame.width , height:     self.view.frame.height)
     }
     
  @objc func appDidEnterBackground() {
      
        setForegroundLoader()
       }

       @objc func appWillEnterForeground() {
           setMapCamera()
       }

 @objc func setForegroundLoader() {
    
        mapViewSnapShot = mapView.snapshotView(afterScreenUpdates: true)
        self.view.addSubview(mapViewSnapShot)
        self.view.bringSubviewToFront(mapViewSnapShot)
        
        }
       
       
       @objc func setMapCamera() {
           
           
         
           DispatchQueue.main.asyncAfter(deadline: .now() + 0.7) { [weak self] in
                   guard let self = self else { return }
                   if self.mapViewSnapShot.isDescendant(of: self.view) {
                       self.mapViewSnapShot.removeFromSuperview()
                   }
               
               locationManager.startUpdatingLocation()
               }

I am sharing the crash report below. enter image description here

Upvotes: 0

Views: 64

Answers (0)

Related Questions