Skip to content

Instantly share code, notes, and snippets.

@ricardopsantos
ricardopsantos / Article_13_G7.swift
Created January 16, 2023 14:42
Article_13_G7.swift
public typealias GenericRequestWithCacheResponse<T1: Codable, E1: Error> = AnyPublisher<T1, E1>
static func genericRequestWithCache<T1: Codable, E1: Error, T2: Codable, E2: Error>(
_ publisher: AnyPublisher<T2, E2>,
_ type: T1.Type,
_ cachePolicy: CachePolicy,
_ serviceKey: String,
_ serviceParams: [String],
_ cacheManager: CodableCacheManagerProtocol = SimpleCacheManagerForCodable.shared) -> GenericRequestWithCacheResponse<T1, E1>
@ricardopsantos
ricardopsantos / Article_13_G8.json
Created January 15, 2023 19:16
Article_13_G8.json
{"status":"success","data":[{"id":1,"employee_name":"Tiger Nixon","employee_salary":320800,"employee_age":61,"profile_image":""},{"id":2,"employee_name":"Garrett Winters","employee_salary":170750,"employee_age":63,"profile_image":""},{"id":3,"employee_name":"Ashton Cox","employee_salary":86000,"employee_age":66,"profile_image":""},{"id":4,"employee_name":"Cedric Kelly","employee_salary":433060,"employee_age":22,"profile_image":""},{"id":5,"employee_name":"Airi Satou","employee_salary":162700,"employee_age":33,"profile_image":""},{"id":6,"employee_name":"Brielle Williamson","employee_salary":372000,"employee_age":61,"profile_image":""},{"id":7,"employee_name":"Herrod Chandler","employee_salary":137500,"employee_age":59,"profile_image":""},{"id":8,"employee_name":"Rhona Davidson","employee_salary":327900,"employee_age":55,"profile_image":""},{"id":9,"employee_name":"Colleen Hurst","employee_salary":205500,"employee_age":39,"profile_image":""},{"id":10,"employee_name":"Sonya Frost","employee_salary":103600,"empl
@ricardopsantos
ricardopsantos / Article_13_G8.swift
Last active January 14, 2023 19:17
Article_13_G8.swift
public func syncRetrieve<T: Codable>(_ some: T.Type, key: String, params: [String]) -> (model: T, recordDate: Date)? {
var cachedRecords: [ExpiringCodableObjectWithKey] {
let records: [CDataExpiringKeyValueEntety] = Self.nonSecureInstanceSync.fetch()
return records .compactMap { record in
let encoding = ExpiringCodableObjectWithKey.ValueEncoding(rawValue: Int(record.encoding)) ?? .dataPlain
let model = ExpiringCodableObjectWithKey(key: record.key!,
expireDate: record.expireDate!,
object: record.object!,
objectType: record.objectType!,
encoding: encoding)
@ricardopsantos
ricardopsantos / Article_13_G7.swift
Last active January 14, 2023 19:18
Article_13_G7.swift
public typealias GenericRequestWithCacheResponse<T1: Codable, E1: Error> = AnyPublisher<T1, E1>
public extension NetworkinNameSpace.NetworkingUtils {
static func genericRequestWithCache<T1: Codable, E1: Error, T2: Codable, E2: Error>(
_ publisher: AnyPublisher<T2, E2>,
_ type: T1.Type,
_ cachePolicy: CachePolicy,
_ serviceKey: String,
_ serviceParams: [String],
_ cacheManager: CodableCacheManagerProtocol = SimpleCacheManagerForCodable.shared) -> GenericRequestWithCacheResponse<T1, E1> {
@ricardopsantos
ricardopsantos / Article_13_G6.swift
Last active January 14, 2023 19:19
Article_13_G6.swift
public class SimpleCacheManagerForCodable: CodableCacheManagerProtocol {
private init() {}
public static let shared = SimpleCacheManagerForCodable()
public func syncStore<T: Codable>(_ codable: T,
key: String,
params: [String],
timeToLiveMinutes: Int? = nil) {
let expiringCodableObjectWithKey = ExpiringCodableObjectWithKey(codable,
key: key,
@ricardopsantos
ricardopsantos / Article_13_G5.swift
Created January 14, 2023 16:31
Article_13_G5.swift
public protocol CodableCacheManagerProtocol {
func syncStore<T: Codable>(_ codable: T, key: String, params: [String], timeToLiveMinutes: Int?)
func syncRetrieve<T: Codable>(_ type: T.Type, key: String, params: [String]) -> (model: T, recordDate: Date)?
}
@ricardopsantos
ricardopsantos / Article_13_G4.swift
Last active January 14, 2023 19:07
Article_13_G4.swift
public extension ExpiringCodableObjectWithKey {
var isExpired: Bool { valueData == nil }
var valueData: Data? {
Self.referenceDate > expireDate ? object : nil
}
func extract<T: Codable>(_ some: T.Type) -> T? {
guard let data = valueData else {
return nil
}
return try? JSONDecoder().decode(T.self, from: data)
@ricardopsantos
ricardopsantos / Article_13_G3.swift
Last active January 14, 2023 14:02
Article_13_G3.swift
typealias ResponseWithCacheMaybe = AnyPublisher<ResponseDto.EmployeeServiceAvailability, AppErrors>
/// No cache API call
public func requestWithoutCache(param: String) -> ResponseWithCacheMaybe {
let requestDto = FRPSampleAPI.RequestDto.Employee(someParam: param)
let apiRequest = sampleRepository2.requestSampleJSON(requestDto)
return apiRequest.mapError({$0.toAppError}).eraseToAnyPublisher()
}
@ricardopsantos
ricardopsantos / Article_13_G2.swift
Last active January 14, 2023 16:04
Article_13_G2.swift
class ExpiringCodableObjectWithKey: Codable {
var key: String! // Cache key (built using api request name and parameters)
var object: Data! // Value to be stored
var expireDate: Date! // The limit date in witch we can retried the object
}
@ricardopsantos
ricardopsantos / Article_13_G1.swift
Last active January 14, 2023 18:54
Article_13_G1.swift
enum CacheStrategy {
case cacheNoLoad
case noCacheLoad
case cacheElseLoad
case cacheAndLoad
}