Skip to content

Commit 899ff09

Browse files
authored
refactor(firestore,apple): migrate iOS/macOS plugin implementation to Swift (#18607)
* refactor(firestore,apple): migrate iOS/macOS plugin implementation to Swift Replace the Objective-C Firestore plugin with a Swift implementation so Apple platforms can use Pigeon swiftOut and the typed pipeline Expression APIs. * format * fix(firestore,apple): address leak, retain-cycle, and parser review notes Unregister event channels when streams cancel, avoid capturing self in snapshot listeners, copy snapshot maps without an intermediate array, cap pipeline nesting, and replace forced casts in query parsing with typed errors. * format * fix(firestore,apple): catch Firestore NSExceptions while parsing queries Swift do/catch does not catch NSException, so an invalid query crashed the release app instead of returning the parse error. Aggregate group expressions now fail the pipeline instead of being dropped. * fix(firestore,apple): point the macOS resources symlink at the iOS file The Resources gitkeep had one extra parent segment, so the macOS package resource directory did not exist. * fix(firestore,apple): accept List<int> pipeline constants as bytes Dart sends a byte constant as an array of numbers. Map that array to Data and pass it to Constant, the public bytes API. * fix(firestore,apple): keep transaction results consistent across threads The handler stays alive for the whole Firestore transaction, and the result and commands are read and written under one lock.
1 parent f4c9ec1 commit 899ff09

101 files changed

Lines changed: 6319 additions & 7796 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/cloud_firestore/cloud_firestore/ios/cloud_firestore.podspec‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ Pod::Spec.new do |s|
2424
s.license = { :file => '../LICENSE' }
2525
s.authors = 'The Chromium Authors'
2626
s.source = { :path => '.' }
27-
s.source_files = 'cloud_firestore/Sources/cloud_firestore/**/*.{h,m,mm}'
28-
s.public_header_files = 'cloud_firestore/Sources/cloud_firestore/include/Public/**/*.h'
29-
s.private_header_files = 'cloud_firestore/Sources/cloud_firestore/include/Private/**/*.h'
27+
s.source_files = 'cloud_firestore/Sources/**/*.{swift,h,m,mm}'
28+
s.public_header_files = 'cloud_firestore/Sources/cloud_firestore_objc/include/*.h'
3029

30+
s.swift_version = '5.0'
3131
s.ios.deployment_target = '15.0'
3232
s.dependency 'Flutter'
3333

@@ -36,7 +36,6 @@ Pod::Spec.new do |s|
3636

3737
s.static_framework = true
3838
s.pod_target_xcconfig = {
39-
'GCC_PREPROCESSOR_DEFINITIONS' => "LIBRARY_VERSION=\\\"#{library_version}\\\" LIBRARY_NAME=\\\"flutter-fire-fst\\\"",
4039
'DEFINES_MODULE' => 'YES'
4140
}
4241
end

‎packages/cloud_firestore/cloud_firestore/ios/cloud_firestore/Package.swift‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,33 @@ let package = Package(
2424
.package(name: "FlutterFramework", path: "../FlutterFramework"),
2525
],
2626
targets: [
27+
// SPM does not allow mixing Swift and ObjC in a single target.
28+
.target(
29+
name: "cloud_firestore_objc",
30+
dependencies: [
31+
.product(name: "FirebaseFirestore", package: "firebase-ios-sdk")
32+
],
33+
path: "Sources/cloud_firestore_objc",
34+
publicHeadersPath: "include",
35+
cSettings: [
36+
.headerSearchPath("include")
37+
]
38+
),
2739
.target(
2840
name: "cloud_firestore",
2941
dependencies: [
42+
"cloud_firestore_objc",
3043
.product(name: "FirebaseFirestore", package: "firebase-ios-sdk"),
3144
.product(name: "firebase-core", package: "firebase_core"),
3245
.product(name: "FlutterFramework", package: "FlutterFramework"),
3346
],
47+
path: "Sources/cloud_firestore",
3448
resources: [
3549
.process("Resources")
3650
],
37-
cSettings: [
38-
.headerSearchPath("include/cloud_firestore/Private"),
39-
.headerSearchPath("include/cloud_firestore/Public"),
40-
.define("LIBRARY_VERSION", to: "\"\(libraryVersion)\""),
41-
.define("LIBRARY_NAME", to: "\"flutter-fire-fst\""),
42-
.define("FLT_FIREBASE_SDK_13"),
51+
swiftSettings: [
52+
.define("FLT_FIREBASE_SDK_13")
4353
]
44-
)
54+
),
4555
]
4656
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2021 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// Auto-generated file. Do not edit.
6+
public let versionNumber = "7.1.0"
7+
8+
let kFLTFirebaseFirestoreChannelName = "plugins.flutter.io/firebase_firestore"
9+
let kFLTFirebaseFirestoreQuerySnapshotEventChannelName =
10+
"plugins.flutter.io/firebase_firestore/query"
11+
let kFLTFirebaseFirestoreDocumentSnapshotEventChannelName =
12+
"plugins.flutter.io/firebase_firestore/document"
13+
let kFLTFirebaseFirestoreSnapshotsInSyncEventChannelName =
14+
"plugins.flutter.io/firebase_firestore/snapshotsInSync"
15+
let kFLTFirebaseFirestoreTransactionChannelName =
16+
"plugins.flutter.io/firebase_firestore/transaction"
17+
let kFLTFirebaseFirestoreLoadBundleChannelName =
18+
"plugins.flutter.io/firebase_firestore/loadBundle"
19+
let kFirebaseFirestoreLibraryName = "flutter-fire-fst"
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2021 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import FirebaseFirestore
6+
import Foundation
7+
8+
#if canImport(firebase_core)
9+
import firebase_core
10+
#else
11+
import firebase_core_shared
12+
#endif
13+
14+
#if os(iOS)
15+
import Flutter
16+
#elseif os(macOS)
17+
import FlutterMacOS
18+
#endif
19+
20+
final class DocumentSnapshotStreamHandler: NSObject, FlutterStreamHandler {
21+
private let firestore: Firestore
22+
private let reference: DocumentReference
23+
private let includeMetadataChanges: Bool
24+
private let serverTimestampBehavior: FirebaseFirestore.ServerTimestampBehavior
25+
private let source: FirebaseFirestore.ListenSource
26+
private var listenerRegistration: ListenerRegistration?
27+
28+
init(
29+
firestore: Firestore,
30+
reference: DocumentReference,
31+
includeMetadataChanges: Bool,
32+
serverTimestampBehavior: FirebaseFirestore.ServerTimestampBehavior,
33+
source: FirebaseFirestore.ListenSource
34+
) {
35+
self.firestore = firestore
36+
self.reference = reference
37+
self.includeMetadataChanges = includeMetadataChanges
38+
self.serverTimestampBehavior = serverTimestampBehavior
39+
self.source = source
40+
}
41+
42+
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink)
43+
-> FlutterError?
44+
{
45+
let options = SnapshotListenOptions()
46+
.withIncludeMetadataChanges(includeMetadataChanges)
47+
.withSource(source)
48+
49+
listenerRegistration = reference.addSnapshotListener(options: options) {
50+
[serverTimestampBehavior] snapshot, error in
51+
if let error {
52+
let (code, message) = FirebaseFirestoreUtils.errorCodeAndMessage(from: error)
53+
DispatchQueue.main.async {
54+
events(
55+
FLTFirebasePlugin.createFlutterError(
56+
fromCode: code,
57+
message: message,
58+
optionalDetails: ["code": code, "message": message],
59+
andOptionalNSError: error as NSError
60+
)
61+
)
62+
}
63+
} else if let snapshot {
64+
DispatchQueue.main.async {
65+
events(
66+
PigeonParser.toPigeonDocumentSnapshot(
67+
snapshot, serverTimestampBehavior: serverTimestampBehavior
68+
)
69+
)
70+
}
71+
}
72+
}
73+
return nil
74+
}
75+
76+
func onCancel(withArguments arguments: Any?) -> FlutterError? {
77+
listenerRegistration?.remove()
78+
listenerRegistration = nil
79+
return nil
80+
}
81+
}

‎packages/cloud_firestore/cloud_firestore/ios/cloud_firestore/Sources/cloud_firestore/FLTDocumentSnapshotStreamHandler.m‎

Lines changed: 0 additions & 84 deletions
This file was deleted.

‎packages/cloud_firestore/cloud_firestore/ios/cloud_firestore/Sources/cloud_firestore/FLTFirebaseFirestoreExtension.m‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)