Skip to content

Commit

Permalink
[Sessions] Migrate to GoogleUtilities's storage container (#12752)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncooke3 authored Apr 11, 2024
1 parent e93e745 commit 16f64bf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion FirebaseSessions.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ Pod::Spec.new do |s|
s.dependency 'FirebaseCoreExtension', '~> 10.0'
s.dependency 'FirebaseInstallations', '~> 10.0'
s.dependency 'GoogleDataTransport', '~> 9.2'
s.dependency 'GoogleUtilities/Environment', '~> 7.10'
s.dependency 'GoogleUtilities/Environment', '~> 7.13'
s.dependency 'GoogleUtilities/UserDefaults', '~> 7.13'
s.dependency 'nanopb', '>= 2.30908.0', '< 2.30911.0'
s.dependency 'PromisesSwift', '~> 2.1'

Expand Down
20 changes: 13 additions & 7 deletions FirebaseSessions/Sources/Settings/SettingsCacheClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

import Foundation

#if SWIFT_PACKAGE
@_implementationOnly import GoogleUtilities_UserDefaults
#else
@_implementationOnly import GoogleUtilities
#endif // SWIFT_PACKAGE

/// CacheKey is like a "key" to a "safe". It provides necessary metadata about the current cache to
/// know if it should be expired.
struct CacheKey: Codable {
Expand Down Expand Up @@ -48,22 +54,22 @@ class SettingsCache: SettingsCacheClient {

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

/// Converting to dictionary is O(1) because object conversion is O(1)
var cacheContent: [String: Any] {
get {
return cache.dictionary(forKey: UserDefaultsKeys.forContent) ?? [:]
return (cache.object(forKey: UserDefaultsKeys.forContent) as? [String: Any]) ?? [:]
}
set {
cache.set(newValue, forKey: UserDefaultsKeys.forContent)
cache.setObject(newValue, forKey: UserDefaultsKeys.forContent)
}
}

/// Casting to Codable from Data is O(n)
var cacheKey: CacheKey? {
get {
if let data = cache.data(forKey: UserDefaultsKeys.forCacheKey) {
if let data = cache.object(forKey: UserDefaultsKeys.forCacheKey) as? Data {
do {
return try JSONDecoder().decode(CacheKey.self, from: data)
} catch {
Expand All @@ -74,7 +80,7 @@ class SettingsCache: SettingsCacheClient {
}
set {
do {
try cache.set(JSONEncoder().encode(newValue), forKey: UserDefaultsKeys.forCacheKey)
try cache.setObject(JSONEncoder().encode(newValue), forKey: UserDefaultsKeys.forCacheKey)
} catch {
Logger.logError("[Settings] Encoding CacheKey failed with error: \(error)")
}
Expand All @@ -83,7 +89,7 @@ class SettingsCache: SettingsCacheClient {

/// Removes stored cache
func removeCache() {
cache.set(nil, forKey: UserDefaultsKeys.forContent)
cache.set(nil, forKey: UserDefaultsKeys.forCacheKey)
cache.setObject(nil, forKey: UserDefaultsKeys.forContent)
cache.setObject(nil, forKey: UserDefaultsKeys.forCacheKey)
}
}
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ let package = Package(
.product(name: "Promises", package: "Promises"),
.product(name: "GoogleDataTransport", package: "GoogleDataTransport"),
.product(name: "GULEnvironment", package: "GoogleUtilities"),
.product(name: "GULUserDefaults", package: "GoogleUtilities"),
],
path: "FirebaseSessions/Sources",
cSettings: [
Expand Down

0 comments on commit 16f64bf

Please sign in to comment.