Files
purchases-ios/Sources/Networking/BackendConfiguration.swift
T
NachoSoto 1234c4ea91 CacheableNetworkOperation: avoid unnecessarily creating operations for cache hits (#2135)
### Changes:
- Created `CacheableNetworkOperationFactory` to abstract operation and
cache key creation
- Instead of always creating an operation, we create a factory first,
which pre-computes the cache key
- This cache key is used to determine if it's a cache hit, as well as
for the subsequently created operation

I'm still investigating a leak (#2105), and I suspect it's coming from
us creating and storing completion blocks, and those staying in memory
beyond the lifetime of the operation.
This refactor and performance improvement helps us avoid get more things
in memory than we need to.

See #2138 for how this is tested.
2022-12-21 08:13:41 -08:00

58 lines
1.7 KiB
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// BackendConfiguration.swift
//
// Created by Joshua Liebowitz on 6/13/22.
import Foundation
final class BackendConfiguration {
let httpClient: HTTPClient
let operationDispatcher: OperationDispatcher
let operationQueue: OperationQueue
let dateProvider: DateProvider
init(httpClient: HTTPClient,
operationDispatcher: OperationDispatcher,
operationQueue: OperationQueue,
dateProvider: DateProvider = DateProvider()) {
self.httpClient = httpClient
self.operationDispatcher = operationDispatcher
self.operationQueue = operationQueue
self.dateProvider = dateProvider
}
func clearCache() {
self.httpClient.clearCaches()
}
}
extension BackendConfiguration {
/// Adds the `operation` to the `OperationQueue` (based on `CallbackCacheStatus`) potentially adding a random delay.
func addCacheableOperation<T: CacheableNetworkOperation>(
with factory: CacheableNetworkOperationFactory<T>,
withRandomDelay randomDelay: Bool,
cacheStatus: CallbackCacheStatus
) {
self.operationDispatcher.dispatchOnWorkerThread(withRandomDelay: randomDelay) {
self.operationQueue.addCacheableOperation(with: factory, cacheStatus: cacheStatus)
}
}
}
// @unchecked because:
// - `OperationQueue` is not `Sendable` as of Swift 5.7
extension BackendConfiguration: @unchecked Sendable {}