Skip to content

Commit 8a658df

Browse files
paulw11russellwheatley
authored andcommitted
fix(cloud_firestore): Fix crashes on iOS/macOS (#11501)
Co-authored-by: russellwheatley <[email protected]>
1 parent 91bab29 commit 8a658df

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

packages/cloud_firestore/cloud_firestore/ios/Classes/FLTFirebaseFirestorePlugin.m

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ - (NSString *)registerEventChannelWithPrefix:(NSString *)prefix
6060
streamHandler:(NSObject<FlutterStreamHandler> *)handler;
6161
@end
6262

63-
static NSMutableDictionary<NSNumber *, NSString *> *_serverTimestampMap;
63+
static NSCache<NSNumber *, NSString *> *_serverTimestampMap;
6464

6565
@implementation FLTFirebaseFirestorePlugin {
6666
NSMutableDictionary<NSString *, FlutterEventChannel *> *_eventChannels;
@@ -72,9 +72,10 @@ @implementation FLTFirebaseFirestorePlugin {
7272
FlutterStandardMethodCodec *_codec;
7373

7474
+ (NSMutableDictionary<NSNumber *, NSString *> *)serverTimestampMap {
75-
if (_serverTimestampMap == nil) {
76-
_serverTimestampMap = [NSMutableDictionary<NSNumber *, NSString *> dictionary];
77-
}
75+
static dispatch_once_t onceToken;
76+
dispatch_once(&onceToken, ^{
77+
_serverTimestampMap = [NSCache<NSNumber *, NSString *> new];
78+
});
7879
return _serverTimestampMap;
7980
}
8081

@@ -107,7 +108,6 @@ - (instancetype)init:(NSObject<FlutterBinaryMessenger> *)messenger {
107108
_eventChannels = [NSMutableDictionary dictionary];
108109
_streamHandlers = [NSMutableDictionary dictionary];
109110
_transactionHandlers = [NSMutableDictionary dictionary];
110-
_serverTimestampMap = [NSMutableDictionary dictionary];
111111
}
112112
return self;
113113
}
@@ -470,7 +470,8 @@ - (void)documentGet:(id)arguments withMethodCallResult:(FLTFirebaseMethodCallRes
470470
if (error != nil) {
471471
result.error(nil, nil, nil, error);
472472
} else {
473-
[_serverTimestampMap setObject:serverTimestampBehaviorString forKey:@([snapshot hash])];
473+
[FLTFirebaseFirestorePlugin.serverTimestampMap setObject:serverTimestampBehaviorString
474+
forKey:@([snapshot hash])];
474475
result.success(snapshot);
475476
}
476477
};

packages/cloud_firestore/cloud_firestore/ios/Classes/FLTFirebaseFirestoreWriter.m

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,12 @@ - (NSDictionary *)FIRDocumentSnapshot:(FIRDocumentSnapshot *)documentSnapshot {
163163

164164
NSNumber *documentSnapshotHash = @([documentSnapshot hash]);
165165
NSString *timestampBehaviorString =
166-
FLTFirebaseFirestorePlugin.serverTimestampMap[documentSnapshotHash];
166+
[FLTFirebaseFirestorePlugin.serverTimestampMap objectForKey:documentSnapshotHash];
167167

168168
FIRServerTimestampBehavior serverTimestampBehavior =
169169
[self toServerTimestampBehavior:timestampBehaviorString];
170170

171-
if (FLTFirebaseFirestorePlugin.serverTimestampMap[documentSnapshotHash] != nil) {
172-
[FLTFirebaseFirestorePlugin.serverTimestampMap removeObjectForKey:documentSnapshotHash];
173-
}
171+
[FLTFirebaseFirestorePlugin.serverTimestampMap removeObjectForKey:documentSnapshotHash];
174172

175173
return @{
176174
@"path" : documentSnapshot.reference.path,
@@ -215,14 +213,12 @@ - (NSDictionary *)FIRQuerySnapshot:(FIRQuerySnapshot *)querySnapshot {
215213
NSMutableArray *documents = [NSMutableArray array];
216214
NSMutableArray *metadatas = [NSMutableArray array];
217215
NSString *timestampBehaviorString =
218-
FLTFirebaseFirestorePlugin.serverTimestampMap[querySnapshotHash];
216+
[FLTFirebaseFirestorePlugin.serverTimestampMap objectForKey:querySnapshotHash];
219217

220218
FIRServerTimestampBehavior serverTimestampBehavior =
221219
[self toServerTimestampBehavior:timestampBehaviorString];
222220

223-
if (FLTFirebaseFirestorePlugin.serverTimestampMap[querySnapshotHash] != nil) {
224-
[FLTFirebaseFirestorePlugin.serverTimestampMap removeObjectForKey:querySnapshotHash];
225-
}
221+
[FLTFirebaseFirestorePlugin.serverTimestampMap removeObjectForKey:querySnapshotHash];
226222

227223
for (FIRDocumentSnapshot *document in querySnapshot.documents) {
228224
[paths addObject:document.reference.path];

packages/cloud_firestore/cloud_firestore/ios/Classes/Public/FLTFirebaseFirestorePlugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
#import <firebase_core/FLTFirebasePlugin.h>
1414

1515
@interface FLTFirebaseFirestorePlugin : FLTFirebasePlugin <FlutterPlugin, FLTFirebasePlugin>
16-
+ (NSMutableDictionary<NSNumber *, NSString *> *)serverTimestampMap;
16+
+ (NSCache<NSNumber *, NSString *> *)serverTimestampMap;
1717
@end

0 commit comments

Comments
 (0)