Files
purchases-ios/Sources/Networking/Operations/GetCustomerInfoOperation.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

82 lines
2.9 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
//
// GetCustomerInfoOperation.swift
//
// Created by Joshua Liebowitz on 11/18/21.
import Foundation
final class GetCustomerInfoOperation: CacheableNetworkOperation {
private let customerInfoResponseHandler: CustomerInfoResponseHandler
private let customerInfoCallbackCache: CallbackCache<CustomerInfoCallback>
private let configuration: UserSpecificConfiguration
static func createFactory(
configuration: UserSpecificConfiguration,
customerInfoResponseHandler: CustomerInfoResponseHandler = CustomerInfoResponseHandler(),
customerInfoCallbackCache: CallbackCache<CustomerInfoCallback>
) -> CacheableNetworkOperationFactory<GetCustomerInfoOperation> {
return .init({
.init(configuration: configuration,
customerInfoResponseHandler: customerInfoResponseHandler,
customerInfoCallbackCache: customerInfoCallbackCache,
cacheKey: $0) },
individualizedCacheKeyPart: configuration.appUserID
)
}
private init(
configuration: UserSpecificConfiguration,
customerInfoResponseHandler: CustomerInfoResponseHandler,
customerInfoCallbackCache: CallbackCache<CustomerInfoCallback>,
cacheKey: String
) {
self.configuration = configuration
self.customerInfoResponseHandler = customerInfoResponseHandler
self.customerInfoCallbackCache = customerInfoCallbackCache
super.init(configuration: configuration,
cacheKey: cacheKey)
}
override func begin(completion: @escaping () -> Void) {
self.getCustomerInfo(completion: completion)
}
}
private extension GetCustomerInfoOperation {
func getCustomerInfo(completion: @escaping () -> Void) {
guard let appUserID = try? configuration.appUserID.escapedOrError() else {
self.customerInfoCallbackCache.performOnAllItemsAndRemoveFromCache(withCacheable: self) { callback in
callback.completion(.failure(.missingAppUserID()))
}
completion()
return
}
let request = HTTPRequest(method: .get,
path: .getCustomerInfo(appUserID: appUserID))
httpClient.perform(request) { (response: HTTPResponse<CustomerInfoResponseHandler.Response>.Result) in
self.customerInfoCallbackCache.performOnAllItemsAndRemoveFromCache(withCacheable: self) { callback in
self.customerInfoResponseHandler.handle(customerInfoResponse: response,
completion: callback.completion)
}
completion()
}
}
}