Skip to content

Commit 84a2a2c

Browse files
ryanwilsonncooke3
andauthored
Migrate to UserDefaults based heartbeat on tvOS (#7893)
* Migrate to UserDefaults based heartbeat on tvOS This should mitigate heartbeat files being wiped due to tvOS cache being invalidated. * Move defaults suite name to widely available constant. * Point to GULs 7.4.0+ in Package.swift Co-authored-by: Nick Cooke <[email protected]>
1 parent 429bac4 commit 84a2a2c

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

FirebaseCore/Sources/FIRApp.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@
9595
*/
9696
NSString *const kFirebaseCoreErrorDomain = @"com.firebase.core";
9797

98+
/** The NSUserDefaults suite name for FirebaseCore, for those storage locations that use it. */
99+
NSString *const kFirebaseCoreDefaultsSuiteName = @"com.firebase.core";
100+
98101
/**
99102
* The URL to download plist files.
100103
*/

FirebaseCore/Sources/FIRHeartbeatInfo.m

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
// limitations under the License.
1414

1515
#import "FirebaseCore/Sources/Private/FIRHeartbeatInfo.h"
16+
#import <GoogleUtilities/GULHeartbeatDateStorable.h>
1617
#import <GoogleUtilities/GULHeartbeatDateStorage.h>
18+
#import <GoogleUtilities/GULHeartbeatDateStorageUserDefaults.h>
1719
#import <GoogleUtilities/GULLogger.h>
20+
#import "FirebaseCore/Sources/Private/FIRAppInternal.h"
1821

1922
const static long secondsInDay = 86400;
2023
@implementation FIRHeartbeatInfo : NSObject
@@ -25,9 +28,17 @@ @implementation FIRHeartbeatInfo : NSObject
2528
*/
2629
+ (BOOL)updateIfNeededHeartbeatDateForTag:(NSString *)heartbeatTag {
2730
@synchronized(self) {
28-
NSString *const kHeartbeatStorageFile = @"HEARTBEAT_INFO_STORAGE";
29-
GULHeartbeatDateStorage *dataStorage =
30-
[[GULHeartbeatDateStorage alloc] initWithFileName:kHeartbeatStorageFile];
31+
NSString *const kHeartbeatStorageName = @"HEARTBEAT_INFO_STORAGE";
32+
id<GULHeartbeatDateStorable> dataStorage;
33+
#if TARGET_OS_TV
34+
NSUserDefaults *defaults =
35+
[[NSUserDefaults alloc] initWithSuiteName:kFirebaseCoreDefaultsSuiteName];
36+
dataStorage =
37+
[[GULHeartbeatDateStorageUserDefaults alloc] initWithDefaults:defaults
38+
key:kHeartbeatStorageName];
39+
#else
40+
dataStorage = [[GULHeartbeatDateStorage alloc] initWithFileName:kHeartbeatStorageName];
41+
#endif
3142
NSDate *heartbeatTime = [dataStorage heartbeatDateForTag:heartbeatTag];
3243
NSDate *currentDate = [NSDate date];
3344
if (heartbeatTime != nil) {

FirebaseCore/Sources/Private/FIRAppInternal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ extern NSString *const kFIRAppNameKey;
4141
extern NSString *const kFIRGoogleAppIDKey;
4242
extern NSString *const kFirebaseCoreErrorDomain;
4343

44+
/** The NSUserDefaults suite name for FirebaseCore, for those storage locations that use it. */
45+
extern NSString *const kFirebaseCoreDefaultsSuiteName;
46+
4447
/**
4548
* The format string for the User Defaults key used for storing the data collection enabled flag.
4649
* This includes formatting to append the Firebase App's name.

FirebaseCore/Tests/Unit/FIRHeartbeatInfoTest.m

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,37 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#import <GoogleUtilities/GULHeartbeatDateStorable.h>
1516
#import <GoogleUtilities/GULHeartbeatDateStorage.h>
17+
#import <GoogleUtilities/GULHeartbeatDateStorageUserDefaults.h>
1618
#import <XCTest/XCTest.h>
1719
#import "FirebaseCore/Sources/Private/FIRHeartbeatInfo.h"
1820

21+
/// Taken from the implementation of `FIRHeartbeatInfo.m`.
22+
NSString *const kFIRCoreSuiteName = @"com.firebase.core";
23+
1924
@interface FIRHeartbeatInfoTest : XCTestCase
2025

21-
@property(nonatomic, strong) GULHeartbeatDateStorage *dataStorage;
26+
@property(nonatomic, strong) id<GULHeartbeatDateStorable> dataStorage;
2227

23-
@property(nonatomic, strong) NSMutableDictionary *dictionary;
28+
#if TARGET_OS_TV
29+
@property(nonatomic, strong) NSUserDefaults *defaults;
30+
#endif // TARGET_OS_TV
2431

2532
@end
2633

2734
@implementation FIRHeartbeatInfoTest
2835

2936
- (void)setUp {
30-
NSString *const kHeartbeatStorageFile = @"HEARTBEAT_INFO_STORAGE";
31-
self.dataStorage = [[GULHeartbeatDateStorage alloc] initWithFileName:kHeartbeatStorageFile];
37+
NSString *const kHeartbeatStorageName = @"HEARTBEAT_INFO_STORAGE";
38+
#if TARGET_OS_TV
39+
self.defaults = [[NSUserDefaults alloc] initWithSuiteName:kFIRCoreSuiteName];
40+
self.dataStorage =
41+
[[GULHeartbeatDateStorageUserDefaults alloc] initWithDefaults:self.defaults
42+
key:kHeartbeatStorageName];
43+
#else
44+
self.dataStorage = [[GULHeartbeatDateStorage alloc] initWithFileName:kHeartbeatStorageName];
45+
#endif // TARGET_OS_TV
3246
NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
3347
componentsToAdd.day = -1;
3448

@@ -40,6 +54,16 @@ - (void)setUp {
4054
[self.dataStorage setHearbeatDate:dayAgo forTag:@"GLOBAL"];
4155
}
4256

57+
#if TARGET_OS_TV
58+
- (void)tearDown {
59+
// Delete any residual storage.
60+
[self.defaults removePersistentDomainForName:kFIRCoreSuiteName];
61+
self.defaults = nil;
62+
63+
[super tearDown];
64+
}
65+
#endif // TARGET_OS_TV
66+
4367
- (void)testCombinedHeartbeat {
4468
FIRHeartbeatInfoCode heartbeatCode = [FIRHeartbeatInfo heartbeatCodeForTag:@"fire-iid"];
4569
XCTAssertEqual(heartbeatCode, FIRHeartbeatInfoCodeCombined);

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ let package = Package(
150150
.package(
151151
name: "GoogleUtilities",
152152
url: "https://github.com/google/GoogleUtilities.git",
153-
"7.3.0" ..< "8.0.0"
153+
"7.4.0" ..< "8.0.0"
154154
),
155155
.package(
156156
name: "GTMSessionFetcher",

0 commit comments

Comments
 (0)