mirror of
https://github.com/RevenueCat/purchases-ios
synced 2026-11-11 22:55:55 +05:45
### 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.
66 lines
1.7 KiB
Swift
66 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
|
|
//
|
|
// CustomerInfoCallback.swift
|
|
//
|
|
// Created by Joshua Liebowitz on 11/18/21.
|
|
|
|
import Foundation
|
|
|
|
struct CustomerInfoCallback: CacheKeyProviding {
|
|
|
|
typealias Completion = (Result<CustomerInfo, BackendError>) -> Void
|
|
|
|
var cacheKey: String
|
|
var source: NetworkOperation.Type
|
|
var completion: Completion
|
|
|
|
init<T: CacheableNetworkOperation>(cacheKey: String,
|
|
source: T.Type,
|
|
completion: @escaping Completion) {
|
|
self.cacheKey = cacheKey
|
|
self.source = T.self
|
|
self.completion = completion
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - CallbackCache helpers
|
|
|
|
extension CallbackCache where T == CustomerInfoCallback {
|
|
|
|
func addOrAppendToPostReceiptDataOperation(callback: CustomerInfoCallback) -> CallbackCacheStatus {
|
|
if let existing = self.callbacks(ofType: PostReceiptDataOperation.self).last {
|
|
return self.add(callback.withNewCacheKey(existing.cacheKey))
|
|
} else {
|
|
return self.add(callback)
|
|
}
|
|
}
|
|
|
|
private func callbacks(ofType type: NetworkOperation.Type) -> [T] {
|
|
return self
|
|
.cachedCallbacksByKey
|
|
.lazy
|
|
.flatMap(\.value)
|
|
.filter { $0.source == type }
|
|
}
|
|
|
|
}
|
|
|
|
private extension CustomerInfoCallback {
|
|
|
|
func withNewCacheKey(_ newKey: String) -> Self {
|
|
var copy = self
|
|
copy.cacheKey = newKey
|
|
|
|
return copy
|
|
}
|
|
|
|
}
|