Skip to content

Commit 16f64bf

Browse files
authored
[Sessions] Migrate to GoogleUtilities's storage container (#12752)
1 parent e93e745 commit 16f64bf

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

FirebaseSessions.podspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ Pod::Spec.new do |s|
4343
s.dependency 'FirebaseCoreExtension', '~> 10.0'
4444
s.dependency 'FirebaseInstallations', '~> 10.0'
4545
s.dependency 'GoogleDataTransport', '~> 9.2'
46-
s.dependency 'GoogleUtilities/Environment', '~> 7.10'
46+
s.dependency 'GoogleUtilities/Environment', '~> 7.13'
47+
s.dependency 'GoogleUtilities/UserDefaults', '~> 7.13'
4748
s.dependency 'nanopb', '>= 2.30908.0', '< 2.30911.0'
4849
s.dependency 'PromisesSwift', '~> 2.1'
4950

FirebaseSessions/Sources/Settings/SettingsCacheClient.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
import Foundation
1717

18+
#if SWIFT_PACKAGE
19+
@_implementationOnly import GoogleUtilities_UserDefaults
20+
#else
21+
@_implementationOnly import GoogleUtilities
22+
#endif // SWIFT_PACKAGE
23+
1824
/// CacheKey is like a "key" to a "safe". It provides necessary metadata about the current cache to
1925
/// know if it should be expired.
2026
struct CacheKey: Codable {
@@ -48,22 +54,22 @@ class SettingsCache: SettingsCacheClient {
4854

4955
/// UserDefaults holds values in memory, making access O(1) and synchronous within the app, while
5056
/// abstracting away async disk IO.
51-
private let cache: UserDefaults = .standard
57+
private let cache: GULUserDefaults = .standard()
5258

5359
/// Converting to dictionary is O(1) because object conversion is O(1)
5460
var cacheContent: [String: Any] {
5561
get {
56-
return cache.dictionary(forKey: UserDefaultsKeys.forContent) ?? [:]
62+
return (cache.object(forKey: UserDefaultsKeys.forContent) as? [String: Any]) ?? [:]
5763
}
5864
set {
59-
cache.set(newValue, forKey: UserDefaultsKeys.forContent)
65+
cache.setObject(newValue, forKey: UserDefaultsKeys.forContent)
6066
}
6167
}
6268

6369
/// Casting to Codable from Data is O(n)
6470
var cacheKey: CacheKey? {
6571
get {
66-
if let data = cache.data(forKey: UserDefaultsKeys.forCacheKey) {
72+
if let data = cache.object(forKey: UserDefaultsKeys.forCacheKey) as? Data {
6773
do {
6874
return try JSONDecoder().decode(CacheKey.self, from: data)
6975
} catch {
@@ -74,7 +80,7 @@ class SettingsCache: SettingsCacheClient {
7480
}
7581
set {
7682
do {
77-
try cache.set(JSONEncoder().encode(newValue), forKey: UserDefaultsKeys.forCacheKey)
83+
try cache.setObject(JSONEncoder().encode(newValue), forKey: UserDefaultsKeys.forCacheKey)
7884
} catch {
7985
Logger.logError("[Settings] Encoding CacheKey failed with error: \(error)")
8086
}
@@ -83,7 +89,7 @@ class SettingsCache: SettingsCacheClient {
8389

8490
/// Removes stored cache
8591
func removeCache() {
86-
cache.set(nil, forKey: UserDefaultsKeys.forContent)
87-
cache.set(nil, forKey: UserDefaultsKeys.forCacheKey)
92+
cache.setObject(nil, forKey: UserDefaultsKeys.forContent)
93+
cache.setObject(nil, forKey: UserDefaultsKeys.forCacheKey)
8894
}
8995
}

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,7 @@ let package = Package(
10901090
.product(name: "Promises", package: "Promises"),
10911091
.product(name: "GoogleDataTransport", package: "GoogleDataTransport"),
10921092
.product(name: "GULEnvironment", package: "GoogleUtilities"),
1093+
.product(name: "GULUserDefaults", package: "GoogleUtilities"),
10931094
],
10941095
path: "FirebaseSessions/Sources",
10951096
cSettings: [

0 commit comments

Comments
 (0)