Files
firebase-ios-sdk/Firestore/Source/API/FIRTransaction.mm
T

185 lines
7.1 KiB
Plaintext
Raw Normal View History

2017-10-03 08:55:22 -07:00
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2018-09-18 09:19:11 -07:00
#import "FIRTransaction.h"
2019-02-11 19:12:52 -05:00
#include <memory>
2018-09-18 09:19:11 -07:00
#include <utility>
2019-02-11 19:12:52 -05:00
#include <vector>
2017-10-03 08:55:22 -07:00
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
#import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
#import "Firestore/Source/API/FIRFirestore+Internal.h"
2018-09-18 09:19:11 -07:00
#import "Firestore/Source/API/FIRTransaction+Internal.h"
#import "Firestore/Source/API/FSTUserDataConverter.h"
2017-10-03 08:55:22 -07:00
2019-02-11 19:12:52 -05:00
#include "Firestore/core/src/firebase/firestore/core/transaction.h"
#include "Firestore/core/src/firebase/firestore/util/error_apple.h"
#include "Firestore/core/src/firebase/firestore/util/exception.h"
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
2019-02-11 19:12:52 -05:00
#include "Firestore/core/src/firebase/firestore/util/status.h"
#include "Firestore/core/src/firebase/firestore/util/statusor.h"
2018-09-18 09:19:11 -07:00
using firebase::firestore::core::ParsedSetData;
using firebase::firestore::core::ParsedUpdateData;
2019-02-11 19:12:52 -05:00
using firebase::firestore::core::Transaction;
using firebase::firestore::model::Document;
using firebase::firestore::model::MaybeDocument;
2019-02-11 19:12:52 -05:00
using firebase::firestore::util::MakeNSError;
using firebase::firestore::util::Status;
using firebase::firestore::util::StatusOr;
using firebase::firestore::util::ThrowInvalidArgument;
2018-09-18 09:19:11 -07:00
2017-10-03 08:55:22 -07:00
NS_ASSUME_NONNULL_BEGIN
#pragma mark - FIRTransaction
@interface FIRTransaction ()
2019-02-11 19:12:52 -05:00
- (instancetype)initWithTransaction:(std::shared_ptr<Transaction>)transaction
2017-10-03 08:55:22 -07:00
firestore:(FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;
@property(nonatomic, strong, readonly) FIRFirestore *firestore;
@end
@implementation FIRTransaction (Internal)
2019-02-11 19:12:52 -05:00
+ (instancetype)transactionWithInternalTransaction:(std::shared_ptr<Transaction>)transaction
firestore:(FIRFirestore *)firestore {
return [[FIRTransaction alloc] initWithTransaction:std::move(transaction) firestore:firestore];
2017-10-03 08:55:22 -07:00
}
@end
2019-02-11 19:12:52 -05:00
@implementation FIRTransaction {
std::shared_ptr<Transaction> _internalTransaction;
}
2017-10-03 08:55:22 -07:00
2019-02-11 19:12:52 -05:00
- (instancetype)initWithTransaction:(std::shared_ptr<Transaction>)transaction
2017-10-03 08:55:22 -07:00
firestore:(FIRFirestore *)firestore {
self = [super init];
if (self) {
2019-02-11 19:12:52 -05:00
_internalTransaction = std::move(transaction);
2017-10-03 08:55:22 -07:00
_firestore = firestore;
}
return self;
}
- (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
forDocument:(FIRDocumentReference *)document {
return [self setData:data forDocument:document merge:NO];
2017-10-03 08:55:22 -07:00
}
- (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
forDocument:(FIRDocumentReference *)document
merge:(BOOL)merge {
2017-10-03 08:55:22 -07:00
[self validateReference:document];
2018-09-18 09:19:11 -07:00
ParsedSetData parsed = merge ? [self.firestore.dataConverter parsedMergeData:data fieldMask:nil]
: [self.firestore.dataConverter parsedSetData:data];
2019-02-11 19:12:52 -05:00
_internalTransaction->Set(document.key, std::move(parsed));
2018-05-02 11:10:19 -07:00
return self;
}
- (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
forDocument:(FIRDocumentReference *)document
mergeFields:(NSArray<id> *)mergeFields {
[self validateReference:document];
2018-09-18 09:19:11 -07:00
ParsedSetData parsed = [self.firestore.dataConverter parsedMergeData:data fieldMask:mergeFields];
2019-02-11 19:12:52 -05:00
_internalTransaction->Set(document.key, std::move(parsed));
2017-10-03 08:55:22 -07:00
return self;
}
- (FIRTransaction *)updateData:(NSDictionary<id, id> *)fields
forDocument:(FIRDocumentReference *)document {
[self validateReference:document];
2018-09-18 09:19:11 -07:00
ParsedUpdateData parsed = [self.firestore.dataConverter parsedUpdateData:fields];
2019-02-11 19:12:52 -05:00
_internalTransaction->Update(document.key, std::move(parsed));
2017-10-03 08:55:22 -07:00
return self;
}
- (FIRTransaction *)deleteDocument:(FIRDocumentReference *)document {
[self validateReference:document];
2019-02-11 19:12:52 -05:00
_internalTransaction->Delete(document.key);
2017-10-03 08:55:22 -07:00
return self;
}
- (void)getDocument:(FIRDocumentReference *)document
completion:(void (^)(FIRDocumentSnapshot *_Nullable document,
NSError *_Nullable error))completion {
[self validateReference:document];
2019-02-11 19:12:52 -05:00
_internalTransaction->Lookup(
{document.key},
[self, document, completion](const StatusOr<std::vector<MaybeDocument>> &maybe_documents) {
if (!maybe_documents.ok()) {
completion(nil, MakeNSError(maybe_documents.status()));
2019-02-11 19:12:52 -05:00
return;
}
const auto &documents = maybe_documents.ValueOrDie();
2019-02-11 19:12:52 -05:00
HARD_ASSERT(documents.size() == 1, "Mismatch in docs returned from document lookup.");
const MaybeDocument &internalDoc = documents.front();
if (internalDoc.is_no_document()) {
FIRDocumentSnapshot *doc =
[[FIRDocumentSnapshot alloc] initWithFirestore:self.firestore.wrapped
documentKey:document.key
document:absl::nullopt
fromCache:false
hasPendingWrites:false];
2019-02-11 19:12:52 -05:00
completion(doc, nil);
} else if (internalDoc.is_document()) {
2019-02-11 19:12:52 -05:00
FIRDocumentSnapshot *doc =
[[FIRDocumentSnapshot alloc] initWithFirestore:self.firestore.wrapped
documentKey:internalDoc.key()
document:Document(internalDoc)
fromCache:false
hasPendingWrites:false];
2019-02-11 19:12:52 -05:00
completion(doc, nil);
} else {
HARD_FAIL("BatchGetDocumentsRequest returned unexpected document type: %s",
internalDoc.type());
2019-02-11 19:12:52 -05:00
}
});
2017-10-03 08:55:22 -07:00
}
- (FIRDocumentSnapshot *_Nullable)getDocument:(FIRDocumentReference *)document
error:(NSError *__autoreleasing *)error {
[self validateReference:document];
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block FIRDocumentSnapshot *result;
// We have to explicitly assign the innerError into a local to cause it to retain correctly.
__block NSError *outerError = nil;
[self getDocument:document
completion:^(FIRDocumentSnapshot *_Nullable snapshot, NSError *_Nullable innerError) {
result = snapshot;
outerError = innerError;
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
if (error) {
*error = outerError;
}
return result;
}
- (void)validateReference:(FIRDocumentReference *)reference {
if (reference.firestore != self.firestore) {
2019-03-28 13:19:28 -07:00
ThrowInvalidArgument("Provided document reference is from a different Firestore instance.");
2017-10-03 08:55:22 -07:00
}
}
@end
NS_ASSUME_NONNULL_END