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(cloud_firestore): Fix crashes on iOS/macOS #11501

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ - (NSString *)registerEventChannelWithPrefix:(NSString *)prefix
streamHandler:(NSObject<FlutterStreamHandler> *)handler;
@end

static NSMutableDictionary<NSNumber *, NSString *> *_serverTimestampMap;
static NSCache<NSNumber *, NSString *> *_serverTimestampMap;

@implementation FLTFirebaseFirestorePlugin {
NSMutableDictionary<NSString *, FlutterEventChannel *> *_eventChannels;
Expand All @@ -72,9 +72,10 @@ @implementation FLTFirebaseFirestorePlugin {
FlutterStandardMethodCodec *_codec;

+ (NSMutableDictionary<NSNumber *, NSString *> *)serverTimestampMap {
if (_serverTimestampMap == nil) {
_serverTimestampMap = [NSMutableDictionary<NSNumber *, NSString *> dictionary];
}
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_serverTimestampMap = [NSCache<NSNumber *, NSString *> new];
});
return _serverTimestampMap;
}

Expand Down Expand Up @@ -107,7 +108,6 @@ - (instancetype)init:(NSObject<FlutterBinaryMessenger> *)messenger {
_eventChannels = [NSMutableDictionary dictionary];
_streamHandlers = [NSMutableDictionary dictionary];
_transactionHandlers = [NSMutableDictionary dictionary];
_serverTimestampMap = [NSMutableDictionary dictionary];
}
return self;
}
Expand Down Expand Up @@ -470,7 +470,8 @@ - (void)documentGet:(id)arguments withMethodCallResult:(FLTFirebaseMethodCallRes
if (error != nil) {
result.error(nil, nil, nil, error);
} else {
[_serverTimestampMap setObject:serverTimestampBehaviorString forKey:@([snapshot hash])];
[FLTFirebaseFirestorePlugin.serverTimestampMap setObject:serverTimestampBehaviorString
forKey:@([snapshot hash])];
result.success(snapshot);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,12 @@ - (NSDictionary *)FIRDocumentSnapshot:(FIRDocumentSnapshot *)documentSnapshot {

NSNumber *documentSnapshotHash = @([documentSnapshot hash]);
NSString *timestampBehaviorString =
FLTFirebaseFirestorePlugin.serverTimestampMap[documentSnapshotHash];
[FLTFirebaseFirestorePlugin.serverTimestampMap objectForKey:documentSnapshotHash];

FIRServerTimestampBehavior serverTimestampBehavior =
[self toServerTimestampBehavior:timestampBehaviorString];

if (FLTFirebaseFirestorePlugin.serverTimestampMap[documentSnapshotHash] != nil) {
[FLTFirebaseFirestorePlugin.serverTimestampMap removeObjectForKey:documentSnapshotHash];
}
[FLTFirebaseFirestorePlugin.serverTimestampMap removeObjectForKey:documentSnapshotHash];

return @{
@"path" : documentSnapshot.reference.path,
Expand Down Expand Up @@ -215,14 +213,12 @@ - (NSDictionary *)FIRQuerySnapshot:(FIRQuerySnapshot *)querySnapshot {
NSMutableArray *documents = [NSMutableArray array];
NSMutableArray *metadatas = [NSMutableArray array];
NSString *timestampBehaviorString =
FLTFirebaseFirestorePlugin.serverTimestampMap[querySnapshotHash];
[FLTFirebaseFirestorePlugin.serverTimestampMap objectForKey:querySnapshotHash];

FIRServerTimestampBehavior serverTimestampBehavior =
[self toServerTimestampBehavior:timestampBehaviorString];

if (FLTFirebaseFirestorePlugin.serverTimestampMap[querySnapshotHash] != nil) {
[FLTFirebaseFirestorePlugin.serverTimestampMap removeObjectForKey:querySnapshotHash];
}
[FLTFirebaseFirestorePlugin.serverTimestampMap removeObjectForKey:querySnapshotHash];

for (FIRDocumentSnapshot *document in querySnapshot.documents) {
[paths addObject:document.reference.path];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
#import <firebase_core/FLTFirebasePlugin.h>

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