Skip to content

Delete stale Firestore instances after FIRApp is deleted. #809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Firestore/Example/Tests/API/FIRFirestoreTests.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2018 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.
*/

#import <FirebaseCore/FIRAppInternal.h>
#import <FirebaseCore/FIROptionsInternal.h>
#import <FirebaseFirestore/FIRFirestore.h>

#import <XCTest/XCTest.h>

@interface FIRFirestoreTests : XCTestCase
@end

@implementation FIRFirestoreTests

- (void)testDeleteApp {
// Create a FIRApp for testing.
NSString *appName = @"custom_app_name";
FIROptions *options =
[[FIROptions alloc] initWithGoogleAppID:@"1:123:ios:123ab" GCMSenderID:@"gcm_sender_id"];
options.projectID = @"project_id";
[FIRApp configureWithName:appName options:options];

// Ensure the app is set appropriately.
FIRApp *app = [FIRApp appNamed:appName];
FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
XCTAssertEqualObjects(firestore.app, app);

// Ensure that firestoreForApp returns the same instance.
XCTAssertEqualObjects(firestore, [FIRFirestore firestoreForApp:app]);

XCTestExpectation *defaultAppDeletedExpectation =
[self expectationWithDescription:
@"Deleting the default app should invalidate the default "
@"Firestore instance."];
[app deleteApp:^(BOOL success) {
// Recreate the FIRApp with the same name, fetch a new Firestore instance and make sure it's
// different than the other one.
[FIRApp configureWithName:appName options:options];
FIRApp *newApp = [FIRApp appNamed:appName];
FIRFirestore *newInstance = [FIRFirestore firestoreForApp:newApp];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a check that verifies [FIRFirestore firestoreForApp:app] returns the same instance above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure what you mean - do you mean call [FIRFirestore firestoreForApp:] twice and ensure the same instance is returned?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's what I mean (at line ~40). It makes it possible to know that the XCTAssertNotEqualObjects(newInstance, firestore) is meaningful here because XCTAssertEqualObjects(newInstance, firestore) was true absent this work.

XCTAssertNotEqualObjects(newInstance, firestore);

[defaultAppDeletedExpectation fulfill];
}];

[self waitForExpectations:@[ defaultAppDeletedExpectation ] timeout:2];
}

@end
35 changes: 34 additions & 1 deletion Firestore/Source/API/FIRFirestore.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#import "FIRFirestore.h"

#import <FirebaseCore/FIRApp.h>
#import <FirebaseCore/FIRAppInternal.h>
#import <FirebaseCore/FIRLogger.h>
#import <FirebaseCore/FIROptions.h>

Expand Down Expand Up @@ -80,6 +80,36 @@ @implementation FIRFirestore {
return instances;
}

+ (void)initialize {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserverForName:kFIRAppDeleteNotification
object:nil
queue:nil
usingBlock:^(NSNotification *_Nonnull note) {
NSString *appName = note.userInfo[kFIRAppNameKey];
if (appName == nil) return;

NSMutableDictionary *instances = [self instances];
@synchronized(instances) {
// Since the key for instances isn't just the app name, iterate over all the
// keys to get the one(s) we have to delete. There could be multiple in case
// the user calls firestoreForApp:database:.
NSMutableArray *keysToDelete = [[NSMutableArray alloc] init];
NSString *keyPrefix = [NSString stringWithFormat:@"%@|", appName];
for (NSString *key in instances.allKeys) {
if ([key hasPrefix:keyPrefix]) {
[keysToDelete addObject:key];
}
}

// Loop through the keys found and delete them from the stored instances.
for (NSString *key in keysToDelete) {
[instances removeObjectForKey:key];
}
}
}];
}

+ (instancetype)firestore {
FIRApp *app = [FIRApp defaultApp];
if (!app) {
Expand Down Expand Up @@ -109,6 +139,9 @@ + (instancetype)firestoreForApp:(FIRApp *)app database:(NSString *)database {
"database",
util::WrapNSStringNoCopy(DatabaseId::kDefaultDatabaseId));
}

// Note: If the key format changes, please change the code that detects FIRApps being deleted
// contained in +initialize. It checks for the app's name followed by a | character.
NSString *key = [NSString stringWithFormat:@"%@|%@", app.name, database];

NSMutableDictionary<NSString *, FIRFirestore *> *instances = self.instances;
Expand Down