Skip to content

Commit

Permalink
Expose clearPersistence() publicly (#3026)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Chen authored Jun 5, 2019
1 parent e2630d5 commit 840ab65
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Unreleased
- [feature] Added `clearPersistence()`, which clears the persistent storage
including pending writes and cached documents. This is intended to help
write reliable tests (https://github.com/firebase/firebase-js-sdk/issues/449).

# 1.3.2
- [fixed] Firestore should now recover its connection to the server more
Expand Down
16 changes: 0 additions & 16 deletions Firestore/Source/API/FIRFirestore+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,6 @@ NS_ASSUME_NONNULL_BEGIN
- (void)shutdownWithCompletion:(nullable void (^)(NSError *_Nullable error))completion
NS_SWIFT_NAME(shutdown(completion:));

/**
* Clears the persistent storage. This includes pending writes and cached documents.
*
* Must be called while the firestore instance is not started (after the app is shutdown or when
* the app is first initialized). On startup, this method must be called before other methods
* (other than `FIRFirestore.settings`). If the firestore instance is still running, the function
* will complete with an error code of `FailedPrecondition`.
*
* Note: `clearPersistence(completion:)` is primarily intended to help write reliable tests that
* use Firestore. It uses the most efficient mechanism possible for dropping existing data but
* does not attempt to securely overwrite or otherwise make cached data unrecoverable. For
* applications that are sensitive to the disclosure of cache data in between user sessions we
* strongly recommend not to enable persistence in the first place.
*/
- (void)clearPersistenceWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;

- (const std::shared_ptr<util::AsyncQueue> &)workerQueue;

@property(nonatomic, assign, readonly) std::shared_ptr<api::Firestore> wrapped;
Expand Down
8 changes: 4 additions & 4 deletions Firestore/Source/API/FIRFirestore.mm
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ - (void)disableNetworkWithCompletion:(nullable void (^)(NSError *_Nullable))comp
_firestore->DisableNetwork(util::MakeCallback(completion));
}

- (void)clearPersistenceWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
_firestore->ClearPersistence(util::MakeCallback(completion));
}

@end

@implementation FIRFirestore (Internal)
Expand All @@ -287,10 +291,6 @@ + (BOOL)isLoggingEnabled {
return util::LogIsLoggable(util::kLogLevelDebug);
}

- (void)clearPersistenceWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
_firestore->ClearPersistence(util::MakeCallback(completion));
}

+ (FIRFirestore *)recoverFromFirestore:(std::shared_ptr<Firestore>)firestore {
return (__bridge FIRFirestore *)firestore->extension();
}
Expand Down
16 changes: 16 additions & 0 deletions Firestore/Source/Public/FIRFirestore.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ NS_SWIFT_NAME(Firestore)
*/
- (void)disableNetworkWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;

/**
* Clears the persistent storage. This includes pending writes and cached documents.
*
* Must be called while the firestore instance is not started (after the app is shutdown or when
* the app is first initialized). On startup, this method must be called before other methods
* (other than `FIRFirestore.settings`). If the firestore instance is still running, the function
* will complete with an error code of `FailedPrecondition`.
*
* Note: `clearPersistence(completion:)` is primarily intended to help write reliable tests that
* use Firestore. It uses the most efficient mechanism possible for dropping existing data but
* does not attempt to securely overwrite or otherwise make cached data unrecoverable. For
* applications that are sensitive to the disclosure of cache data in between user sessions we
* strongly recommend not to enable persistence in the first place.
*/
- (void)clearPersistenceWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;

@end

NS_ASSUME_NONNULL_END
11 changes: 11 additions & 0 deletions Firestore/Swift/Tests/API/BasicCompileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func main() {

enableDisableNetwork(database: db)

clearPersistence(database: db)

types()
}

Expand Down Expand Up @@ -174,6 +176,15 @@ func enableDisableNetwork(database db: Firestore) {
}
}

func clearPersistence(database db: Firestore) {
db.clearPersistence { error in
if let e = error {
print("Uh oh! \(e)")
return
}
}
}

func writeDocuments(at docRef: DocumentReference, database db: Firestore) {
var batch: WriteBatch

Expand Down

0 comments on commit 840ab65

Please sign in to comment.