From 3a7219832ab051f6e51c6059f266563623bb893b Mon Sep 17 00:00:00 2001 From: Daniel Bachar Date: Sat, 29 Jun 2024 08:16:09 +0300 Subject: [PATCH 1/2] Remove Stateful JSON encoding and decoding --- Airship/AirshipCore/Source/ChannelAudienceManager.swift | 6 ++---- .../AirshipCore/Source/ChannelAuthTokenAPIClient.swift | 1 - Airship/AirshipCore/Source/ContactAPIClient.swift | 8 ++++---- Airship/AirshipCore/Source/ContactChannelsAPIClient.swift | 4 ++-- Airship/AirshipCore/Source/PreferenceDataStore.swift | 7 ++----- Airship/AirshipCore/Source/RemoteDataAPIClient.swift | 4 ++-- Airship/AirshipCore/Source/RemoteDataInfo.swift | 7 ++----- Airship/AirshipCore/Source/SMSValidator.swift | 6 +++--- Airship/AirshipCore/Source/SubscriptionListAction.swift | 4 ++-- 9 files changed, 19 insertions(+), 28 deletions(-) diff --git a/Airship/AirshipCore/Source/ChannelAudienceManager.swift b/Airship/AirshipCore/Source/ChannelAudienceManager.swift index c0251c59e..8fad71c40 100644 --- a/Airship/AirshipCore/Source/ChannelAudienceManager.swift +++ b/Airship/AirshipCore/Source/ChannelAudienceManager.swift @@ -45,8 +45,6 @@ final class ChannelAudienceManager: ChannelAudienceManagerProtocol { private let audienceOverridesProvider: AudienceOverridesProvider private let date: AirshipDateProtocol - private let encoder = JSONEncoder() - private let decoder = JSONDecoder() private let updateLock = AirshipLock() private let cachedSubscriptionLists: CachedValue<[String]> @@ -409,7 +407,7 @@ final class ChannelAudienceManager: ChannelAudienceManagerProtocol { if let data = self.dataStore.data( forKey: ChannelAudienceManager.updatesKey ) { - result = try? self.decoder.decode( + result = try? JSONDecoder().decode( [AudienceUpdate].self, from: data ) @@ -420,7 +418,7 @@ final class ChannelAudienceManager: ChannelAudienceManagerProtocol { private func storeUpdates(_ operations: [AudienceUpdate]) { updateLock.sync { - if let data = try? self.encoder.encode(operations) { + if let data = try? JSONEncoder().encode(operations) { self.dataStore.setObject( data, forKey: ChannelAudienceManager.updatesKey diff --git a/Airship/AirshipCore/Source/ChannelAuthTokenAPIClient.swift b/Airship/AirshipCore/Source/ChannelAuthTokenAPIClient.swift index 3987889d5..92e901890 100644 --- a/Airship/AirshipCore/Source/ChannelAuthTokenAPIClient.swift +++ b/Airship/AirshipCore/Source/ChannelAuthTokenAPIClient.swift @@ -4,7 +4,6 @@ final class ChannelAuthTokenAPIClient: ChannelAuthTokenAPIClientProtocol, Sendab private let tokenPath = "/api/auth/device" private let config: RuntimeConfig private let session: AirshipRequestSession - private let decoder: JSONDecoder = JSONDecoder() init( config: RuntimeConfig, diff --git a/Airship/AirshipCore/Source/ContactAPIClient.swift b/Airship/AirshipCore/Source/ContactAPIClient.swift index c62dc7e58..544b19cc9 100644 --- a/Airship/AirshipCore/Source/ContactAPIClient.swift +++ b/Airship/AirshipCore/Source/ContactAPIClient.swift @@ -72,7 +72,7 @@ final class ContactAPIClient: ContactsAPIClientProtocol { private let config: RuntimeConfig private let session: AirshipRequestSession - private let decoder: JSONDecoder = { + private var decoder: JSONDecoder { let decoder = JSONDecoder() decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in let container = try decoder.singleValueContainer() @@ -84,9 +84,9 @@ final class ContactAPIClient: ContactsAPIClientProtocol { return date }) return decoder - }() + } - private let encoder: JSONEncoder = { + private var encoder: JSONEncoder { let encoder = JSONEncoder() encoder.dateEncodingStrategy = .custom({ date, encoder in var container = encoder.singleValueContainer() @@ -95,7 +95,7 @@ final class ContactAPIClient: ContactsAPIClientProtocol { ) }) return encoder - }() + } init(config: RuntimeConfig, session: AirshipRequestSession) { self.config = config diff --git a/Airship/AirshipCore/Source/ContactChannelsAPIClient.swift b/Airship/AirshipCore/Source/ContactChannelsAPIClient.swift index d41625b4f..30534fec9 100644 --- a/Airship/AirshipCore/Source/ContactChannelsAPIClient.swift +++ b/Airship/AirshipCore/Source/ContactChannelsAPIClient.swift @@ -14,7 +14,7 @@ final class ContactChannelsAPIClient: ContactChannelsAPIClientProtocol { private let config: RuntimeConfig private let session: AirshipRequestSession - private let decoder: JSONDecoder = { + private var decoder: JSONDecoder { let decoder = JSONDecoder() decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in let container = try decoder.singleValueContainer() @@ -26,7 +26,7 @@ final class ContactChannelsAPIClient: ContactChannelsAPIClientProtocol { return date }) return decoder - }() + } init(config: RuntimeConfig, session: AirshipRequestSession) { self.config = config diff --git a/Airship/AirshipCore/Source/PreferenceDataStore.swift b/Airship/AirshipCore/Source/PreferenceDataStore.swift index bd735a74b..00510f197 100644 --- a/Airship/AirshipCore/Source/PreferenceDataStore.swift +++ b/Airship/AirshipCore/Source/PreferenceDataStore.swift @@ -8,9 +8,6 @@ public final class PreferenceDataStore: @unchecked Sendable { private let defaults: UserDefaults private let appKey: String static let deviceIDKey = "deviceID" - - private let decoder = JSONDecoder() - private let encoder = JSONEncoder() private var pending: [String: [Any?]] = [:] private var cache: [String: Cached] = [:] @@ -177,7 +174,7 @@ public final class PreferenceDataStore: @unchecked Sendable { return nil } - return try decoder.decode(T.self, from: data) + return try JSONDecoder().decode(T.self, from: data) } public func safeCodable(forKey key: String) -> T? { @@ -209,7 +206,7 @@ public final class PreferenceDataStore: @unchecked Sendable { return } - let data = try encoder.encode(codable) + let data = try JSONEncoder().encode(codable) write(key, value: data) } diff --git a/Airship/AirshipCore/Source/RemoteDataAPIClient.swift b/Airship/AirshipCore/Source/RemoteDataAPIClient.swift index 574e84037..700c6681b 100644 --- a/Airship/AirshipCore/Source/RemoteDataAPIClient.swift +++ b/Airship/AirshipCore/Source/RemoteDataAPIClient.swift @@ -13,7 +13,7 @@ final class RemoteDataAPIClient: RemoteDataAPIClientProtocol { private let session: AirshipRequestSession private let config: RuntimeConfig - private let decoder: JSONDecoder = { + private var decoder: JSONDecoder { let decoder = JSONDecoder() decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in let container = try decoder.singleValueContainer() @@ -25,7 +25,7 @@ final class RemoteDataAPIClient: RemoteDataAPIClientProtocol { return date }) return decoder - }() + } init(config: RuntimeConfig, session: AirshipRequestSession) { self.config = config diff --git a/Airship/AirshipCore/Source/RemoteDataInfo.swift b/Airship/AirshipCore/Source/RemoteDataInfo.swift index 492032d84..37dcdf8ee 100644 --- a/Airship/AirshipCore/Source/RemoteDataInfo.swift +++ b/Airship/AirshipCore/Source/RemoteDataInfo.swift @@ -14,14 +14,11 @@ public struct RemoteDataInfo: Sendable, Codable, Equatable, Hashable { self.contactID = contactID } - private static let decoder = JSONDecoder() - private static let encoder = JSONEncoder() - static func fromJSON(data: Data) throws -> RemoteDataInfo { - return try RemoteDataInfo.decoder.decode(RemoteDataInfo.self, from: data) + try JSONDecoder().decode(RemoteDataInfo.self, from: data) } func toEncodedJSONData() throws -> Data { - return try RemoteDataInfo.encoder.encode(self) + try JSONEncoder().encode(self) } } diff --git a/Airship/AirshipCore/Source/SMSValidator.swift b/Airship/AirshipCore/Source/SMSValidator.swift index def1931cc..7fa334260 100644 --- a/Airship/AirshipCore/Source/SMSValidator.swift +++ b/Airship/AirshipCore/Source/SMSValidator.swift @@ -92,7 +92,7 @@ final class SMSValidatorAPIClient: SMSValidatorAPIClientProtocol { private let config: RuntimeConfig private let session: AirshipRequestSession - private let decoder: JSONDecoder = { + private var decoder: JSONDecoder { let decoder = JSONDecoder() decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in let container = try decoder.singleValueContainer() @@ -104,9 +104,9 @@ final class SMSValidatorAPIClient: SMSValidatorAPIClientProtocol { return date }) return decoder - }() + } - private let encoder: JSONEncoder = { + private var encoder: JSONEncoder = { let encoder = JSONEncoder() encoder.dateEncodingStrategy = .custom({ date, encoder in var container = encoder.singleValueContainer() diff --git a/Airship/AirshipCore/Source/SubscriptionListAction.swift b/Airship/AirshipCore/Source/SubscriptionListAction.swift index f190ba1ca..571d14633 100644 --- a/Airship/AirshipCore/Source/SubscriptionListAction.swift +++ b/Airship/AirshipCore/Source/SubscriptionListAction.swift @@ -23,11 +23,11 @@ public final class SubscriptionListAction: AirshipAction { private let channel: @Sendable () -> AirshipChannelProtocol private let contact: @Sendable () -> AirshipContactProtocol - private let decoder: JSONDecoder = { + private var decoder: JSONDecoder { let decoder = JSONDecoder() decoder.dateDecodingStrategy = .iso8601 return decoder - }() + } public var _decoder: JSONDecoder { return decoder From a9ba0e3f41109153f9cebf4d7f585f29ff72c6b0 Mon Sep 17 00:00:00 2001 From: Daniel Bachar Date: Wed, 24 Jul 2024 11:30:07 -0700 Subject: [PATCH 2/2] warning fix --- Airship/AirshipCore/Source/SMSValidator.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Airship/AirshipCore/Source/SMSValidator.swift b/Airship/AirshipCore/Source/SMSValidator.swift index 7fa334260..a81e9b897 100644 --- a/Airship/AirshipCore/Source/SMSValidator.swift +++ b/Airship/AirshipCore/Source/SMSValidator.swift @@ -106,7 +106,7 @@ final class SMSValidatorAPIClient: SMSValidatorAPIClientProtocol { return decoder } - private var encoder: JSONEncoder = { + private var encoder: JSONEncoder { let encoder = JSONEncoder() encoder.dateEncodingStrategy = .custom({ date, encoder in var container = encoder.singleValueContainer() @@ -115,7 +115,7 @@ final class SMSValidatorAPIClient: SMSValidatorAPIClientProtocol { ) }) return encoder - }() + } init(config: RuntimeConfig, session: AirshipRequestSession) { self.config = config