Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(firestore): Prevent iOS crash caused by concurrency issue #772

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/eleven-hornets-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/firestore': patch
---

fix(ios): prevent crash caused by concurrency issue
51 changes: 37 additions & 14 deletions packages/firestore/ios/Plugin/FirebaseFirestore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,32 @@ import Foundation
import FirebaseCore
import FirebaseFirestore

@objc public class FirebaseFirestore: NSObject {
private let plugin: FirebaseFirestorePlugin
private actor ListenerRegistrationMap {
private var listenerRegistrationMap: [String: ListenerRegistration] = [:]

func addRegistration(_ listenerRegistration: ListenerRegistration, listenerId: String) async {
listenerRegistrationMap[listenerId] = listenerRegistration
}

func removeRegistration(listenerId: String) async {
if let listenerRegistration = listenerRegistrationMap[listenerId] {
listenerRegistration.remove()
}
listenerRegistrationMap.removeValue(forKey: listenerId)
}

func removeAll() async {
listenerRegistrationMap.forEach { _, value in
value.remove()
}
listenerRegistrationMap.removeAll()
}
}

@objc public class FirebaseFirestore: NSObject {
private let plugin: FirebaseFirestorePlugin
private var listenerRegistrationMap = ListenerRegistrationMap()

init(plugin: FirebaseFirestorePlugin) {
self.plugin = plugin
super.init()
Expand Down Expand Up @@ -208,7 +230,9 @@ import FirebaseFirestore
completion(result, nil)
}
}
self.listenerRegistrationMap[callbackId] = listenerRegistration
Task {
await self.listenerRegistrationMap.addRegistration(listenerRegistration, listenerId: callbackId)
}
}

@objc public func addCollectionSnapshotListener(_ options: AddCollectionSnapshotListenerOptions, completion: @escaping (Result?, Error?) -> Void) {
Expand Down Expand Up @@ -241,7 +265,7 @@ import FirebaseFirestore
completion(result, nil)
}
}
self.listenerRegistrationMap[callbackId] = listenerRegistration
await listenerRegistrationMap.addRegistration(listenerRegistration, listenerId: callbackId)
} catch {
completion(nil, error)
}
Expand Down Expand Up @@ -278,26 +302,25 @@ import FirebaseFirestore
completion(result, nil)
}
}
self.listenerRegistrationMap[callbackId] = listenerRegistration
await listenerRegistrationMap.addRegistration(listenerRegistration, listenerId: callbackId)
} catch {
completion(nil, error)
}
}
}

@objc public func removeSnapshotListener(_ options: RemoveSnapshotListenerOptions) {
@objc public func removeSnapshotListener(_ options: RemoveSnapshotListenerOptions, completion: @escaping () -> Void) {
let callbackId = options.getCallbackId()

if let listenerRegistration = self.listenerRegistrationMap[callbackId] {
listenerRegistration.remove()
Task {
await listenerRegistrationMap.removeRegistration(listenerId: callbackId)
completion()
}
self.listenerRegistrationMap.removeValue(forKey: callbackId)
}

@objc public func removeAllListeners() {
for listenerRegistration in self.listenerRegistrationMap.values {
listenerRegistration.remove()
@objc public func removeAllListeners(completion: @escaping () -> Void) {
Task {
await listenerRegistrationMap.removeAll()
completion()
}
self.listenerRegistrationMap.removeAll()
}
}
13 changes: 8 additions & 5 deletions packages/firestore/ios/Plugin/FirebaseFirestorePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,20 @@ public class FirebaseFirestorePlugin: CAPPlugin {

let options = RemoveSnapshotListenerOptions(callbackId: callbackId)

implementation?.removeSnapshotListener(options)
call.resolve()
implementation?.removeSnapshotListener(options) {
call.resolve()
}
}

@objc override public func removeAllListeners(_ call: CAPPluginCall) {
for (_, savedCall) in self.pluginCallMap {
bridge?.releaseCall(savedCall)
}
self.pluginCallMap.removeAll()

implementation?.removeAllListeners()
super.removeAllListeners(call)
robingenz marked this conversation as resolved.
Show resolved Hide resolved
self.eventListeners?.removeAllObjects()
TomCarey marked this conversation as resolved.
Show resolved Hide resolved

implementation?.removeAllListeners {
call.resolve()
TomCarey marked this conversation as resolved.
Show resolved Hide resolved
}
}
}