Skip to content
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

[Sessions] Migrate to GoogleUtilities's storage container #12752

Merged
merged 2 commits into from
Apr 11, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
themiswang marked this conversation as resolved.
Show resolved Hide resolved

/// 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