Skip to content

Commit

Permalink
review pass 2
Browse files Browse the repository at this point in the history
  • Loading branch information
paulb777 committed Jan 16, 2025
1 parent 4ab7446 commit 3ccecaf
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions FirebaseRemoteConfig/SwiftNew/RemoteConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,36 +241,27 @@ open class RemoteConfig: NSObject, NSFastEnumeration {
}

/// Last successful fetch completion time.
@objc public var lastFetchTime: Date? {
var fetchTime: Date?
@objc public var lastFetchTime: Date {
queue.sync {
let lastFetchTimeInterval = self.settings.lastFetchTimeInterval
if lastFetchTimeInterval > 0 {
fetchTime = Date(timeIntervalSince1970: lastFetchTimeInterval)
}
return Date(timeIntervalSince1970: lastFetchTimeInterval)
}
return fetchTime
}

/// Last fetch status. The status can be any enumerated value from `RemoteConfigFetchStatus`.
@objc public var lastFetchStatus: RemoteConfigFetchStatus {
var currentStatus: RemoteConfigFetchStatus = .noFetchYet
queue.sync {
currentStatus = self.configFetch.settings.lastFetchStatus
self.configFetch.settings.lastFetchStatus
}
return currentStatus
}

/// Config settings are custom settings.
@objc public var configSettings: RemoteConfigSettings {
get {
// These properties *must* be accessed and returned on the lock queue
// to ensure thread safety.
var minimumFetchInterval: TimeInterval = ConfigConstants.defaultMinimumFetchInterval
var fetchTimeout: TimeInterval = ConfigConstants.httpDefaultConnectionTimeout
queue.sync {
minimumFetchInterval = self.settings.minimumFetchInterval
fetchTimeout = self.settings.fetchTimeout
let (minimumFetchInterval, fetchTimeout) = queue.sync {
(self.settings.minimumFetchInterval, self.settings.fetchTimeout)
}

RCLog.debug("I-RCN000066",
Expand All @@ -279,16 +270,18 @@ open class RemoteConfig: NSObject, NSFastEnumeration {
let settings = RemoteConfigSettings()
settings.minimumFetchInterval = minimumFetchInterval
settings.fetchTimeout = fetchTimeout

/// The NSURLSession needs to be recreated whenever the fetch timeout may be updated.
configFetch.recreateNetworkSession()
RCLog.debug("I-RCN987366",
"Successfully read configSettings. Minimum Fetch Interval: " +
"\(minimumFetchInterval), Fetch timeout: \(fetchTimeout)")
return settings
}
set {
queue.async {
let configSettings = newValue
self.settings.minimumFetchInterval = configSettings.minimumFetchInterval
self.settings.fetchTimeout = configSettings.fetchTimeout
self.settings.minimumFetchInterval = newValue.minimumFetchInterval
self.settings.fetchTimeout = newValue.fetchTimeout

/// The NSURLSession needs to be recreated whenever the fetch timeout may be updated.
self.configFetch.recreateNetworkSession()
Expand Down Expand Up @@ -413,7 +406,7 @@ open class RemoteConfig: NSObject, NSFastEnumeration {
/// - Parameter completionHandler: Initialization complete callback with error parameter.
@objc public func ensureInitialized(completionHandler: @escaping (Error?) -> Void) {
DispatchQueue.global(qos: .utility).async { [weak self] in
guard let self = self else { return }
guard let self else { return }
let initializationSuccess = self.configContent.initializationSuccessful()
let error = initializationSuccess ? nil :
NSError(
Expand All @@ -435,7 +428,7 @@ open class RemoteConfig: NSObject, NSFastEnumeration {

private func callListeners(key: String, config: [String: RemoteConfigValue]) {
queue.async { [weak self] in
guard let self = self else { return }
guard let self else { return }
for listener in self.listeners {
listener(key, config)
}
Expand Down Expand Up @@ -564,7 +557,7 @@ open class RemoteConfig: NSObject, NSFastEnumeration {
((RemoteConfigFetchAndActivateStatus, Error?) -> Void)? =
nil) {
fetch { [weak self] fetchStatus, error in
guard let self = self else { return }
guard let self else { return }
// Fetch completed. We are being called on the main queue.
// If fetch is successful, try to activate the fetched config
if fetchStatus == .success, error == nil {
Expand Down Expand Up @@ -611,7 +604,7 @@ open class RemoteConfig: NSObject, NSFastEnumeration {
/// - Parameter completion Activate operation callback with changed and error parameters.
@objc public func activate(completion: ((Bool, Error?) -> Void)? = nil) {
queue.async { [weak self] in
guard let self = self else {
guard let self else {
let error = NSError(
domain: ConfigConstants.remoteConfigErrorDomain,
code: RemoteConfigError.internalError.rawValue,
Expand Down Expand Up @@ -816,7 +809,7 @@ open class RemoteConfig: NSObject, NSFastEnumeration {
let defaults = defaults ?? [String: Any]()
let fullyQualifiedNamespace = self.fullyQualifiedNamespace(FIRNamespace)
queue.async { [weak self] in
guard let self = self else { return }
guard let self else { return }

self.configContent.copy(fromDictionary: [fullyQualifiedNamespace: defaults],
toSource: .default,
Expand Down

0 comments on commit 3ccecaf

Please sign in to comment.