16
16
17
17
#import " FIRTransaction.h"
18
18
19
+ #include < memory>
19
20
#include < utility>
21
+ #include < vector>
20
22
21
23
#import " Firestore/Source/API/FIRDocumentReference+Internal.h"
22
24
#import " Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
23
25
#import " Firestore/Source/API/FIRFirestore+Internal.h"
24
26
#import " Firestore/Source/API/FIRTransaction+Internal.h"
25
27
#import " Firestore/Source/API/FSTUserDataConverter.h"
26
- #import " Firestore/Source/Core/FSTTransaction.h"
27
28
#import " Firestore/Source/Model/FSTDocument.h"
28
29
#import " Firestore/Source/Util/FSTUsageValidation.h"
29
30
31
+ #include " Firestore/core/src/firebase/firestore/core/transaction.h"
32
+ #include " Firestore/core/src/firebase/firestore/util/error_apple.h"
30
33
#include " Firestore/core/src/firebase/firestore/util/hard_assert.h"
34
+ #include " Firestore/core/src/firebase/firestore/util/status.h"
31
35
32
36
using firebase::firestore::core::ParsedSetData;
33
37
using firebase::firestore::core::ParsedUpdateData;
38
+ using firebase::firestore::core::Transaction;
39
+ using firebase::firestore::util::MakeNSError;
40
+ using firebase::firestore::util::Status;
34
41
35
42
NS_ASSUME_NONNULL_BEGIN
36
43
37
44
#pragma mark - FIRTransaction
38
45
39
46
@interface FIRTransaction ()
40
47
41
- - (instancetype )initWithTransaction : (FSTTransaction * )transaction
48
+ - (instancetype )initWithTransaction : (std::shared_ptr<Transaction> )transaction
42
49
firestore : (FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;
43
50
44
- @property (nonatomic , strong , readonly ) FSTTransaction *internalTransaction;
45
51
@property (nonatomic , strong , readonly ) FIRFirestore *firestore;
46
52
@end
47
53
48
54
@implementation FIRTransaction (Internal)
49
55
50
- + (instancetype )transactionWithFSTTransaction : (FSTTransaction * )transaction
51
- firestore : (FIRFirestore *)firestore {
52
- return [[FIRTransaction alloc ] initWithTransaction: transaction firestore: firestore];
56
+ + (instancetype )transactionWithInternalTransaction : (std::shared_ptr<Transaction> )transaction
57
+ firestore : (FIRFirestore *)firestore {
58
+ return [[FIRTransaction alloc ] initWithTransaction: std: : move ( transaction) firestore: firestore];
53
59
}
54
60
55
61
@end
56
62
57
- @implementation FIRTransaction
63
+ @implementation FIRTransaction {
64
+ std::shared_ptr<Transaction> _internalTransaction;
65
+ }
58
66
59
- - (instancetype )initWithTransaction : (FSTTransaction * )transaction
67
+ - (instancetype )initWithTransaction : (std::shared_ptr<Transaction> )transaction
60
68
firestore : (FIRFirestore *)firestore {
61
69
self = [super init ];
62
70
if (self) {
63
- _internalTransaction = transaction;
71
+ _internalTransaction = std::move ( transaction) ;
64
72
_firestore = firestore;
65
73
}
66
74
return self;
@@ -77,7 +85,7 @@ - (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
77
85
[self validateReference: document];
78
86
ParsedSetData parsed = merge ? [self .firestore.dataConverter parsedMergeData: data fieldMask: nil ]
79
87
: [self .firestore.dataConverter parsedSetData: data];
80
- [ self .internalTransaction setData: std: :move (parsed) forDocument: document.key] ;
88
+ _internalTransaction-> Set (document. key , std::move (parsed)) ;
81
89
return self;
82
90
}
83
91
@@ -86,60 +94,58 @@ - (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
86
94
mergeFields : (NSArray <id> *)mergeFields {
87
95
[self validateReference: document];
88
96
ParsedSetData parsed = [self .firestore.dataConverter parsedMergeData: data fieldMask: mergeFields];
89
- [ self .internalTransaction setData: std: :move (parsed) forDocument: document.key] ;
97
+ _internalTransaction-> Set (document. key , std::move (parsed)) ;
90
98
return self;
91
99
}
92
100
93
101
- (FIRTransaction *)updateData : (NSDictionary <id, id> *)fields
94
102
forDocument : (FIRDocumentReference *)document {
95
103
[self validateReference: document];
96
104
ParsedUpdateData parsed = [self .firestore.dataConverter parsedUpdateData: fields];
97
- [ self .internalTransaction updateData: std: :move (parsed) forDocument: document.key] ;
105
+ _internalTransaction-> Update (document. key , std::move (parsed)) ;
98
106
return self;
99
107
}
100
108
101
109
- (FIRTransaction *)deleteDocument : (FIRDocumentReference *)document {
102
110
[self validateReference: document];
103
- [ self .internalTransaction deleteDocument: document.key] ;
111
+ _internalTransaction-> Delete ( document.key ) ;
104
112
return self;
105
113
}
106
114
107
115
- (void )getDocument : (FIRDocumentReference *)document
108
116
completion : (void (^)(FIRDocumentSnapshot *_Nullable document,
109
117
NSError *_Nullable error))completion {
110
118
[self validateReference: document];
111
- [self .internalTransaction
112
- lookupDocumentsForKeys: {document.key }
113
- completion: ^(NSArray <FSTMaybeDocument *> *_Nullable documents,
114
- NSError *_Nullable error) {
115
- if (error) {
116
- completion (nil , error);
117
- return ;
118
- }
119
- HARD_ASSERT (documents.count == 1 ,
120
- " Mismatch in docs returned from document lookup." );
121
- FSTMaybeDocument *internalDoc = documents.firstObject ;
122
- if ([internalDoc isKindOfClass: [FSTDeletedDocument class ]]) {
123
- FIRDocumentSnapshot *doc =
124
- [FIRDocumentSnapshot snapshotWithFirestore: self .firestore
125
- documentKey: document.key
126
- document: nil
127
- fromCache: NO
128
- hasPendingWrites: NO ];
129
- completion (doc, nil );
130
- } else if ([internalDoc isKindOfClass: [FSTDocument class ]]) {
131
- FIRDocumentSnapshot *doc =
132
- [FIRDocumentSnapshot snapshotWithFirestore: self .firestore
133
- documentKey: internalDoc.key
134
- document: (FSTDocument *)internalDoc
135
- fromCache: NO
136
- hasPendingWrites: NO ];
137
- completion (doc, nil );
138
- } else {
139
- HARD_FAIL (" BatchGetDocumentsRequest returned unexpected document type: %s" ,
140
- NSStringFromClass ([internalDoc class ]));
141
- }
142
- }];
119
+ _internalTransaction->Lookup (
120
+ {document.key }, [self , document, completion](const std::vector<FSTMaybeDocument *> &documents,
121
+ const Status &status) {
122
+ if (!status.ok ()) {
123
+ completion (nil , MakeNSError (status));
124
+ return ;
125
+ }
126
+
127
+ HARD_ASSERT (documents.size () == 1 , " Mismatch in docs returned from document lookup." );
128
+ FSTMaybeDocument *internalDoc = documents.front ();
129
+ if ([internalDoc isKindOfClass: [FSTDeletedDocument class ]]) {
130
+ FIRDocumentSnapshot *doc = [FIRDocumentSnapshot snapshotWithFirestore: self .firestore
131
+ documentKey: document.key
132
+ document: nil
133
+ fromCache: NO
134
+ hasPendingWrites: NO ];
135
+ completion (doc, nil );
136
+ } else if ([internalDoc isKindOfClass: [FSTDocument class ]]) {
137
+ FIRDocumentSnapshot *doc =
138
+ [FIRDocumentSnapshot snapshotWithFirestore: self .firestore
139
+ documentKey: internalDoc.key
140
+ document: (FSTDocument *)internalDoc
141
+ fromCache: NO
142
+ hasPendingWrites: NO ];
143
+ completion (doc, nil );
144
+ } else {
145
+ HARD_FAIL (" BatchGetDocumentsRequest returned unexpected document type: %s" ,
146
+ NSStringFromClass ([internalDoc class ]));
147
+ }
148
+ });
143
149
}
144
150
145
151
- (FIRDocumentSnapshot *_Nullable)getDocument : (FIRDocumentReference *)document
0 commit comments