Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions ios/Sources/CapgoCameraPreviewPlugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,13 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
}

DispatchQueue.main.async {
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self,
selector: #selector(self.handleOrientationChange),
name: UIDevice.orientationDidChangeNotification,
object: nil)
if self.rotateWhenOrientationChanged == true {
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self,
selector: #selector(self.handleOrientationChange),
name: UIDevice.orientationDidChangeNotification,
object: nil)
}
self.completeStartCamera(call: call)
}
Comment on lines 781 to 790
Copy link
Contributor

@coderabbitai coderabbitai bot Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Balance beginGeneratingDeviceOrientationNotifications() / endGeneratingDeviceOrientationNotifications() now that start() is conditional.

Gating the observer fixes the reported first-start repositioning bug, but stop() (and the force cleanup) still call UIDevice.current.endGeneratingDeviceOrientationNotifications() unconditionally, which can become an unbalanced decrement and potentially interfere with other orientation-notification users in the app process.
Recommend tracking whether this plugin started generation and only ending when it did.

Proposed fix (track and conditionally end)
 public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelegate {
+    private var isGeneratingDeviceOrientationNotifications = false
     private let pluginVersion: String = "8.0.6"
     public let identifier = "CameraPreviewPlugin"
     public let jsName = "CameraPreview"
@@
                 DispatchQueue.main.async {
                     if self.rotateWhenOrientationChanged == true {
                         UIDevice.current.beginGeneratingDeviceOrientationNotifications()
+                        self.isGeneratingDeviceOrientationNotifications = true
                         NotificationCenter.default.addObserver(self,
                                                             selector: #selector(self.handleOrientationChange),
                                                             name: UIDevice.orientationDidChangeNotification,
                                                             object: nil)
                     }
                     self.completeStartCamera(call: call)
                 }
@@
             // Remove notification observers regardless
             NotificationCenter.default.removeObserver(self)
             NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
-            UIDevice.current.endGeneratingDeviceOrientationNotifications()
+            if self.isGeneratingDeviceOrientationNotifications {
+                UIDevice.current.endGeneratingDeviceOrientationNotifications()
+                self.isGeneratingDeviceOrientationNotifications = false
+            }
 // In the `force` cleanup block inside start(_:) as well:
-NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
-UIDevice.current.endGeneratingDeviceOrientationNotifications()
+NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
+if self.isGeneratingDeviceOrientationNotifications {
+    UIDevice.current.endGeneratingDeviceOrientationNotifications()
+    self.isGeneratingDeviceOrientationNotifications = false
+}
🤖 Prompt for AI Agents
In @ios/Sources/CapgoCameraPreviewPlugin/Plugin.swift around lines 781 - 790,
The code calls UIDevice.current.beginGeneratingDeviceOrientationNotifications()
only when rotateWhenOrientationChanged is true but always calls
endGeneratingDeviceOrientationNotifications() in stop()/force cleanup, risking
an unbalanced decrement; add a boolean property (e.g.
didStartOrientationNotifications or isGeneratingOrientationNotifications) to the
Plugin class, set it to true inside the DispatchQueue.main.async block when
beginGeneratingDeviceOrientationNotifications() is invoked (or when
rotateWhenOrientationChanged is true), and in stop(), force cleanup and any
other teardown paths only call
UIDevice.current.endGeneratingDeviceOrientationNotifications() if that boolean
is true and then reset it to false; reference rotateWhenOrientationChanged,
handleOrientationChange, completeStartCamera(call:), start(), stop(), and the
force cleanup paths when making the changes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riderx I apologize if this obvious, but I'm pretty new to contributing on these cap plugins. Do you want me to do what the coderabbitai is suggesting? or is that something you/maintainers decide?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

}
Expand Down