Skip to content

Commit 19159a4

Browse files
authored
Fix tvOS storage issues. (#6658)
* Fix tvOS storage issues. On tvOS devices, the ApplicationSupport directory cannot be used. Unfortunately this doesn't come up in simulator testing, only on device. This hasn't been fully tested on device but given that any writes to the ApplicationSupport directory fail on tvOS, this change can't break much. Fixes #6612 @defagos can you please give this a test? * Fixed changed dir name for RC * Fixed test naming, too. * Fix the documentation comment for GoogleUtilities. * Updated changelog. * Review feedback
1 parent 31f978e commit 19159a4

File tree

10 files changed

+70
-31
lines changed

10 files changed

+70
-31
lines changed

FirebaseRemoteConfig/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- [fixed] Updated `numberValue` to be nonnull to align with current behavior. (#6623)
55
- [removed] Removed deprecated APIs `isDeveloperModeEnabled`, `initWithDeveloperModeEnabled:developerModeEnabled`, `activateWithCompletionHandler:completionHandler`, `activateFetched`, `configValueForKey:namespace`, `configValueForKey:namespace:source`, `allKeysFromSource:namespace`, `keysWithPrefix:namespace`, `setDefaults:namespace`, `setDefaultsFromPlistFileName:namespace`, `defaultValueForKey:namespace`. (#6637)
66
- [fixed] Completion handler for `fetchAndActivateWithCompletionHandler` is now run on the main thread. (#5897)
7+
- [fixed] Fixed database creation on tvOS. (#6612)
78

89
# v4.9.1
910
- [fixed] Fix an `attempt to insert nil object` crash in `fetchWithExpirationDuration:`. (#6522)

FirebaseRemoteConfig/Sources/RCNConfigDBManager.m

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
static BOOL gIsNewDatabase;
3434
/// SQLite file name in versions 0, 1 and 2.
3535
static NSString *const RCNDatabaseName = @"RemoteConfig.sqlite3";
36-
/// The application support sub-directory that the Remote Config database resides in.
37-
static NSString *const RCNRemoteConfigApplicationSupportSubDirectory = @"Google/RemoteConfig";
36+
/// The storage sub-directory that the Remote Config database resides in.
37+
static NSString *const RCNRemoteConfigStorageSubDirectory = @"Google/RemoteConfig";
3838

3939
/// Remote Config database path for deprecated V0 version.
4040
static NSString *RemoteConfigPathForOldDatabaseV0() {
@@ -46,11 +46,14 @@
4646

4747
/// Remote Config database path for current database.
4848
static NSString *RemoteConfigPathForDatabase(void) {
49+
#if TARGET_OS_TV
50+
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
51+
#else
4952
NSArray *dirPaths =
5053
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
51-
NSString *appSupportPath = dirPaths.firstObject;
52-
NSArray *components =
53-
@[ appSupportPath, RCNRemoteConfigApplicationSupportSubDirectory, RCNDatabaseName ];
54+
#endif
55+
NSString *storageDirPath = dirPaths.firstObject;
56+
NSArray *components = @[ storageDirPath, RCNRemoteConfigStorageSubDirectory, RCNDatabaseName ];
5457
return [NSString pathWithComponents:components];
5558
}
5659

FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
NSString *const RCNTestsDefaultFIRAppName = @"__FIRAPP_DEFAULT";
2222
NSString *const RCNTestsSecondFIRAppName = @"secondFIRApp";
2323

24-
/// The application support sub-directory that the Remote Config database resides in.
25-
static NSString *const RCNRemoteConfigApplicationSupportSubDirectory = @"Google/RemoteConfig";
24+
/// The storage sub-directory that the Remote Config database resides in.
25+
static NSString *const RCNRemoteConfigStorageSubDirectory = @"Google/RemoteConfig";
2626

2727
@implementation RCNTestUtilities
2828

@@ -39,11 +39,15 @@ + (NSString *)generatedTestAppNameForTest:(NSString *)testName {
3939

4040
/// Remote Config database path for test version
4141
+ (NSString *)remoteConfigPathForTestDatabase {
42-
NSArray<NSString *> *dirPaths =
42+
#if TARGET_OS_TV
43+
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
44+
#else
45+
NSArray *dirPaths =
4346
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
44-
NSString *appSupportPath = dirPaths.firstObject;
47+
#endif
48+
NSString *storageDirPath = dirPaths.firstObject;
4549
NSArray<NSString *> *components = @[
46-
appSupportPath, RCNRemoteConfigApplicationSupportSubDirectory,
50+
storageDirPath, RCNRemoteConfigStorageSubDirectory,
4751
[NSString stringWithFormat:@"test-%f.sqlite3", [[NSDate date] timeIntervalSince1970] * 1000]
4852
];
4953
NSString *dbPath = [NSString pathWithComponents:components];

FirebaseSegmentation/Sources/SEGDatabaseManager.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
/// SQLite file name.
2121
static NSString *const kDatabaseName = @"FirebaseSegmentation.sqlite3";
22-
/// The application support sub-directory that the Segmentation database resides in.
23-
static NSString *const kApplicationSupportSubDirectory = @"Google/FirebaseSegmentation";
22+
/// The storage sub-directory that the Segmentation database resides in.
23+
static NSString *const kSegmentationStorageSubDirectory = @"Google/FirebaseSegmentation";
2424
/// Column names
2525
static NSString *const kMainTableName = @"main";
2626
static NSString *const kMainTableColumnApplicationIdentifier = @"firebase_app_identifier";
@@ -220,11 +220,15 @@ - (NSDictionary *)loadMainTable {
220220

221221
/// Returns the current version of the Remote Config database.
222222
+ (NSString *)pathForSegmentationDatabase {
223-
NSArray<NSString *> *dirPaths =
223+
#if TARGET_OS_TV
224+
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
225+
#else
226+
NSArray *dirPaths =
224227
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
225-
NSString *appSupportPath = dirPaths.firstObject;
228+
#endif
229+
NSString *storageDir = dirPaths.firstObject;
226230
NSArray<NSString *> *components =
227-
@[ appSupportPath, kApplicationSupportSubDirectory, kDatabaseName ];
231+
@[ storageDir, kSegmentationStorageSubDirectory, kDatabaseName ];
228232
return [NSString pathWithComponents:components];
229233
}
230234

FirebaseSegmentation/Tests/Unit/SEGDatabaseManagerTests.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,16 @@ - (void)testDatabaseInsertAndRead {
105105

106106
#pragma mark Helpers
107107
- (NSString *)pathForSegmentationTestDatabase {
108+
#if TARGET_OS_TV
109+
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
110+
#else
108111
NSArray *dirPaths =
109112
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
110-
NSString *appSupportPath = dirPaths.firstObject;
113+
#endif
114+
NSString *storageDir = dirPaths.firstObject;
111115
NSString *databaseName =
112116
[NSString stringWithFormat:@"FirebaseSegmentation-test-%d.sqlite3", (arc4random() % 100)];
113-
NSArray *components = @[ appSupportPath, @"Google/FirebaseSegmentation", databaseName ];
117+
NSArray *components = @[ storageDir, @"Google/FirebaseSegmentation", databaseName ];
114118
NSString *dbPath = [NSString pathWithComponents:components];
115119
NSLog(@"Created test database at: %@", dbPath);
116120
return dbPath;

GoogleDataTransport/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22
- Legacy pre Xcode 10 compatibility checks removed. (#6486)
33
- `GDTCORDirectorySizeTracker` crash fixed. (#6540)
4+
- Fixed writing heartbeat to disk on tvOS devices. (#6658)
45

56
# v7.4.0
67
- Limit disk space consumed by GoogleDataTransport to store events. (#6365)

GoogleUtilities/Environment/GULHeartbeatDateStorage.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,17 @@ - (NSURL *)fileURL {
5353
return _fileURL;
5454
}
5555

56-
/** Returns the URL path of the Application Support folder.
57-
* @return the URL path of Application Support.
56+
/** Returns the URL path of the directory for heartbeat storage data.
57+
* @return the URL path of the directory for heartbeat storage data.
5858
*/
5959
+ (NSURL *)directoryPathURL {
60+
#if TARGET_OS_TV
61+
NSArray<NSString *> *paths =
62+
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
63+
#else
6064
NSArray<NSString *> *paths =
6165
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
66+
#endif
6267
NSArray<NSString *> *components = @[ paths.lastObject, @"Google/FIRApp" ];
6368
NSString *directoryString = [NSString pathWithComponents:components];
6469
NSURL *directoryURL = [NSURL fileURLWithPath:directoryString];

GoogleUtilities/Network/GULNetworkURLSession.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,15 @@ - (instancetype)initWithNetworkLoggerDelegate:(id<GULNetworkLoggerDelegate>)netw
6363
self = [super init];
6464
if (self) {
6565
// Create URL to the directory where all temporary files to upload have to be stored.
66+
#if TARGET_OS_TV
67+
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
68+
#else
6669
NSArray *paths =
6770
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
68-
NSString *applicationSupportDirectory = paths.firstObject;
71+
#endif
72+
NSString *storageDirectory = paths.firstObject;
6973
NSArray *tempPathComponents = @[
70-
applicationSupportDirectory, kGULNetworkApplicationSupportSubdirectory,
71-
kGULNetworkTempDirectoryName
74+
storageDirectory, kGULNetworkApplicationSupportSubdirectory, kGULNetworkTempDirectoryName
7275
];
7376
_networkDirectoryURL = [NSURL fileURLWithPathComponents:tempPathComponents];
7477
_sessionID = [NSString stringWithFormat:@"%@-%@", kGULNetworkBackgroundSessionConfigIDPrefix,

GoogleUtilities/Tests/Unit/Environment/GULHeartbeatDateStorageTest.m

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ @interface GULHeartbeatDateStorageTest : XCTestCase
2626
@implementation GULHeartbeatDateStorageTest
2727

2828
- (void)setUp {
29-
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(
30-
NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
31-
XCTAssertNotNil(documentsPath);
32-
NSURL *documentsURL = [NSURL fileURLWithPath:documentsPath];
29+
#if TARGET_OS_TV
30+
NSArray *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
31+
#else
32+
NSArray *path =
33+
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
34+
#endif
35+
NSString *rootPath = [path firstObject];
36+
XCTAssertNotNil(rootPath);
37+
NSURL *rootURL = [NSURL fileURLWithPath:rootPath];
3338

3439
NSError *error;
35-
if (![documentsURL checkResourceIsReachableAndReturnError:&error]) {
36-
XCTAssert([[NSFileManager defaultManager] createDirectoryAtURL:documentsURL
40+
if (![rootURL checkResourceIsReachableAndReturnError:&error]) {
41+
XCTAssert([[NSFileManager defaultManager] createDirectoryAtURL:rootURL
3742
withIntermediateDirectories:YES
3843
attributes:nil
3944
error:&error],
@@ -69,9 +74,14 @@ - (void)assertInitializationDoesNotAccessFileSystem {
6974
}
7075

7176
- (NSURL *)heartbeatFileURL {
72-
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(
73-
NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
74-
NSArray<NSString *> *components = @[ documentsPath, @"Google/FIRApp", kTestFileName ];
77+
#if TARGET_OS_TV
78+
NSArray *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
79+
#else
80+
NSArray *path =
81+
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
82+
#endif
83+
NSString *rootPath = [path firstObject];
84+
NSArray<NSString *> *components = @[ rootPath, @"Google/FIRApp", kTestFileName ];
7585
NSString *fileString = [NSString pathWithComponents:components];
7686
NSURL *fileURL = [NSURL fileURLWithPath:fileString];
7787
return fileURL;

GoogleUtilities/Tests/Unit/Network/GULNetworkTest.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,12 @@ - (void)testRemoveExpiredFiles {
897897

898898
GULNetworkURLSession *session = [[GULNetworkURLSession alloc]
899899
initWithNetworkLoggerDelegate:(id<GULNetworkLoggerDelegate>)_network];
900+
#if TARGET_OS_TV
901+
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
902+
#else
900903
NSArray *paths =
901904
NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
905+
#endif
902906
NSString *applicationSupportDirectory = paths.firstObject;
903907
NSArray *tempPathComponents = @[
904908
applicationSupportDirectory, kGULNetworkApplicationSupportSubdirectory,

0 commit comments

Comments
 (0)