diff --git a/Firestore/Example/SwiftBuildTest/main.swift b/Firestore/Example/SwiftBuildTest/main.swift index 2d77669f265..c74c3d8b7b3 100644 --- a/Firestore/Example/SwiftBuildTest/main.swift +++ b/Firestore/Example/SwiftBuildTest/main.swift @@ -228,11 +228,11 @@ func readDocument(at docRef: DocumentReference) { func readDocumentWithOptions(at docRef: DocumentReference) { docRef.getDocument(options:GetOptions.defaultOptions()) { document, error in } - docRef.getDocument(options:GetOptions(source:GetSource.default)) { document, error in + docRef.getDocument(options:GetOptions.source(GetSource.default)) { document, error in } - docRef.getDocument(options:GetOptions(source:.server)) { document, error in + docRef.getDocument(options:GetOptions.source(.server)) { document, error in } - docRef.getDocument(options:GetOptions(source:GetSource.cache)) { document, error in + docRef.getDocument(options:GetOptions.source(GetSource.cache)) { document, error in } } @@ -249,11 +249,11 @@ func readDocuments(matching query: Query) { func readDocumentsWithOptions(matching query: Query) { query.getDocuments(options:GetOptions.defaultOptions()) { querySnapshot, error in } - query.getDocuments(options:GetOptions.init(source:GetSource.default)) { querySnapshot, error in + query.getDocuments(options:GetOptions.source(GetSource.default)) { querySnapshot, error in } - query.getDocuments(options:GetOptions.init(source:GetSource.server)) { querySnapshot, error in + query.getDocuments(options:GetOptions.source(.server)) { querySnapshot, error in } - query.getDocuments(options:GetOptions.init(source:GetSource.cache)) { querySnapshot, error in + query.getDocuments(options:GetOptions.source(GetSource.cache)) { querySnapshot, error in } } diff --git a/Firestore/Example/Tests/Integration/API/FIRGetOptionsTests.m b/Firestore/Example/Tests/Integration/API/FIRGetOptionsTests.m index 628a6dcb2d8..cf77d4ab0c5 100644 --- a/Firestore/Example/Tests/Integration/API/FIRGetOptionsTests.m +++ b/Firestore/Example/Tests/Integration/API/FIRGetOptionsTests.m @@ -145,8 +145,7 @@ - (void)testGetDocumentWhileOnlineCacheOnly { // get doc and ensure that it exists, *is* from the cache, and matches // the initialData. FIRDocumentSnapshot *result = - [self readDocumentForRef:doc - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]]; + [self readDocumentForRef:doc options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]]; XCTAssertTrue(result.exists); XCTAssertTrue(result.metadata.fromCache); XCTAssertFalse(result.metadata.hasPendingWrites); @@ -167,8 +166,7 @@ - (void)testGetCollectionWhileOnlineCacheOnly { // get docs and ensure they *are* from the cache, and matches the // initialDocs. FIRQuerySnapshot *result = - [self readDocumentSetForRef:col - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]]; + [self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]]; XCTAssertTrue(result.metadata.fromCache); XCTAssertFalse(result.metadata.hasPendingWrites); XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[ @@ -205,8 +203,7 @@ - (void)testGetDocumentWhileOfflineCacheOnly { // get doc and ensure it exists, *is* from the cache, and matches the // newData. FIRDocumentSnapshot *result = - [self readDocumentForRef:doc - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]]; + [self readDocumentForRef:doc options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]]; XCTAssertTrue(result.exists); XCTAssertTrue(result.metadata.fromCache); XCTAssertTrue(result.metadata.hasPendingWrites); @@ -237,8 +234,7 @@ - (void)testGetCollectionWhileOfflineCacheOnly { // get docs and ensure they *are* from the cache, and matches the updated // data. FIRQuerySnapshot *result = - [self readDocumentSetForRef:col - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]]; + [self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]]; XCTAssertTrue(result.metadata.fromCache); XCTAssertTrue(result.metadata.hasPendingWrites); XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[ @@ -264,8 +260,7 @@ - (void)testGetDocumentWhileOnlineServerOnly { // get doc and ensure that it exists, is *not* from the cache, and matches // the initialData. FIRDocumentSnapshot *result = - [self readDocumentForRef:doc - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]]; + [self readDocumentForRef:doc options:[FIRGetOptions optionsWithSource:FIRGetSourceServer]]; XCTAssertTrue(result.exists); XCTAssertFalse(result.metadata.fromCache); XCTAssertFalse(result.metadata.hasPendingWrites); @@ -286,8 +281,7 @@ - (void)testGetCollectionWhileOnlineServerOnly { // get docs and ensure they are *not* from the cache, and matches the // initialData. FIRQuerySnapshot *result = - [self readDocumentSetForRef:col - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]]; + [self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceServer]]; XCTAssertFalse(result.metadata.fromCache); XCTAssertFalse(result.metadata.hasPendingWrites); XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[ @@ -314,7 +308,7 @@ - (void)testGetDocumentWhileOfflineServerOnly { // attempt to get doc and ensure it cannot be retreived XCTestExpectation *failedGetDocCompletion = [self expectationWithDescription:@"failedGetDoc"]; - [doc getDocumentWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer] + [doc getDocumentWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceServer] completion:^(FIRDocumentSnapshot *snapshot, NSError *error) { XCTAssertNotNil(error); XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain); @@ -340,7 +334,7 @@ - (void)testGetCollectionWhileOfflineServerOnly { // attempt to get docs and ensure they cannot be retreived XCTestExpectation *failedGetDocsCompletion = [self expectationWithDescription:@"failedGetDocs"]; - [col getDocumentsWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer] + [col getDocumentsWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceServer] completion:^(FIRQuerySnapshot *snapshot, NSError *error) { XCTAssertNotNil(error); XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain); @@ -380,16 +374,15 @@ - (void)testGetDocumentWhileOfflineWithDifferentGetOptions { // get doc (from cache) and ensure it exists, *is* from the cache, and // matches the newData. FIRDocumentSnapshot *result = - [self readDocumentForRef:doc - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]]; + [self readDocumentForRef:doc options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]]; XCTAssertTrue(result.exists); XCTAssertTrue(result.metadata.fromCache); XCTAssertTrue(result.metadata.hasPendingWrites); XCTAssertEqualObjects(result.data, newData); // attempt to get doc (with default get options) - result = [self readDocumentForRef:doc - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceDefault]]; + result = + [self readDocumentForRef:doc options:[FIRGetOptions optionsWithSource:FIRGetSourceDefault]]; XCTAssertTrue(result.exists); XCTAssertTrue(result.metadata.fromCache); XCTAssertTrue(result.metadata.hasPendingWrites); @@ -397,7 +390,7 @@ - (void)testGetDocumentWhileOfflineWithDifferentGetOptions { // attempt to get doc (from the server) and ensure it cannot be retreived XCTestExpectation *failedGetDocCompletion = [self expectationWithDescription:@"failedGetDoc"]; - [doc getDocumentWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer] + [doc getDocumentWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceServer] completion:^(FIRDocumentSnapshot *snapshot, NSError *error) { XCTAssertNotNil(error); XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain); @@ -440,8 +433,7 @@ - (void)testGetCollectionWhileOfflineWithDifferentGetOptions { // get docs (from cache) and ensure they *are* from the cache, and // matches the updated data. FIRQuerySnapshot *result = - [self readDocumentSetForRef:col - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]]; + [self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]]; XCTAssertTrue(result.metadata.fromCache); XCTAssertTrue(result.metadata.hasPendingWrites); XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[ @@ -458,7 +450,7 @@ - (void)testGetCollectionWhileOfflineWithDifferentGetOptions { // attempt to get docs (with default get options) result = [self readDocumentSetForRef:col - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceDefault]]; + options:[FIRGetOptions optionsWithSource:FIRGetSourceDefault]]; XCTAssertTrue(result.metadata.fromCache); XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[ @{@"key1" : @"value1"}, @{@"key2" : @"value2", @"key2b" : @"value2b"}, @@ -474,7 +466,7 @@ - (void)testGetCollectionWhileOfflineWithDifferentGetOptions { // attempt to get docs (from the server) and ensure they cannot be retreived XCTestExpectation *failedGetDocsCompletion = [self expectationWithDescription:@"failedGetDocs"]; - [col getDocumentsWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer] + [col getDocumentsWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceServer] completion:^(FIRQuerySnapshot *snapshot, NSError *error) { XCTAssertNotNil(error); XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain); @@ -547,7 +539,7 @@ - (void)testGetNonExistingDocWhileOnlineCacheOnly { // certain documents *don't* exist. XCTestExpectation *getNonExistingDocCompletion = [self expectationWithDescription:@"getNonExistingDoc"]; - [doc getDocumentWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache] + [doc getDocumentWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceCache] completion:^(FIRDocumentSnapshot *snapshot, NSError *error) { XCTAssertNotNil(error); XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain); @@ -562,8 +554,7 @@ - (void)testGetNonExistingCollectionWhileOnlineCacheOnly { // get collection and ensure it's empty and that it *is* from the cache. FIRQuerySnapshot *snapshot = - [self readDocumentSetForRef:col - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]]; + [self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]]; XCTAssertEqual(snapshot.count, 0); XCTAssertEqual(snapshot.documentChanges.count, 0); XCTAssertTrue(snapshot.metadata.fromCache); @@ -581,7 +572,7 @@ - (void)testGetNonExistingDocWhileOfflineCacheOnly { // certain documents *don't* exist. XCTestExpectation *getNonExistingDocCompletion = [self expectationWithDescription:@"getNonExistingDoc"]; - [doc getDocumentWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache] + [doc getDocumentWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceCache] completion:^(FIRDocumentSnapshot *snapshot, NSError *error) { XCTAssertNotNil(error); XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain); @@ -599,8 +590,7 @@ - (void)testGetNonExistingCollectionWhileOfflineCacheOnly { // get collection and ensure it's empty and that it *is* from the cache. FIRQuerySnapshot *snapshot = - [self readDocumentSetForRef:col - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]]; + [self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]]; XCTAssertEqual(snapshot.count, 0); XCTAssertEqual(snapshot.documentChanges.count, 0); XCTAssertTrue(snapshot.metadata.fromCache); @@ -612,8 +602,7 @@ - (void)testGetNonExistingDocWhileOnlineServerOnly { // get doc and ensure that it does not exist and is *not* from the cache. FIRDocumentSnapshot *snapshot = - [self readDocumentForRef:doc - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]]; + [self readDocumentForRef:doc options:[FIRGetOptions optionsWithSource:FIRGetSourceServer]]; XCTAssertFalse(snapshot.exists); XCTAssertFalse(snapshot.metadata.fromCache); XCTAssertFalse(snapshot.metadata.hasPendingWrites); @@ -624,8 +613,7 @@ - (void)testGetNonExistingCollectionWhileOnlineServerOnly { // get collection and ensure that it's empty and that it's *not* from the cache. FIRQuerySnapshot *snapshot = - [self readDocumentSetForRef:col - options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]]; + [self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceServer]]; XCTAssertEqual(snapshot.count, 0); XCTAssertEqual(snapshot.documentChanges.count, 0); XCTAssertFalse(snapshot.metadata.fromCache); @@ -643,7 +631,7 @@ - (void)testGetNonExistingDocWhileOfflineServerOnly { // certain documents *don't* exist. XCTestExpectation *getNonExistingDocCompletion = [self expectationWithDescription:@"getNonExistingDoc"]; - [doc getDocumentWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer] + [doc getDocumentWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceServer] completion:^(FIRDocumentSnapshot *snapshot, NSError *error) { XCTAssertNotNil(error); XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain); @@ -661,7 +649,7 @@ - (void)testGetNonExistingCollectionWhileOfflineServerOnly { // attempt to get collection and ensure that it cannot be retreived XCTestExpectation *failedGetDocsCompletion = [self expectationWithDescription:@"failedGetDocs"]; - [col getDocumentsWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer] + [col getDocumentsWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceServer] completion:^(FIRQuerySnapshot *snapshot, NSError *error) { XCTAssertNotNil(error); XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain); diff --git a/Firestore/Source/API/FIRGetOptions+Internal.h b/Firestore/Source/API/FIRGetOptions+Internal.h index 1e8a479c837..da3bdc9de6b 100644 --- a/Firestore/Source/API/FIRGetOptions+Internal.h +++ b/Firestore/Source/API/FIRGetOptions+Internal.h @@ -23,6 +23,8 @@ NS_ASSUME_NONNULL_BEGIN /** Where getDocument[s] calls should get their data from. */ @property(nonatomic, readonly, getter=source) FIRGetSource source; +- (instancetype)initWithSource:(FIRGetSource)source; + @end NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/API/FIRGetOptions.m b/Firestore/Source/API/FIRGetOptions.m index b7eb5e2ece9..e43f25f837b 100644 --- a/Firestore/Source/API/FIRGetOptions.m +++ b/Firestore/Source/API/FIRGetOptions.m @@ -20,7 +20,7 @@ @implementation FIRGetOptions -+ (FIRGetOptions *)defaultOptions { ++ (instancetype)defaultOptions { return [[FIRGetOptions alloc] initWithSource:FIRGetSourceDefault]; } @@ -31,6 +31,10 @@ - (instancetype)initWithSource:(FIRGetSource)source { return self; } ++ (instancetype)optionsWithSource:(FIRGetSource)source { + return [[FIRGetOptions alloc] initWithSource:source]; +} + @end NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Public/FIRGetOptions.h b/Firestore/Source/Public/FIRGetOptions.h index 0b7412191d9..de283255ecf 100644 --- a/Firestore/Source/Public/FIRGetOptions.h +++ b/Firestore/Source/Public/FIRGetOptions.h @@ -28,13 +28,16 @@ NS_ASSUME_NONNULL_BEGIN NS_SWIFT_NAME(GetOptions) @interface FIRGetOptions : NSObject +/** */ +- (instancetype)init __attribute((unavailable("FIRGetOptions cannot be created directly."))); + /** * Returns the default options. * - * Equiavlent to `[[FIRGetOptions alloc] initWithSource:FIRGetSourceDefault]` in + * Equiavlent to `[FIRGetOptions source:FIRGetSourceDefault]` in * objective-c. */ -+ (FIRGetOptions *)defaultOptions NS_SWIFT_NAME(defaultOptions()); ++ (instancetype)defaultOptions NS_SWIFT_NAME(defaultOptions()); /** * Describes whether we should get from server or cache. @@ -62,7 +65,7 @@ typedef NS_ENUM(NSUInteger, FIRGetSource) { /** * Initializes the get options with the specified source. */ -- (instancetype)initWithSource:(FIRGetSource)source NS_SWIFT_NAME(init(source:)); ++ (instancetype)optionsWithSource:(FIRGetSource)source NS_SWIFT_NAME(source(_:)); @end