-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
FLTQuerySnapshotStreamHandler.m
111 lines (92 loc) 路 4.19 KB
/
FLTQuerySnapshotStreamHandler.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Firebase/Firebase.h>
#import <firebase_core/FLTFirebasePluginRegistry.h>
#import "Private/FLTFirebaseFirestoreUtils.h"
#import "Private/FLTQuerySnapshotStreamHandler.h"
#import "Private/FirestorePigeonParser.h"
#import "Public/CustomPigeonHeaderFirestore.h"
@interface FLTQuerySnapshotStreamHandler ()
@property(readwrite, strong) id<FIRListenerRegistration> listenerRegistration;
@end
@implementation FLTQuerySnapshotStreamHandler
- (instancetype)initWithFirestore:(FIRFirestore *)firestore
query:(FIRQuery *)query
includeMetadataChanges:(BOOL)includeMetadataChanges
serverTimestampBehavior:(FIRServerTimestampBehavior)serverTimestampBehavior
source:(FIRListenSource)source {
self = [super init];
if (self) {
_firestore = firestore;
_query = query;
_includeMetadataChanges = includeMetadataChanges;
_serverTimestampBehavior = serverTimestampBehavior;
_source = source;
}
return self;
}
- (FlutterError *_Nullable)onListenWithArguments:(id _Nullable)arguments
eventSink:(nonnull FlutterEventSink)events {
FIRQuery *query = self.query;
if (query == nil) {
return [FlutterError
errorWithCode:@"sdk-error"
message:@"An error occurred while parsing query arguments, see native logs for more "
@"information. Please report this issue."
details:nil];
}
id listener = ^(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error) {
if (error) {
NSArray *codeAndMessage = [FLTFirebaseFirestoreUtils ErrorCodeAndMessageFromNSError:error];
NSString *code = codeAndMessage[0];
NSString *message = codeAndMessage[1];
NSDictionary *details = @{
@"code" : code,
@"message" : message,
};
dispatch_async(dispatch_get_main_queue(), ^{
events([FLTFirebasePlugin createFlutterErrorFromCode:code
message:message
optionalDetails:details
andOptionalNSError:error]);
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableArray *toListResult = [[NSMutableArray alloc] initWithCapacity:3];
NSMutableArray *documents =
[[NSMutableArray alloc] initWithCapacity:snapshot.documents.count];
NSMutableArray *documentChanges =
[[NSMutableArray alloc] initWithCapacity:snapshot.documentChanges.count];
for (FIRDocumentSnapshot *documentSnapshot in snapshot.documents) {
[documents addObject:[[FirestorePigeonParser
toPigeonDocumentSnapshot:documentSnapshot
serverTimestampBehavior:self.serverTimestampBehavior] toList]];
}
for (FIRDocumentChange *documentChange in snapshot.documentChanges) {
[documentChanges
addObject:[[FirestorePigeonParser toPigeonDocumentChange:documentChange
serverTimestampBehavior:self.serverTimestampBehavior]
toList]];
}
[toListResult addObject:documents];
[toListResult addObject:documentChanges];
[toListResult
addObject:[[FirestorePigeonParser toPigeonSnapshotMetadata:snapshot.metadata] toList]];
events(toListResult);
});
}
};
FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] init];
FIRSnapshotListenOptions *optionsWithSourceAndMetadata = [[options
optionsWithIncludeMetadataChanges:_includeMetadataChanges] optionsWithSource:_source];
self.listenerRegistration = [query addSnapshotListenerWithOptions:optionsWithSourceAndMetadata
listener:listener];
return nil;
}
- (FlutterError *_Nullable)onCancelWithArguments:(id _Nullable)arguments {
[self.listenerRegistration remove];
self.listenerRegistration = nil;
return nil;
}
@end