Skip to content

Commit 840ab65

Browse files
author
Brian Chen
authored
Expose clearPersistence() publicly (#3026)
1 parent e2630d5 commit 840ab65

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

Firestore/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Unreleased
2+
- [feature] Added `clearPersistence()`, which clears the persistent storage
3+
including pending writes and cached documents. This is intended to help
4+
write reliable tests (https://github.com/firebase/firebase-js-sdk/issues/449).
25

36
# 1.3.2
47
- [fixed] Firestore should now recover its connection to the server more

Firestore/Source/API/FIRFirestore+Internal.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,6 @@ NS_ASSUME_NONNULL_BEGIN
6464
- (void)shutdownWithCompletion:(nullable void (^)(NSError *_Nullable error))completion
6565
NS_SWIFT_NAME(shutdown(completion:));
6666

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

8569
@property(nonatomic, assign, readonly) std::shared_ptr<api::Firestore> wrapped;

Firestore/Source/API/FIRFirestore.mm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ - (void)disableNetworkWithCompletion:(nullable void (^)(NSError *_Nullable))comp
267267
_firestore->DisableNetwork(util::MakeCallback(completion));
268268
}
269269

270+
- (void)clearPersistenceWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
271+
_firestore->ClearPersistence(util::MakeCallback(completion));
272+
}
273+
270274
@end
271275

272276
@implementation FIRFirestore (Internal)
@@ -287,10 +291,6 @@ + (BOOL)isLoggingEnabled {
287291
return util::LogIsLoggable(util::kLogLevelDebug);
288292
}
289293

290-
- (void)clearPersistenceWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
291-
_firestore->ClearPersistence(util::MakeCallback(completion));
292-
}
293-
294294
+ (FIRFirestore *)recoverFromFirestore:(std::shared_ptr<Firestore>)firestore {
295295
return (__bridge FIRFirestore *)firestore->extension();
296296
}

Firestore/Source/Public/FIRFirestore.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ NS_SWIFT_NAME(Firestore)
176176
*/
177177
- (void)disableNetworkWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;
178178

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

181197
NS_ASSUME_NONNULL_END

Firestore/Swift/Tests/API/BasicCompileTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func main() {
5252

5353
enableDisableNetwork(database: db)
5454

55+
clearPersistence(database: db)
56+
5557
types()
5658
}
5759

@@ -174,6 +176,15 @@ func enableDisableNetwork(database db: Firestore) {
174176
}
175177
}
176178

179+
func clearPersistence(database db: Firestore) {
180+
db.clearPersistence { error in
181+
if let e = error {
182+
print("Uh oh! \(e)")
183+
return
184+
}
185+
}
186+
}
187+
177188
func writeDocuments(at docRef: DocumentReference, database db: Firestore) {
178189
var batch: WriteBatch
179190

0 commit comments

Comments
 (0)