================================================
FILE: Example/CarlosWatchSample/Info.plist
================================================
CFBundleDevelopmentRegionenCFBundleDisplayNameCarlosSampleCFBundleExecutable$(EXECUTABLE_NAME)CFBundleIdentifier$(PRODUCT_BUNDLE_IDENTIFIER)CFBundleInfoDictionaryVersion6.0CFBundleName$(PRODUCT_NAME)CFBundlePackageTypeAPPLCFBundleShortVersionString1.0CFBundleSignature????CFBundleVersion1UISupportedInterfaceOrientationsUIInterfaceOrientationPortraitUIInterfaceOrientationPortraitUpsideDownWKCompanionAppBundleIdentifierde.axelspringer.CarlosSampleWKWatchKitApp
================================================
FILE: Example/CarlosWatchSample Extension/Assets.xcassets/Contents.json
================================================
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
================================================
FILE: Example/CarlosWatchSample Extension/Assets.xcassets/placeholder.imageset/Contents.json
================================================
{
"images" : [
{
"idiom" : "universal",
"filename" : "placeholder.jpg",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "placeholder-1.jpg",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
================================================
FILE: Example/CarlosWatchSample Extension/CountryRow.swift
================================================
import Foundation
import WatchKit
class CountryRow: NSObject {
@IBOutlet var countryName: WKInterfaceLabel!
@IBOutlet var flagImage: WKInterfaceImage!
}
================================================
FILE: Example/CarlosWatchSample Extension/ExtensionDelegate.swift
================================================
import WatchKit
class ExtensionDelegate: NSObject, WKExtensionDelegate {
func applicationDidFinishLaunching() {}
}
================================================
FILE: Example/CarlosWatchSample Extension/Info.plist
================================================
CFBundleDevelopmentRegionenCFBundleDisplayNameCarlosWatchSample ExtensionCFBundleExecutable$(EXECUTABLE_NAME)CFBundleIdentifier$(PRODUCT_BUNDLE_IDENTIFIER)CFBundleInfoDictionaryVersion6.0CFBundleName$(PRODUCT_NAME)CFBundlePackageTypeXPC!CFBundleShortVersionString1.0CFBundleSignature????CFBundleVersion1NSAppTransportSecurityNSAllowsArbitraryLoadsNSExtensionNSExtensionAttributesWKAppBundleIdentifierde.axelspringer.CarlosSample.watchkitappNSExtensionPointIdentifiercom.apple.watchkitRemoteInterfacePrincipalClass$(PRODUCT_MODULE_NAME).InterfaceControllerWKExtensionDelegateClassName$(PRODUCT_MODULE_NAME).ExtensionDelegate
================================================
FILE: Example/CarlosWatchSample Extension/InterfaceController.swift
================================================
import Foundation
import WatchKit
import Carlos
import Combine
private struct Country {
let name: String
let flagURL: URL
}
class InterfaceController: WKInterfaceController {
@IBOutlet var tableView: WKInterfaceTable!
let imageCache = CacheProvider.imageCache()
private var cancellables = Set()
private let countries = [
Country(name: "Italy", flagURL: URL(string: "http://2.bp.blogspot.com/-51ZhmfLCi9s/VBLNUQL-giI/AAAAAAAAAfA/LTayxh5K3C4/s1600/flag_italy_mini.gif")!),
Country(name: "Germany", flagURL: URL(string: "http://www.weezerpedia.com/wiki/images/e/eb/Flag-germany.png")!),
Country(name: "France", flagURL: URL(string: "http://www.worldflagsportal.com/pics/thumbnails/france-flag.png")!),
Country(name: "Netherlands", flagURL: URL(string: "https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Flag_of_the_Netherlands.svg/50px-Flag_of_the_Netherlands.svg.png")!),
Country(name: "South Africa", flagURL: URL(string: "https://8b90b43d6bcfc09ee36c-3ad5470e7d4bb324e402ac2f90d6d0ba.ssl.cf3.rackcdn.com/soaf_1.gif")!),
Country(name: "USA", flagURL: URL(string: "http://www.scramblestuff.us/images/us_flag.png")!),
Country(name: "Australia", flagURL: URL(string: "http://dropdownaustralia.com/wp-content/uploads/2013/11/Australian-Flag.png")!),
Country(name: "Spain", flagURL: URL(string: "http://www.romanhomes.com/vacation_rentals/images/navona-campo-fiori-turtles-dream/navona-campo-fiori-turtles-dream/flag-spain-small.jpg")!),
Country(name: "Austria", flagURL: URL(string: "http://flagpedia.net/data/flags/mini/at.png")!),
Country(name: "Congo", flagURL: URL(string: "https://www.usaid.gov/sites/default/files/styles/40x24_flag/public/missions/flags/congo-democratic-republic-of.gif?itok=xRT3uqRi")!),
Country(name: "Cuba", flagURL: URL(string: "http://flagpedia.net/data/flags/mini/cu.png")!),
Country(name: "UK", flagURL: URL(string: "http://images.smh.com.au/2012/07/18/3464759/Olympic-Flag-Icon_Great_Britain.gif")!)
]
override func awake(withContext context: Any?) {
super.awake(withContext: context)
tableView.setNumberOfRows(countries.count, withRowType: "CountryRow")
for (idx, country) in countries.enumerated() {
if let row = tableView.rowController(at: idx) as? CountryRow {
row.countryName.setText(country.name)
row.flagImage.setImage(UIImage(named: "placeholder"))
imageCache.get(country.flagURL).sink(receiveCompletion: { _ in }) { image in
row.flagImage.setImage(image)
}.store(in: &cancellables)
}
}
}
}
================================================
FILE: Example/Example/AppDelegate.swift
================================================
import Carlos
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
true
}
}
func simpleCache() -> BasicCache {
CacheProvider.dataCache()
}
func delayedNetworkCache() -> BasicCache {
MemoryCacheLevel().compose(DiskCacheLevel()).compose(DelayedNetworkFetcher())
}
================================================
FILE: Example/Example/Base.lproj/Main.storyboard
================================================
================================================
FILE: Example/Example/BaseCacheViewController.swift
================================================
import Carlos
import Foundation
import UIKit
class BaseCacheViewController: UIViewController {
@IBOutlet var urlKeyField: UITextField?
@IBOutlet var fetchButton: UIButton!
@IBOutlet var eventsLogView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
title = titleForScreen()
setupCache()
Logger.output = { message, _ in
self.eventsLogView.text = "\(self.eventsLogView.text!)\(message)\n"
}
}
func setupCache() {}
func fetchRequested() {}
func titleForScreen() -> String {
"Carlos Sample"
}
@IBAction func fetchButtonTapped(_: AnyObject) {
fetchRequested()
urlKeyField?.resignFirstResponder()
}
}
extension BaseCacheViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text {
let newText = (text as NSString).replacingCharacters(in: range, with: string)
let textIsURL = URL(string: newText) != nil
fetchButton.isEnabled = textIsURL
}
return true
}
}
================================================
FILE: Example/Example/ComplexCacheSampleViewController.swift
================================================
import Foundation
import UIKit
import Carlos
import Combine
struct ModelDomain {
let name: String
let identifier: Int
let URL: Foundation.URL
}
extension ModelDomain: StringConvertible {
func toString() -> String {
"\(identifier)"
}
}
enum IgnoreError: Error {
case ignore
}
class CustomCacheLevel: Fetcher {
typealias KeyType = Int
typealias OutputType = String
func get(_ key: KeyType) -> AnyPublisher {
if key > 0 {
Logger.log("Fetched \(key) on the custom cache", .info)
return Just("\(key)")
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
}
Logger.log("Failed fetching \(key) on the custom cache", .info)
return Fail(error: IgnoreError.ignore).eraseToAnyPublisher()
}
}
class ComplexCacheSampleViewController: BaseCacheViewController {
@IBOutlet var nameField: UITextField!
@IBOutlet var identifierField: UITextField!
@IBOutlet var urlField: UITextField!
private var cache: BasicCache!
private var cancellables = Set()
override func titleForScreen() -> String {
"Complex cache"
}
override func setupCache() {
super.setupCache()
let modelDomainToString = OneWayTransformationBox(transform: {
Just($0.name).setFailureType(to: Error.self).eraseToAnyPublisher()
})
let modelDomainToInt = OneWayTransformationBox(transform: {
Just($0.identifier).setFailureType(to: Error.self).eraseToAnyPublisher()
})
let stringToData = StringTransformer().invert()
let uppercaseTransformer = OneWayTransformationBox(transform: {
Just($0.uppercased()).setFailureType(to: Error.self).eraseToAnyPublisher()
})
let memoryAndDisk = MemoryCacheLevel()
.compose(DiskCacheLevel())
.transformKeys(modelDomainToString)
let customCache = CustomCacheLevel()
.postProcess(uppercaseTransformer)
.transformKeys(modelDomainToInt)
.transformValues(stringToData)
cache = memoryAndDisk
.compose(customCache)
.compose(
BasicFetcher(getClosure: { (key: ModelDomain) in
Logger.log("Fetched \(key.name) on the fetcher closure", .info)
return Just(("Last level was hit!".data(using: .utf8, allowLossyConversion: false) as NSData?)!)
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
})
)
}
override func fetchRequested() {
super.fetchRequested()
let key = ModelDomain(name: nameField.text ?? "", identifier: Int(identifierField.text ?? "") ?? 0, URL: URL(string: urlField.text ?? "")!)
cache.get(key)
.subscribe(on: DispatchQueue(label: "carlose test queue", qos: .userInitiated))
.sink(receiveCompletion: { _ in }) { data in
print("Is Main Thread:", Thread.isMainThread)
print(data)
}.store(in: &cancellables)
for field in [nameField, identifierField, urlField] {
field?.resignFirstResponder()
}
}
}
================================================
FILE: Example/Example/ConditionedCacheSampleViewController.swift
================================================
import Foundation
import UIKit
import Carlos
import Combine
enum ConditionError: Error {
case globalKillSwitch
case urlScheme
func toString() -> String {
switch self {
case .globalKillSwitch:
return "Global kill switch is on"
case .urlScheme:
return "URL Scheme is not HTTP"
}
}
}
final class ConditionedCacheSampleViewController: BaseCacheViewController {
private var cache: BasicCache!
private var globalKillSwitch = false
private var cancellables = Set()
override func fetchRequested() {
super.fetchRequested()
cache.get(URL(string: urlKeyField?.text ?? "")!)
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { completion in
if case let .failure(error) = completion {
self.eventsLogView.text = "\(self.eventsLogView.text!)Failed because of condition\n"
print(error)
}
}, receiveValue: { _ in })
.store(in: &cancellables)
}
override func titleForScreen() -> String {
"Conditioned cache"
}
@IBAction func killSwitchValueChanged(_ sender: UISwitch) {
globalKillSwitch = sender.isOn
}
override func setupCache() {
super.setupCache()
cache = simpleCache().conditioned { key -> AnyPublisher in
if self.globalKillSwitch {
return Fail(error: ConditionError.globalKillSwitch).eraseToAnyPublisher()
} else if key.scheme != "http" {
return Fail(error: ConditionError.urlScheme).eraseToAnyPublisher()
}
return Just(true).setFailureType(to: Error.self).eraseToAnyPublisher()
}
}
}
================================================
FILE: Example/Example/DataCacheSampleViewController.swift
================================================
import Carlos
import Combine
import Foundation
import UIKit
class DataCacheSampleViewController: BaseCacheViewController {
fileprivate var cache: BasicCache!
private var cancellable: AnyCancellable?
override func fetchRequested() {
super.fetchRequested()
cancellable = cache.get(URL(string: urlKeyField?.text ?? "")!)
.sink(receiveCompletion: { _ in }, receiveValue: { _ in })
}
override func titleForScreen() -> String {
"Data cache"
}
override func setupCache() {
super.setupCache()
cache = CacheProvider.dataCache()
}
}
================================================
FILE: Example/Example/DelayedNetworkFetcher.swift
================================================
import Foundation
import Carlos
import Combine
final class DelayedNetworkFetcher: NetworkFetcher {
override func get(_ key: KeyType) -> AnyPublisher {
super.get(key)
.delay(for: 2, scheduler: DispatchQueue.global())
.eraseToAnyPublisher()
}
}
================================================
FILE: Example/Example/ExampleCell.swift
================================================
import Foundation
import UIKit
class ExampleCell: UITableViewCell {
static let Identifier = "ExampleCell"
func configureWithExample(_ example: Example) {
textLabel?.text = example.name
detailTextLabel?.text = example.shortDescription
}
}
================================================
FILE: Example/Example/ExamplesListViewController.swift
================================================
import Foundation
import UIKit
struct ExamplesListSection {
let name: String
let samples: [Example]
}
struct Example {
let name: String
let shortDescription: String
let segueIdentifier: String
}
class ExamplesListViewController: UIViewController {
fileprivate let sections = [
ExamplesListSection(name: "Simple", samples: [
Example(name: "Image cache", shortDescription: "Out-of-the-box image cache", segueIdentifier: "imageCache"),
Example(name: "Data cache", shortDescription: "Out-of-the-box data cache", segueIdentifier: "dataCache"),
Example(name: "JSON cache", shortDescription: "Out-of-the-box JSON cache", segueIdentifier: "jsonCache"),
Example(name: "User defaults cache", shortDescription: "Out-of-the-box NSUserDefaults cache", segueIdentifier: "userDefaultsCache"),
Example(name: "Memory warnings", shortDescription: "Simple stack with memory warnings listeners", segueIdentifier: "memoryWarning")
]),
ExamplesListSection(name: "Advanced", samples: [
Example(name: "Complex cache", shortDescription: "Custom stack with key and value transformations", segueIdentifier: "complexCache"),
Example(name: "Conditioned cache", shortDescription: "Simple stack with conditioned levels", segueIdentifier: "conditionedCache"),
Example(name: "Pooled cache", shortDescription: "Simple stack with requests pooling", segueIdentifier: "pooledCache"),
Example(name: "Switched cache", shortDescription: "2 Simple switched lanes", segueIdentifier: "switchedCache")
])
]
override func viewDidLoad() {
super.viewDidLoad()
title = "Carlos Samples"
}
}
extension ExamplesListViewController: UITableViewDataSource {
func numberOfSections(in _: UITableView) -> Int {
sections.count
}
func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
sections[section].samples.count
}
func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
sections[section].name
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: ExampleCell.Identifier, for: indexPath) as! ExampleCell
cell.configureWithExample(sections[(indexPath as NSIndexPath).section].samples[(indexPath as NSIndexPath).row])
return cell
}
}
extension ExamplesListViewController: UITableViewDelegate {
func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
let example = sections[(indexPath as NSIndexPath).section].samples[(indexPath as NSIndexPath).row]
performSegue(withIdentifier: example.segueIdentifier, sender: self)
}
}
================================================
FILE: Example/Example/ImageCacheSampleViewController.swift
================================================
import Foundation
import UIKit
import Carlos
import Combine
final class ImageCacheSampleViewController: BaseCacheViewController {
private var cache: BasicCache!
private var cancellables = Set()
@IBOutlet var imageView: UIImageView?
override func fetchRequested() {
super.fetchRequested()
cache.get(URL(string: urlKeyField?.text ?? "")!)
.receive(on: DispatchQueue.main)
.print()
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
self.imageView?.image = self.imageWithColor(.darkGray, size: self.imageView?.frame.size ?? .zero)
default:
break
}
print(completion)
}, receiveValue: { image in
self.imageView?.image = image
})
.store(in: &cancellables)
}
private func imageWithColor(_ color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(origin: CGPoint.zero, size: size)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
override func titleForScreen() -> String {
"Image cache"
}
override func setupCache() {
super.setupCache()
cache = CacheProvider.imageCache()
}
}
================================================
FILE: Example/Example/Images.xcassets/AppIcon.appiconset/Contents.json
================================================
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
================================================
FILE: Example/Example/Info.plist
================================================
CFBundleDevelopmentRegionenCFBundleExecutable$(EXECUTABLE_NAME)CFBundleIdentifier$(PRODUCT_BUNDLE_IDENTIFIER)CFBundleInfoDictionaryVersion6.0CFBundleName$(PRODUCT_NAME)CFBundlePackageTypeAPPLCFBundleShortVersionString1.0CFBundleSignature????CFBundleVersion1LSRequiresIPhoneOSNSAppTransportSecurityNSAllowsArbitraryLoadsUILaunchStoryboardNameMainUIMainStoryboardFileMainUIRequiredDeviceCapabilitiesarmv7UISupportedInterfaceOrientationsUIInterfaceOrientationPortraitUIInterfaceOrientationLandscapeLeftUIInterfaceOrientationLandscapeRightUISupportedInterfaceOrientations~ipadUIInterfaceOrientationPortraitUIInterfaceOrientationPortraitUpsideDownUIInterfaceOrientationLandscapeLeftUIInterfaceOrientationLandscapeRight
================================================
FILE: Example/Example/JSONCacheSampleViewController.swift
================================================
import Foundation
import UIKit
import Carlos
import Combine
private var myContext = 0
final class JSONCacheSampleViewController: BaseCacheViewController {
private var cache: BasicCache!
private var cancellables = Set()
override func fetchRequested() {
super.fetchRequested()
cache.get(URL(string: urlKeyField?.text ?? "")!)
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { _ in }, receiveValue: { JSON in
self.eventsLogView.text = "\(self.eventsLogView.text!)\nJSON Dictionary result: \(JSON as? NSDictionary)\n"
})
.store(in: &cancellables)
let progress = Progress.current()
progress?.addObserver(self, forKeyPath: "fractionCompleted", options: .initial, context: &myContext)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
if context == &myContext {
if let newValue = change?[NSKeyValueChangeKey.newKey] {
print("Progress changed: \(newValue)")
}
} else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
override func titleForScreen() -> String {
"JSON cache"
}
override func setupCache() {
super.setupCache()
cache = CacheProvider.JSONCache()
}
}
================================================
FILE: Example/Example/MemoryWarningSampleViewController.swift
================================================
import Carlos
import Combine
import Foundation
import UIKit
class MemoryWarningSampleViewController: BaseCacheViewController {
private var cache: BasicCache!
private var token: NSObjectProtocol?
var cancellable: AnyCancellable?
override func fetchRequested() {
super.fetchRequested()
cancellable = cache.get(URL(string: urlKeyField?.text ?? "")!)
.sink(receiveCompletion: { _ in }, receiveValue: { _ in })
}
override func titleForScreen() -> String {
"Memory warnings"
}
override func setupCache() {
super.setupCache()
cache = simpleCache()
}
@IBAction func memoryWarningSwitchValueChanged(_ sender: UISwitch) {
if sender.isOn, token == nil {
token = cache.listenToMemoryWarnings()
} else if let token = token, !sender.isOn {
unsubscribeToMemoryWarnings(token)
self.token = nil
}
}
@IBAction func simulateMemoryWarning(_: AnyObject) {
NotificationCenter.default.post(name: UIApplication.didReceiveMemoryWarningNotification, object: nil)
}
}
================================================
FILE: Example/Example/PooledCacheSampleViewController.swift
================================================
import Foundation
import UIKit
import Carlos
import Combine
final class PooledCacheSampleViewController: BaseCacheViewController {
private var cache: PoolCache>!
private var cancellables = Set()
override func fetchRequested() {
super.fetchRequested()
let timestamp = Date().timeIntervalSince1970
eventsLogView.text = "\(eventsLogView.text!)Request timestamp: \(timestamp)\n"
cache.get(URL(string: urlKeyField?.text ?? "")!)
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { _ in }, receiveValue: { _ in
self.eventsLogView.text = "\(self.eventsLogView.text!)Request with timestamp \(timestamp) succeeded\n"
}).store(in: &cancellables)
}
override func titleForScreen() -> String {
"Pooled cache"
}
override func setupCache() {
super.setupCache()
cache = delayedNetworkCache().pooled()
}
}
================================================
FILE: Example/Example/SwitchCacheSampleViewController.swift
================================================
import Foundation
import UIKit
import Carlos
import Combine
final class SwitchCacheSampleViewController: BaseCacheViewController {
private var cache: BasicCache!
private var cancellables = Set()
override func fetchRequested() {
super.fetchRequested()
cache.get(URL(string: urlKeyField?.text ?? "")!)
.sink(receiveCompletion: { _ in }, receiveValue: { _ in })
.store(in: &cancellables)
}
override func titleForScreen() -> String {
"Switched caches"
}
override func setupCache() {
super.setupCache()
let lane1 = MemoryCacheLevel()
lane1.set(("Yes, this is hitting the memory cache now".data(using: .utf8, allowLossyConversion: false) as NSData?)!, forKey: URL(string: "test")!)
.sink(receiveCompletion: { _ in }, receiveValue: { _ in })
.store(in: &cancellables)
lane1.set(("Carlos lets you create quite complex cache infrastructures".data(using: .utf8, allowLossyConversion: false) as NSData?)!, forKey: URL(string: "carlos")!)
.sink(receiveCompletion: { _ in }, receiveValue: { _ in })
.store(in: &cancellables)
let lane2 = CacheProvider.dataCache()
cache = switchLevels(cacheA: lane1, cacheB: lane2, switchClosure: { key in
if key.scheme == "http" {
return .cacheB
} else {
return .cacheA
}
})
}
}
================================================
FILE: Example/Example/UserDefaultsCacheSampleViewController.swift
================================================
import Foundation
import UIKit
import Carlos
import Combine
final class UserDefaultsCacheSampleViewController: BaseCacheViewController {
private var cache: NSUserDefaultsCacheLevel!
private var cancellables = Set()
override func fetchRequested() {
super.fetchRequested()
cache.get(urlKeyField?.text ?? "")
.sink(receiveCompletion: { _ in }, receiveValue: { _ in })
.store(in: &cancellables)
}
override func titleForScreen() -> String {
"User defaults cache"
}
@IBAction func clearCache(_: AnyObject) {
cache.clear()
}
override func setupCache() {
super.setupCache()
cache = NSUserDefaultsCacheLevel()
let values = [
"test": "value".data(using: String.Encoding.utf8)!,
"key": "another value".data(using: String.Encoding.utf8)!
]
for (key, value) in values {
cache.set(value as NSData, forKey: key)
.sink(receiveCompletion: { _ in }, receiveValue: { _ in })
.store(in: &cancellables)
}
let prepopulatingMessage = values.reduce("") { accumulator, value in
"\(accumulator)\n\(value.0): \(value.1)"
}
eventsLogView.text = "\(eventsLogView.text!)Prepopulating the cache:\n\(prepopulatingMessage)\n"
}
}
================================================
FILE: Example/Example.xcodeproj/project.pbxproj
================================================
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objects = {
/* Begin PBXBuildFile section */
5208636F1B64268000F740C7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520863661B64268000F740C7 /* AppDelegate.swift */; };
520863701B64268000F740C7 /* BaseCacheViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520863671B64268000F740C7 /* BaseCacheViewController.swift */; };
520863711B64268000F740C7 /* ComplexCacheSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520863681B64268000F740C7 /* ComplexCacheSampleViewController.swift */; };
520863721B64268000F740C7 /* ConditionedCacheSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520863691B64268000F740C7 /* ConditionedCacheSampleViewController.swift */; };
520863731B64268000F740C7 /* ExampleCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5208636A1B64268000F740C7 /* ExampleCell.swift */; };
520863741B64268000F740C7 /* ExamplesListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5208636B1B64268000F740C7 /* ExamplesListViewController.swift */; };
520863751B64268000F740C7 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5208636C1B64268000F740C7 /* Images.xcassets */; };
520863761B64268000F740C7 /* MemoryWarningSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5208636D1B64268000F740C7 /* MemoryWarningSampleViewController.swift */; };
520863771B64268000F740C7 /* DataCacheSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5208636E1B64268000F740C7 /* DataCacheSampleViewController.swift */; };
5208637F1B64268E00F740C7 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5208637C1B64268E00F740C7 /* Main.storyboard */; };
520863811B642D4A00F740C7 /* PooledCacheSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520863801B642D4A00F740C7 /* PooledCacheSampleViewController.swift */; };
520863851B64314400F740C7 /* DelayedNetworkFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520863841B64314400F740C7 /* DelayedNetworkFetcher.swift */; };
5216CD0F1BC04B43005B729F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5216CD0E1BC04B43005B729F /* AppDelegate.swift */; };
5216CD111BC04B43005B729F /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5216CD101BC04B43005B729F /* ViewController.swift */; };
5216CD141BC04B43005B729F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5216CD121BC04B43005B729F /* Main.storyboard */; };
5216CD161BC04B43005B729F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5216CD151BC04B43005B729F /* Assets.xcassets */; };
522A4E6B1B7BDACB00646D52 /* ImageCacheSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 522A4E6A1B7BDACB00646D52 /* ImageCacheSampleViewController.swift */; };
5235BAD51BE16B010049CFA6 /* UserDefaultsCacheSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5235BAD41BE16B010049CFA6 /* UserDefaultsCacheSampleViewController.swift */; };
523BBF891BC07C410040DA1B /* JSONCacheSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 523BBF881BC07C410040DA1B /* JSONCacheSampleViewController.swift */; };
528FAD481B7B0DCB0020A70E /* SwitchCacheSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 528FAD471B7B0DCB0020A70E /* SwitchCacheSampleViewController.swift */; };
52CF0A8E1B91F5EE0061022D /* CountryRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52CF0A8D1B91F5EE0061022D /* CountryRow.swift */; };
52CF0AD11B920DC20061022D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52CF0AD01B920DC20061022D /* AppDelegate.swift */; };
52CF0AD31B920DC20061022D /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52CF0AD21B920DC20061022D /* ViewController.swift */; };
52CF0AD51B920DC20061022D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 52CF0AD41B920DC20061022D /* Assets.xcassets */; };
52CF0AD81B920DC20061022D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 52CF0AD61B920DC20061022D /* Main.storyboard */; };
AD64D97E1B8DE69200C783D3 /* Interface.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AD64D97C1B8DE69200C783D3 /* Interface.storyboard */; };
AD64D9801B8DE69200C783D3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AD64D97F1B8DE69200C783D3 /* Assets.xcassets */; };
AD64D9871B8DE69200C783D3 /* ExampleWatchSample Extension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = AD64D9861B8DE69200C783D3 /* ExampleWatchSample Extension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
AD64D98C1B8DE69200C783D3 /* InterfaceController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD64D98B1B8DE69200C783D3 /* InterfaceController.swift */; };
AD64D98E1B8DE69200C783D3 /* ExtensionDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD64D98D1B8DE69200C783D3 /* ExtensionDelegate.swift */; };
AD64D9901B8DE69200C783D3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AD64D98F1B8DE69200C783D3 /* Assets.xcassets */; };
AD64D9941B8DE69200C783D3 /* ExampleWatchSample.app in Embed Watch Content */ = {isa = PBXBuildFile; fileRef = AD64D97A1B8DE69200C783D3 /* ExampleWatchSample.app */; };
CC6E521924EC192700DF1D80 /* Carlos in Frameworks */ = {isa = PBXBuildFile; productRef = CC6E521824EC192700DF1D80 /* Carlos */; };
CC6E521B24EC192F00DF1D80 /* Carlos in Frameworks */ = {isa = PBXBuildFile; productRef = CC6E521A24EC192F00DF1D80 /* Carlos */; };
CC6E521D24EC193500DF1D80 /* Carlos in Frameworks */ = {isa = PBXBuildFile; productRef = CC6E521C24EC193500DF1D80 /* Carlos */; };
CC6E521F24EC194E00DF1D80 /* Carlos in Frameworks */ = {isa = PBXBuildFile; productRef = CC6E521E24EC194E00DF1D80 /* Carlos */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
AD64D9881B8DE69200C783D3 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = AD695CD71B46CD65004E998D /* Project object */;
proxyType = 1;
remoteGlobalIDString = AD64D9851B8DE69200C783D3;
remoteInfo = "CarlosWatchSample Extension";
};
AD64D9921B8DE69200C783D3 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = AD695CD71B46CD65004E998D /* Project object */;
proxyType = 1;
remoteGlobalIDString = AD64D9791B8DE69200C783D3;
remoteInfo = CarlosWatchSample;
};
/* End PBXContainerItemProxy section */
/* Begin PBXCopyFilesBuildPhase section */
AD64D9981B8DE69200C783D3 /* Embed App Extensions */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 13;
files = (
AD64D9871B8DE69200C783D3 /* ExampleWatchSample Extension.appex in Embed App Extensions */,
);
name = "Embed App Extensions";
runOnlyForDeploymentPostprocessing = 0;
};
ADBC8F4C1B8DE4AB0080EF0A /* Embed Watch Content */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "$(CONTENTS_FOLDER_PATH)/Watch";
dstSubfolderSpec = 16;
files = (
AD64D9941B8DE69200C783D3 /* ExampleWatchSample.app in Embed Watch Content */,
);
name = "Embed Watch Content";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
520863661B64268000F740C7 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; };
520863671B64268000F740C7 /* BaseCacheViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseCacheViewController.swift; sourceTree = ""; };
520863681B64268000F740C7 /* ComplexCacheSampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ComplexCacheSampleViewController.swift; sourceTree = ""; };
520863691B64268000F740C7 /* ConditionedCacheSampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConditionedCacheSampleViewController.swift; sourceTree = ""; };
5208636A1B64268000F740C7 /* ExampleCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExampleCell.swift; sourceTree = ""; };
5208636B1B64268000F740C7 /* ExamplesListViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExamplesListViewController.swift; sourceTree = ""; };
5208636C1B64268000F740C7 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; };
5208636D1B64268000F740C7 /* MemoryWarningSampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MemoryWarningSampleViewController.swift; sourceTree = ""; };
5208636E1B64268000F740C7 /* DataCacheSampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataCacheSampleViewController.swift; sourceTree = ""; };
520863781B64268700F740C7 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
5208637D1B64268E00F740C7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
520863801B642D4A00F740C7 /* PooledCacheSampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PooledCacheSampleViewController.swift; sourceTree = ""; };
520863841B64314400F740C7 /* DelayedNetworkFetcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DelayedNetworkFetcher.swift; sourceTree = ""; };
5216CD0C1BC04B43005B729F /* ExampleTvSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ExampleTvSample.app; sourceTree = BUILT_PRODUCTS_DIR; };
5216CD0E1BC04B43005B729F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; };
5216CD101BC04B43005B729F /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; };
5216CD131BC04B43005B729F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
5216CD151BC04B43005B729F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
5216CD171BC04B43005B729F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
522A4E6A1B7BDACB00646D52 /* ImageCacheSampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageCacheSampleViewController.swift; sourceTree = ""; };
5235BAD41BE16B010049CFA6 /* UserDefaultsCacheSampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserDefaultsCacheSampleViewController.swift; sourceTree = ""; };
523BBF881BC07C410040DA1B /* JSONCacheSampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONCacheSampleViewController.swift; sourceTree = ""; };
528FAD471B7B0DCB0020A70E /* SwitchCacheSampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwitchCacheSampleViewController.swift; sourceTree = ""; };
52CF0A8D1B91F5EE0061022D /* CountryRow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CountryRow.swift; sourceTree = ""; };
52CF0ACE1B920DC20061022D /* ExampleMacSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ExampleMacSample.app; sourceTree = BUILT_PRODUCTS_DIR; };
52CF0AD01B920DC20061022D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; };
52CF0AD21B920DC20061022D /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; };
52CF0AD41B920DC20061022D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
52CF0AD71B920DC20061022D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
52CF0AD91B920DC20061022D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
AD64D97A1B8DE69200C783D3 /* ExampleWatchSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ExampleWatchSample.app; sourceTree = BUILT_PRODUCTS_DIR; };
AD64D97D1B8DE69200C783D3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Interface.storyboard; sourceTree = ""; };
AD64D97F1B8DE69200C783D3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
AD64D9811B8DE69200C783D3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
AD64D9861B8DE69200C783D3 /* ExampleWatchSample Extension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = "ExampleWatchSample Extension.appex"; sourceTree = BUILT_PRODUCTS_DIR; };
AD64D98B1B8DE69200C783D3 /* InterfaceController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InterfaceController.swift; sourceTree = ""; };
AD64D98D1B8DE69200C783D3 /* ExtensionDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionDelegate.swift; sourceTree = ""; };
AD64D98F1B8DE69200C783D3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
AD64D9911B8DE69200C783D3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
AD695CDF1B46CD65004E998D /* ExampleSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ExampleSample.app; sourceTree = BUILT_PRODUCTS_DIR; };
CCC88FA724ADDB60008C4060 /* Carlos.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Carlos.framework; sourceTree = BUILT_PRODUCTS_DIR; };
CCC88FAA24ADDB78008C4060 /* Carlos.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Carlos.framework; sourceTree = BUILT_PRODUCTS_DIR; };
CCC88FAD24ADDB7F008C4060 /* Carlos.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Carlos.framework; sourceTree = BUILT_PRODUCTS_DIR; };
CCC88FB124ADDB83008C4060 /* Carlos.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Carlos.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
5216CD091BC04B43005B729F /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
CC6E521D24EC193500DF1D80 /* Carlos in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
52CF0ACB1B920DC20061022D /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
CC6E521B24EC192F00DF1D80 /* Carlos in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
AD64D9831B8DE69200C783D3 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
CC6E521F24EC194E00DF1D80 /* Carlos in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
AD695CDC1B46CD65004E998D /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
CC6E521924EC192700DF1D80 /* Carlos in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
2677A0397774353D4B14F63F /* Frameworks */ = {
isa = PBXGroup;
children = (
CCC88FB124ADDB83008C4060 /* Carlos.framework */,
CCC88FAD24ADDB7F008C4060 /* Carlos.framework */,
CCC88FAA24ADDB78008C4060 /* Carlos.framework */,
CCC88FA724ADDB60008C4060 /* Carlos.framework */,
);
name = Frameworks;
sourceTree = "";
};
5216CD0D1BC04B43005B729F /* CarlosTvSample */ = {
isa = PBXGroup;
children = (
5216CD0E1BC04B43005B729F /* AppDelegate.swift */,
5216CD101BC04B43005B729F /* ViewController.swift */,
5216CD121BC04B43005B729F /* Main.storyboard */,
5216CD151BC04B43005B729F /* Assets.xcassets */,
5216CD171BC04B43005B729F /* Info.plist */,
);
path = CarlosTvSample;
sourceTree = "";
};
52CF0ACF1B920DC20061022D /* CarlosMacSample */ = {
isa = PBXGroup;
children = (
52CF0AD01B920DC20061022D /* AppDelegate.swift */,
52CF0AD21B920DC20061022D /* ViewController.swift */,
52CF0AD41B920DC20061022D /* Assets.xcassets */,
52CF0AD61B920DC20061022D /* Main.storyboard */,
52CF0AD91B920DC20061022D /* Info.plist */,
);
path = CarlosMacSample;
sourceTree = "";
};
AD64D97B1B8DE69200C783D3 /* CarlosWatchSample */ = {
isa = PBXGroup;
children = (
AD64D97C1B8DE69200C783D3 /* Interface.storyboard */,
AD64D97F1B8DE69200C783D3 /* Assets.xcassets */,
AD64D9811B8DE69200C783D3 /* Info.plist */,
);
path = CarlosWatchSample;
sourceTree = "";
};
AD64D98A1B8DE69200C783D3 /* CarlosWatchSample Extension */ = {
isa = PBXGroup;
children = (
AD64D98B1B8DE69200C783D3 /* InterfaceController.swift */,
AD64D98D1B8DE69200C783D3 /* ExtensionDelegate.swift */,
AD64D98F1B8DE69200C783D3 /* Assets.xcassets */,
AD64D9911B8DE69200C783D3 /* Info.plist */,
52CF0A8D1B91F5EE0061022D /* CountryRow.swift */,
);
path = "CarlosWatchSample Extension";
sourceTree = "";
};
AD695CD61B46CD65004E998D = {
isa = PBXGroup;
children = (
AD695CE11B46CD65004E998D /* Example */,
AD64D97B1B8DE69200C783D3 /* CarlosWatchSample */,
AD64D98A1B8DE69200C783D3 /* CarlosWatchSample Extension */,
52CF0ACF1B920DC20061022D /* CarlosMacSample */,
5216CD0D1BC04B43005B729F /* CarlosTvSample */,
AD695CE01B46CD65004E998D /* Products */,
2677A0397774353D4B14F63F /* Frameworks */,
);
sourceTree = "";
};
AD695CE01B46CD65004E998D /* Products */ = {
isa = PBXGroup;
children = (
AD695CDF1B46CD65004E998D /* ExampleSample.app */,
AD64D97A1B8DE69200C783D3 /* ExampleWatchSample.app */,
AD64D9861B8DE69200C783D3 /* ExampleWatchSample Extension.appex */,
52CF0ACE1B920DC20061022D /* ExampleMacSample.app */,
5216CD0C1BC04B43005B729F /* ExampleTvSample.app */,
);
name = Products;
sourceTree = "";
};
AD695CE11B46CD65004E998D /* Example */ = {
isa = PBXGroup;
children = (
AD695CE21B46CD65004E998D /* Supporting Files */,
5208636C1B64268000F740C7 /* Images.xcassets */,
5208637C1B64268E00F740C7 /* Main.storyboard */,
520863661B64268000F740C7 /* AppDelegate.swift */,
520863671B64268000F740C7 /* BaseCacheViewController.swift */,
520863681B64268000F740C7 /* ComplexCacheSampleViewController.swift */,
520863691B64268000F740C7 /* ConditionedCacheSampleViewController.swift */,
5208636A1B64268000F740C7 /* ExampleCell.swift */,
5208636B1B64268000F740C7 /* ExamplesListViewController.swift */,
5208636D1B64268000F740C7 /* MemoryWarningSampleViewController.swift */,
5208636E1B64268000F740C7 /* DataCacheSampleViewController.swift */,
520863801B642D4A00F740C7 /* PooledCacheSampleViewController.swift */,
520863841B64314400F740C7 /* DelayedNetworkFetcher.swift */,
528FAD471B7B0DCB0020A70E /* SwitchCacheSampleViewController.swift */,
522A4E6A1B7BDACB00646D52 /* ImageCacheSampleViewController.swift */,
523BBF881BC07C410040DA1B /* JSONCacheSampleViewController.swift */,
5235BAD41BE16B010049CFA6 /* UserDefaultsCacheSampleViewController.swift */,
);
path = Example;
sourceTree = SOURCE_ROOT;
};
AD695CE21B46CD65004E998D /* Supporting Files */ = {
isa = PBXGroup;
children = (
520863781B64268700F740C7 /* Info.plist */,
);
name = "Supporting Files";
sourceTree = "";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
5216CD0B1BC04B43005B729F /* ExampleTvSample */ = {
isa = PBXNativeTarget;
buildConfigurationList = 5216CD241BC04B43005B729F /* Build configuration list for PBXNativeTarget "ExampleTvSample" */;
buildPhases = (
5216CD081BC04B43005B729F /* Sources */,
5216CD091BC04B43005B729F /* Frameworks */,
5216CD0A1BC04B43005B729F /* Resources */,
);
buildRules = (
);
dependencies = (
);
name = ExampleTvSample;
packageProductDependencies = (
CC6E521C24EC193500DF1D80 /* Carlos */,
);
productName = CarlosTvSample;
productReference = 5216CD0C1BC04B43005B729F /* ExampleTvSample.app */;
productType = "com.apple.product-type.application";
};
52CF0ACD1B920DC20061022D /* ExampleMacSample */ = {
isa = PBXNativeTarget;
buildConfigurationList = 52CF0ADA1B920DC20061022D /* Build configuration list for PBXNativeTarget "ExampleMacSample" */;
buildPhases = (
52CF0ACA1B920DC20061022D /* Sources */,
52CF0ACB1B920DC20061022D /* Frameworks */,
52CF0ACC1B920DC20061022D /* Resources */,
);
buildRules = (
);
dependencies = (
);
name = ExampleMacSample;
packageProductDependencies = (
CC6E521A24EC192F00DF1D80 /* Carlos */,
);
productName = CarlosMacSample;
productReference = 52CF0ACE1B920DC20061022D /* ExampleMacSample.app */;
productType = "com.apple.product-type.application";
};
AD64D9791B8DE69200C783D3 /* ExampleWatchSample */ = {
isa = PBXNativeTarget;
buildConfigurationList = AD64D9991B8DE69200C783D3 /* Build configuration list for PBXNativeTarget "ExampleWatchSample" */;
buildPhases = (
AD64D9781B8DE69200C783D3 /* Resources */,
AD64D9981B8DE69200C783D3 /* Embed App Extensions */,
);
buildRules = (
);
dependencies = (
AD64D9891B8DE69200C783D3 /* PBXTargetDependency */,
);
name = ExampleWatchSample;
productName = CarlosWatchSample;
productReference = AD64D97A1B8DE69200C783D3 /* ExampleWatchSample.app */;
productType = "com.apple.product-type.application.watchapp2";
};
AD64D9851B8DE69200C783D3 /* ExampleWatchSample Extension */ = {
isa = PBXNativeTarget;
buildConfigurationList = AD64D9951B8DE69200C783D3 /* Build configuration list for PBXNativeTarget "ExampleWatchSample Extension" */;
buildPhases = (
AD64D9821B8DE69200C783D3 /* Sources */,
AD64D9831B8DE69200C783D3 /* Frameworks */,
AD64D9841B8DE69200C783D3 /* Resources */,
);
buildRules = (
);
dependencies = (
);
name = "ExampleWatchSample Extension";
packageProductDependencies = (
CC6E521E24EC194E00DF1D80 /* Carlos */,
);
productName = "CarlosWatchSample Extension";
productReference = AD64D9861B8DE69200C783D3 /* ExampleWatchSample Extension.appex */;
productType = "com.apple.product-type.watchkit2-extension";
};
AD695CDE1B46CD65004E998D /* ExampleSample */ = {
isa = PBXNativeTarget;
buildConfigurationList = AD695CFE1B46CD65004E998D /* Build configuration list for PBXNativeTarget "ExampleSample" */;
buildPhases = (
AD695CDB1B46CD65004E998D /* Sources */,
AD695CDC1B46CD65004E998D /* Frameworks */,
AD695CDD1B46CD65004E998D /* Resources */,
ADBC8F4C1B8DE4AB0080EF0A /* Embed Watch Content */,
);
buildRules = (
);
dependencies = (
AD64D9931B8DE69200C783D3 /* PBXTargetDependency */,
);
name = ExampleSample;
packageProductDependencies = (
CC6E521824EC192700DF1D80 /* Carlos */,
);
productName = CarlosSample;
productReference = AD695CDF1B46CD65004E998D /* ExampleSample.app */;
productType = "com.apple.product-type.application";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
AD695CD71B46CD65004E998D /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 1200;
ORGANIZATIONNAME = WeltN24;
TargetAttributes = {
5216CD0B1BC04B43005B729F = {
CreatedOnToolsVersion = 7.1;
LastSwiftMigration = 0800;
};
52CF0ACD1B920DC20061022D = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 1150;
};
AD64D9791B8DE69200C783D3 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 0920;
};
AD64D9851B8DE69200C783D3 = {
CreatedOnToolsVersion = 7.0;
LastSwiftMigration = 1020;
};
AD695CDE1B46CD65004E998D = {
CreatedOnToolsVersion = 6.3.2;
LastSwiftMigration = 1020;
};
};
};
buildConfigurationList = AD695CDA1B46CD65004E998D /* Build configuration list for PBXProject "Example" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = AD695CD61B46CD65004E998D;
productRefGroup = AD695CE01B46CD65004E998D /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
AD695CDE1B46CD65004E998D /* ExampleSample */,
AD64D9791B8DE69200C783D3 /* ExampleWatchSample */,
AD64D9851B8DE69200C783D3 /* ExampleWatchSample Extension */,
52CF0ACD1B920DC20061022D /* ExampleMacSample */,
5216CD0B1BC04B43005B729F /* ExampleTvSample */,
);
};
/* End PBXProject section */
/* Begin PBXResourcesBuildPhase section */
5216CD0A1BC04B43005B729F /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5216CD161BC04B43005B729F /* Assets.xcassets in Resources */,
5216CD141BC04B43005B729F /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
52CF0ACC1B920DC20061022D /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
52CF0AD51B920DC20061022D /* Assets.xcassets in Resources */,
52CF0AD81B920DC20061022D /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
AD64D9781B8DE69200C783D3 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
AD64D9801B8DE69200C783D3 /* Assets.xcassets in Resources */,
AD64D97E1B8DE69200C783D3 /* Interface.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
AD64D9841B8DE69200C783D3 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
AD64D9901B8DE69200C783D3 /* Assets.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
AD695CDD1B46CD65004E998D /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
520863751B64268000F740C7 /* Images.xcassets in Resources */,
5208637F1B64268E00F740C7 /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
5216CD081BC04B43005B729F /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5216CD111BC04B43005B729F /* ViewController.swift in Sources */,
5216CD0F1BC04B43005B729F /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
52CF0ACA1B920DC20061022D /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
52CF0AD31B920DC20061022D /* ViewController.swift in Sources */,
52CF0AD11B920DC20061022D /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
AD64D9821B8DE69200C783D3 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
52CF0A8E1B91F5EE0061022D /* CountryRow.swift in Sources */,
AD64D98E1B8DE69200C783D3 /* ExtensionDelegate.swift in Sources */,
AD64D98C1B8DE69200C783D3 /* InterfaceController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
AD695CDB1B46CD65004E998D /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
520863731B64268000F740C7 /* ExampleCell.swift in Sources */,
5235BAD51BE16B010049CFA6 /* UserDefaultsCacheSampleViewController.swift in Sources */,
520863701B64268000F740C7 /* BaseCacheViewController.swift in Sources */,
520863851B64314400F740C7 /* DelayedNetworkFetcher.swift in Sources */,
528FAD481B7B0DCB0020A70E /* SwitchCacheSampleViewController.swift in Sources */,
522A4E6B1B7BDACB00646D52 /* ImageCacheSampleViewController.swift in Sources */,
5208636F1B64268000F740C7 /* AppDelegate.swift in Sources */,
520863721B64268000F740C7 /* ConditionedCacheSampleViewController.swift in Sources */,
520863741B64268000F740C7 /* ExamplesListViewController.swift in Sources */,
520863711B64268000F740C7 /* ComplexCacheSampleViewController.swift in Sources */,
520863811B642D4A00F740C7 /* PooledCacheSampleViewController.swift in Sources */,
520863761B64268000F740C7 /* MemoryWarningSampleViewController.swift in Sources */,
520863771B64268000F740C7 /* DataCacheSampleViewController.swift in Sources */,
523BBF891BC07C410040DA1B /* JSONCacheSampleViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin PBXTargetDependency section */
AD64D9891B8DE69200C783D3 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = AD64D9851B8DE69200C783D3 /* ExampleWatchSample Extension */;
targetProxy = AD64D9881B8DE69200C783D3 /* PBXContainerItemProxy */;
};
AD64D9931B8DE69200C783D3 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = AD64D9791B8DE69200C783D3 /* ExampleWatchSample */;
targetProxy = AD64D9921B8DE69200C783D3 /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */
/* Begin PBXVariantGroup section */
5208637C1B64268E00F740C7 /* Main.storyboard */ = {
isa = PBXVariantGroup;
children = (
5208637D1B64268E00F740C7 /* Base */,
);
name = Main.storyboard;
sourceTree = "";
};
5216CD121BC04B43005B729F /* Main.storyboard */ = {
isa = PBXVariantGroup;
children = (
5216CD131BC04B43005B729F /* Base */,
);
name = Main.storyboard;
sourceTree = "";
};
52CF0AD61B920DC20061022D /* Main.storyboard */ = {
isa = PBXVariantGroup;
children = (
52CF0AD71B920DC20061022D /* Base */,
);
name = Main.storyboard;
sourceTree = "";
};
AD64D97C1B8DE69200C783D3 /* Interface.storyboard */ = {
isa = PBXVariantGroup;
children = (
AD64D97D1B8DE69200C783D3 /* Base */,
);
name = Interface.storyboard;
sourceTree = "";
};
/* End PBXVariantGroup section */
/* Begin XCBuildConfiguration section */
5216CD181BC04B43005B729F /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = "App Icon & Top Shelf Image";
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
DEBUG_INFORMATION_FORMAT = dwarf;
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = CarlosTvSample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = de.axelspringer.CarlosTvSample;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = appletvos;
SWIFT_VERSION = 4.2;
TVOS_DEPLOYMENT_TARGET = 13.0;
};
name = Debug;
};
5216CD191BC04B43005B729F /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = "App Icon & Top Shelf Image";
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = CarlosTvSample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = de.axelspringer.CarlosTvSample;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = appletvos;
SWIFT_VERSION = 4.2;
TVOS_DEPLOYMENT_TARGET = 13.0;
};
name = Release;
};
52CF0ADB1B920DC20061022D /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "-";
COMBINE_HIDPI_IMAGES = YES;
DEBUG_INFORMATION_FORMAT = dwarf;
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = CarlosMacSample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
PRODUCT_BUNDLE_IDENTIFIER = de.axelspringer.CarlosMacSample;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
52CF0ADC1B920DC20061022D /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "-";
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = CarlosMacSample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
PRODUCT_BUNDLE_IDENTIFIER = de.axelspringer.CarlosMacSample;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_VERSION = 5.0;
};
name = Release;
};
AD64D9961B8DE69200C783D3 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
DEBUG_INFORMATION_FORMAT = dwarf;
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = "CarlosWatchSample Extension/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = de.axelspringer.CarlosSample.watchkitapp.watchkitextension;
PRODUCT_NAME = "${TARGET_NAME}";
SDKROOT = watchos;
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "watchsimulator watchos";
SWIFT_VERSION = 5.0;
WATCHOS_DEPLOYMENT_TARGET = 6.0;
};
name = Debug;
};
AD64D9971B8DE69200C783D3 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = "CarlosWatchSample Extension/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = de.axelspringer.CarlosSample.watchkitapp.watchkitextension;
PRODUCT_NAME = "${TARGET_NAME}";
SDKROOT = watchos;
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "watchsimulator watchos";
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_VERSION = 5.0;
WATCHOS_DEPLOYMENT_TARGET = 6.0;
};
name = Release;
};
AD64D99A1B8DE69200C783D3 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEBUG_INFORMATION_FORMAT = dwarf;
FRAMEWORK_SEARCH_PATHS = "";
IBSC_MODULE = CarlosWatchSample_Extension;
INFOPLIST_FILE = CarlosWatchSample/Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = de.axelspringer.CarlosSample.watchkitapp;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = watchos;
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "watchsimulator watchos";
WATCHOS_DEPLOYMENT_TARGET = 6.0;
};
name = Debug;
};
AD64D99B1B8DE69200C783D3 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
FRAMEWORK_SEARCH_PATHS = "";
IBSC_MODULE = CarlosWatchSample_Extension;
INFOPLIST_FILE = CarlosWatchSample/Info.plist;
PRODUCT_BUNDLE_IDENTIFIER = de.axelspringer.CarlosSample.watchkitapp;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = watchos;
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "watchsimulator watchos";
WATCHOS_DEPLOYMENT_TARGET = 6.0;
};
name = Release;
};
AD695CFC1B46CD65004E998D /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 0.10.0;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = de.axelspringer.carlos;
PRODUCT_NAME = Carlos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos appletvos macosx watchos watchsimulator appletvsimulator";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TVOS_DEPLOYMENT_TARGET = 12.0;
WATCHOS_DEPLOYMENT_TARGET = 3.0;
};
name = Debug;
};
AD695CFD1B46CD65004E998D /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 0.10.0;
PRODUCT_BUNDLE_IDENTIFIER = de.axelspringer.carlos;
PRODUCT_NAME = Carlos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos appletvos macosx watchos watchsimulator appletvsimulator";
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_VERSION = 5.0;
TVOS_DEPLOYMENT_TARGET = 12.0;
VALIDATE_PRODUCT = YES;
WATCHOS_DEPLOYMENT_TARGET = 3.0;
};
name = Release;
};
AD695CFF1B46CD65004E998D /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
GCC_PREPROCESSOR_DEFINITIONS = "$(inherited)";
INFOPLIST_FILE = Example/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
OTHER_SWIFT_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = de.axelspringer.CarlosSample;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
};
name = Debug;
};
AD695D001B46CD65004E998D /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = Example/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
OTHER_SWIFT_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = de.axelspringer.CarlosSample;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
SWIFT_VERSION = 5.0;
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
5216CD241BC04B43005B729F /* Build configuration list for PBXNativeTarget "ExampleTvSample" */ = {
isa = XCConfigurationList;
buildConfigurations = (
5216CD181BC04B43005B729F /* Debug */,
5216CD191BC04B43005B729F /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
52CF0ADA1B920DC20061022D /* Build configuration list for PBXNativeTarget "ExampleMacSample" */ = {
isa = XCConfigurationList;
buildConfigurations = (
52CF0ADB1B920DC20061022D /* Debug */,
52CF0ADC1B920DC20061022D /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
AD64D9951B8DE69200C783D3 /* Build configuration list for PBXNativeTarget "ExampleWatchSample Extension" */ = {
isa = XCConfigurationList;
buildConfigurations = (
AD64D9961B8DE69200C783D3 /* Debug */,
AD64D9971B8DE69200C783D3 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
AD64D9991B8DE69200C783D3 /* Build configuration list for PBXNativeTarget "ExampleWatchSample" */ = {
isa = XCConfigurationList;
buildConfigurations = (
AD64D99A1B8DE69200C783D3 /* Debug */,
AD64D99B1B8DE69200C783D3 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
AD695CDA1B46CD65004E998D /* Build configuration list for PBXProject "Example" */ = {
isa = XCConfigurationList;
buildConfigurations = (
AD695CFC1B46CD65004E998D /* Debug */,
AD695CFD1B46CD65004E998D /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
AD695CFE1B46CD65004E998D /* Build configuration list for PBXNativeTarget "ExampleSample" */ = {
isa = XCConfigurationList;
buildConfigurations = (
AD695CFF1B46CD65004E998D /* Debug */,
AD695D001B46CD65004E998D /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
/* Begin XCSwiftPackageProductDependency section */
CC6E521824EC192700DF1D80 /* Carlos */ = {
isa = XCSwiftPackageProductDependency;
productName = Carlos;
};
CC6E521A24EC192F00DF1D80 /* Carlos */ = {
isa = XCSwiftPackageProductDependency;
productName = Carlos;
};
CC6E521C24EC193500DF1D80 /* Carlos */ = {
isa = XCSwiftPackageProductDependency;
productName = Carlos;
};
CC6E521E24EC194E00DF1D80 /* Carlos */ = {
isa = XCSwiftPackageProductDependency;
productName = Carlos;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = AD695CD71B46CD65004E998D /* Project object */;
}
================================================
FILE: Example/Example.xcodeproj/project.xcworkspace/contents.xcworkspacedata
================================================
================================================
FILE: Example/Example.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
================================================
IDEDidComputeMac32BitWarning
================================================
FILE: Gemfile
================================================
source 'https://rubygems.org'
gem 'fastlane', '~> 2.140'
gem 'rake'
================================================
FILE: LICENSE
================================================
The MIT License (MIT)
Copyright (c) 2020 SPRING AS Digital News Media GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: MIGRATING.md
================================================
## Migrating from 0.8 to 0.9
### Swift 3 support
Please make sure to convert all usages of `Carlos` APIs to the Swift 3 counterparts. In most cases there won't be a big syntactic difference.
### Deprecated functions
All deprecated functions have been removed. This were of 2 types:
- Global functions
- Functions taking fetch closures or transformation closures
For the first type, just use the corresponding protocol extension function.
For the second type, please use `BasicFetcher` instead of fetch closures, and explicit `OneWayTransformationBox` instances instead of transformation closures.
### Custom operators
All custom operators have been removed. Please use the corresponding protocol extension functions instead. This may require some reordering of the objects.
## Migrating from 0.7 to 0.8
### `Promise` now has only an empty `init`.
If you used one of the convenience `init` (with `value:`, with `error:` or with `value:error:`), they now moved to `Future`.
```swift
// Before
let future = Promise(value: 10).future
// Now
let future = Future(10)
```
```swift
// Before
let future = Promise(error: MyError.SomeError).future
// Now
let future = Future(MyError.SomeError)
```
```swift
// Before
let future = Promise(value: someOptionalInt, error: MyError.InvalidConversion).future
// Now
let future = Future(value: someOptionalInt, error: MyError.InvalidConversion)
```
## Migrating from 0.6 to 0.7
##### - Please note that with `Carlos` 0.7 the `Future` and `Promise`s code has been moved to a new framework.
- If you use `CocoaPods` or `Carthage`, you will just have to add a `import PiedPiper` line everywhere you make use of Carlos' `Future`s.
- If you did a submodule integration, please add `PiedPiper` as `Embeded binary` to your target.
- If you did a manual integration, please make sure that all the files missing from your target are re-added from the `Futures` folder.
##### - Check all your usages of `onCompletion` and replace the tuple `(value, error)` with the value `result`. Code will look like the following:
*Before*
```swift
future.onCompletion { (value, error) in
if let value = value {
//handle success case
} else if let error = error {
//handle error case
} else {
//handle cancelation case
}
}
```
*Now*
```swift
future.onCompletion { result in
switch result {
case .Success(let value):
//handle success case
case .Error(let error):
//handle error case
case .Cancelled:
//handle cancelation case
}
}
```
##### - Check all your usages of closures in the API. Methods taking closures instead of `Fetcher`, `CacheLevel` or `OneWayTransformer` values have been deprecated.
## Migrating from 0.4 to 0.5
##### - Rename all your usages of `CacheRequest` to `Future`
##### - If you created come custom `CacheLevel` or `Fetcher`, you should internally use `Promise` instead of `Future` so that you can control when the request can `fail` or `succeed`. `Future` is in fact a read-only version of `Promise`
*Before*
```swift
class MyCustomLevel: Fetcher {
typealias KeyType = String
typealias OutputType = String
func get(key: String) -> CacheRequest {
let request = CacheRequest()
//Do stuff...
request.succeed("Yay!")
return request
}
}
```
*Now*
```swift
class MyCustomLevel: Fetcher {
typealias KeyType = String
typealias OutputType = String
func get(key: String) -> Future {
let request = Promise()
//Do stuff...
request.succeed("Yay!")
return request.future
}
}
```
##### - If you created some custom `OneWayTransformer` or `TwoWayTransformer`, you should return `Future` now. This means that you have to wrap simple return values into `Future` instances
*Before*
```swift
let transformer = OneWayTransformationBox(transform: { $0.uppercaseString })
```
*Now*
```swift
let transformer = OneWayTransformationBox(transform: { Promise(value: $0.uppercaseString).future })
```
##### - If you used the `conditioned` API, the same async changes apply
*Before*
```swift
let conditionedCache = myCache.conditioned { key in
//whatever
return true
}
```
*Now*
```swift
let conditionedCache = myCache.conditioned { key in
return Promise(value: true).future
}
```
##### - If you use global functions, please consider using protocol extensions instead. Global functions are now **deprecated** and will be discontinued in `Carlos 1.0`
For example:
*Before*
```swift
let cache = compose(firstLevel, secondLevel)
```
*Now*
```swift
let cache = firstLevel.compose(secondLevel)
```
================================================
FILE: Package.resolved
================================================
{
"object": {
"pins": [
{
"package": "CwlCatchException",
"repositoryURL": "https://github.com/mattgallagher/CwlCatchException.git",
"state": {
"branch": null,
"revision": "35f9e770f54ce62dd8526470f14c6e137cef3eea",
"version": "2.1.1"
}
},
{
"package": "CwlPreconditionTesting",
"repositoryURL": "https://github.com/mattgallagher/CwlPreconditionTesting.git",
"state": {
"branch": null,
"revision": "c21f7bab5ca8eee0a9998bbd17ca1d0eb45d4688",
"version": "2.1.0"
}
},
{
"package": "Nimble",
"repositoryURL": "https://github.com/Quick/Nimble.git",
"state": {
"branch": null,
"revision": "c93f16c25af5770f0d3e6af27c9634640946b068",
"version": "9.2.1"
}
},
{
"package": "Quick",
"repositoryURL": "https://github.com/Quick/Quick.git",
"state": {
"branch": null,
"revision": "bd86ca0141e3cfb333546de5a11ede63f0c4a0e6",
"version": "4.0.0"
}
}
]
},
"version": 1
}
================================================
FILE: Package.swift
================================================
// swift-tools-version:5.2
import PackageDescription
let package = Package(
name: "Carlos",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13),
.watchOS(.v6)
],
products: [
.library(
name: "Carlos",
targets: ["Carlos"]
)
],
dependencies: [
.package(url: "https://github.com/Quick/Quick.git", .upToNextMajor(from: "4.0.0")),
.package(url: "https://github.com/Quick/Nimble.git", .upToNextMajor(from: "9.2.0"))
],
targets: [
.target(
name: "Carlos",
dependencies: []
),
.testTarget(
name: "CarlosTests",
dependencies: [
"Carlos",
"Quick",
"Nimble"
]
)
],
swiftLanguageVersions: [.v5]
)
================================================
FILE: README.md
================================================
# Carlos
[](https://github.com/Carthage/Carthage)
> A simple but flexible cache, written in Swift for `iOS 13+` and `WatchOS 6` apps.
# Breaking Changes
Carlos 1.0.0 has been migrated from PiedPiper dependency to Combine hence the minimum supported platforms versions are equal to the Combine's minimum supported platforms versions. See the releases page for more information.
# Contents of this Readme
- [Carlos](#carlos)
- [Breaking Changes](#breaking-changes)
- [Contents of this Readme](#contents-of-this-readme)
- [What is Carlos?](#what-is-carlos)
- [Installation](#installation)
- [Swift Package Manager (Preferred)](#swift-package-manager-preferred)
- [CocoaPods](#cocoapods)
- [Carthage](#carthage)
- [Requirements](#requirements)
- [Usage](#usage)
- [Usage examples](#usage-examples)
- [Creating requests](#creating-requests)
- [Key transformations](#key-transformations)
- [Value transformations](#value-transformations)
- [Post-processing output](#post-processing-output)
- [Conditioned output post-processing](#conditioned-output-post-processing)
- [Conditioned value transformation](#conditioned-value-transformation)
- [Composing transformers](#composing-transformers)
- [Pooling requests](#pooling-requests)
- [Batching get requests](#batching-get-requests)
- [Conditioning caches](#conditioning-caches)
- [Multiple cache lanes](#multiple-cache-lanes)
- [Listening to memory warnings](#listening-to-memory-warnings)
- [Normalization](#normalization)
- [Creating custom levels](#creating-custom-levels)
- [Creating custom fetchers](#creating-custom-fetchers)
- [Built-in levels](#built-in-levels)
- [Logging](#logging)
- [Tests](#tests)
- [Future development](#future-development)
- [Apps using Carlos](#apps-using-carlos)
- [Authors](#authors)
- [Contributors:](#contributors)
- [License](#license)
- [Acknowledgements](#acknowledgements)
## What is Carlos?
`Carlos` is a small set of classes and functions to **realize custom, flexible and powerful cache layers** in your application.
With a Functional Programming vocabulary, Carlos makes for a monoidal cache system. You can check the best explanation of how that is realized [here](https://bkase.github.io/slides/composable-caching-swift/) or in [this video](https://www.youtube.com/watch?v=8uqXuEZLyUU), thanks to [@bkase](https://github.com/bkase) for the slides.
By default, **`Carlos` ships with an in-memory cache, a disk cache, a simple network fetcher and a `NSUserDefaults` cache** (the disk cache is inspired by [HanekeSwift](https://github.com/Haneke/HanekeSwift)).
With `Carlos` you can:
- **[create levels and fetchers](#creating-custom-levels)** depending on your needs
- [combine levels](#usage-examples)
- Cancel pending requests
- [transform the key](#key-transformations) each level will get, [or the values](#value-transformations) each level will output (this means you're free to implement every level independing on how it will be used later on). Some common value transformers are already provided with `Carlos`
- Apply [post-processing steps](#post-processing-output) to a cache level, for example sanitizing the output or resizing images
- [Post-processing steps](#conditioned-output-post-processing) and [value transformations](#conditioned-value-transformation) can also be applied conditionally on the key used to fetch the value
- [react to memory pressure events](#listening-to-memory-warnings) in your app
- **automatically populate upper levels when one of the lower levels fetches a value** for a key, so the next time the first level will already have it cached
- enable or disable specific levels of your composed cache depending on [boolean conditions](#conditioning-caches)
- easily [**pool requests**](#pooling-requests) so you don't have to care whether 5 requests with the same key have to be executed by an expensive cache level before even only 1 of them is done. `Carlos` can take care of that for you
- [batch get requests](#batching-get-requests) to only get notified when all of them are done
- setup [multiple lanes](#multiple-cache-lanes) for complex scenarios where, depending on certain keys or conditions, different caches should be used
- have a type-safe complex cache that won't even compile if the code doesn't satisfy the type requirements
## Installation
### Swift Package Manager (Preferred)
Add `Carlos` to your project through the Xcode or add the following line to your package dependencies:
```swift
.package("https://github.com/spring-media/Carlos", from: "1.0.0")
```
### CocoaPods
`Carlos` is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
```
pod "Carlos", :git => "https://github.com/spring-media/Carlos"
```
### Carthage
`Carthage` is also supported.
## Requirements
- iOS 13.0+
- WatchOS 6+
- Xcode 12+
## Usage
To run the example project, clone the repo.
### Usage examples
```swift
let cache = MemoryCacheLevel().compose(DiskCacheLevel())
```
This line will generate a cache that takes `String` keys and returns `NSData` values.
Setting a value for a given key on this cache will set it for both the levels.
Getting a value for a given key on this cache will first try getting it on the memory level, and if it cannot find one, will ask the disk level.
In case both levels don't have a value, the request will fail.
In case the disk level can fetch a value, this will also be set on the memory level so that the next fetch will be faster.
`Carlos` comes with a `CacheProvider` class so that standard caches are easily accessible.
- `CacheProvider.dataCache()` to create a cache that takes `URL` keys and returns `NSData` values
- `CacheProvider.imageCache()` to create a cache that takes `URL` keys and returns `UIImage` values
- `CacheProvider.JSONCache()` to create a cache that takes `URL` keys and returns `AnyObject` values (that should be then safely casted to arrays or dictionaries depending on your application)
The above methods always create new instances (so calling `CacheProvider.imageCache()` twice doesn't return the same instance, even though the disk level will be effectively shared because it will use the same folder on disk, but this is a side-effect and should not be relied upon) and you should take care of retaining the result in your application layer.
If you want to always get the same instance, you can use the following accessors instead:
- `CacheProvider.sharedDataCache` to retrieve a shared instance of a data cache
- `CacheProvider.sharedImageCache` to retrieve a shared instance of an image cache
- `CacheProvider.sharedJSONCache` to retrieve a shared instance of a JSON cache
### Creating requests
To fetch a value from a cache, use the `get` method.
```swift
cache.get("key")
.sink(
receiveCompletion: { completion in
if case let .failure(error) = completion {
print("An error occurred :( \(error)")
}
},
receiveValue: { value in
print("I found \(value)!")
}
)
```
A request can also be canceled with the `cancel()` method, and you can be notified of this event by calling `onCancel` on a given request:
```swift
let cancellable = cache.get(key)
.handleEvents(receiveCancel: {
print("Looks like somebody canceled this request!")
})
.sink(...)
[... somewhere else]
cancellable.cancel()
```
This cache is not very useful, though. It will never *actively* fetch values, just store them for later use. Let's try to make it more interesting:
```swift
let cache = MemoryCacheLevel()
.compose(DiskCacheLevel())
.compose(NetworkFetcher())
```
This will create a cache level that takes `URL` keys and stores `NSData` values (the type is inferred from the `NetworkFetcher` hard-requirement of `URL` keys and `NSData` values, while `MemoryCacheLevel` and `DiskCacheLevel` are much more flexible as described later).
### Key transformations
Key transformations are meant to make it possible to plug cache levels in whatever cache you're building.
Let's see how they work:
```swift
// Define your custom ErrorType values
enum URLTransformationError: Error {
case invalidURLString
}
let transformedCache = NetworkFetcher().transformKeys(
OneWayTransformationBox(
transform: {
Future { promise in
let url = URL(string: $0) {
promise(.success(url))
} else {
promise(.failure(URLTransformationError.invalidURLString))
}
}
}
)
)
```
With the line above, we're saying that all the keys coming into the NetworkFetcher level have to be transformed to `URL` values first. We can now plug this cache into a previously defined cache level that takes `String` keys:
```swift
let cache = MemoryCacheLevel().compose(transformedCache)
```
If this doesn't look very safe (one could always pass string garbage as a key and it won't magically translate to a `URL`, thus causing the `NetworkFetcher` to silently fail), we can still use a domain specific structure as a key, assuming it contains both `String` and `URL` values:
```swift
struct Image {
let identifier: String
let URL: Foundation.URL
}
let imageToString = OneWayTransformationBox(transform: { (image: Image) -> AnyPublisher in
Just(image.identifier).eraseToAnyPublisher()
})
let imageToURL = OneWayTransformationBox(transform: { (image: Image) -> AnyPublisher in
Just(image.URL).eraseToAnyPublisher()
})
let memoryLevel = MemoryCacheLevel().transformKeys(imageToString)
let diskLevel = DiskCacheLevel().transformKeys(imageToString)
let networkLevel = NetworkFetcher().transformKeys(imageToURL)
let cache = memoryLevel.compose(diskLevel).compose(networkLevel)
```
Now we can perform safe requests like this:
```swift
let image = Image(identifier: "550e8400-e29b-41d4-a716-446655440000", URL: URL(string: "http://goo.gl/KcGz8T")!)
cache.get(image).sink {
print("Found \(value)!")
}
```
Since `Carlos 0.5` you can also apply conditions to `OneWayTransformers` used for key transformations. Just call the `conditioned` function on the transformer and pass your condition. The condition can also be asynchronous and has to return a `AnyPublisher`, having the chance to return a specific error for the failure of the transformation.
```swift
let transformer = OneWayTransformationBox(transform: { key in
Future { promise in
if let value = URL(string: key) {
promise(.success(value))
} else {
promise(.failure(MyError.stringIsNotURL))
}
}.eraseToAnyPublisher()
}).conditioned { key in
Just(key)
.filter { $0.rangeOfString("http") != nil }
.eraseToAnyPublisher()
}
let cache = CacheProvider.imageCache().transformKeys(transformer)
```
That's not all, though.
What if our disk cache only stores `Data`, but we want our memory cache to conveniently store `UIImage` instances instead?
### Value transformations
Value transformers let you have a cache that (let's say) stores `Data` and mutate it to a cache that stores `UIImage` values. Let's see how:
```swift
let dataTransformer = TwoWayTransformationBox(transform: { (image: UIImage) -> AnyPublisher in
Just(UIImagePNGRepresentation(image)).eraseToAnyPublisher()
}, inverseTransform: { (data: Data) -> AnyPublisher in
Just(UIImage(data: data)!).eraseToAnyPublisher()
})
let memoryLevel = MemoryCacheLevel().transformKeys(imageToString).transformValues(dataTransformer)
```
This memory level can now replace the one we had before, with the difference that it will internally store `UIImage` values!
Keep in mind that, as with key transformations, if your transformation closure fails (either the forward transformation or the inverse transformation), the cache level will be skipped, as if the fetch would fail. Same considerations apply for `set` calls.
`Carlos` comes with some value transformers out of the box, for example:
- `JSONTransformer` to serialize `NSData` instances into JSON
- `ImageTransformer` to serialize `NSData` instances into `UIImage` values (not available on the Mac OS X framework)
- `StringTransformer` to serialize `NSData` instances into `String` values with a given encoding
- Extensions for some Cocoa classes (`DateFormatter`, `NumberFormatter`, `MKDistanceFormatter`) so that you can use customized instances depending on your needs.
As of `Carlos 0.4`, it's possible to transform values coming out of `Fetcher` instances with just a `OneWayTransformer` (as opposed to the required `TwoWayTransformer` for normal `CacheLevel` instancess. This is because the `Fetcher` protocol doesn't require `set`).
This means you can easily chain `Fetcher`s that get a JSON from the internet and transform their output to a model object (for example a `struct`) into a complex cache pipeline without having to create a dummy inverse transformation just to satisfy the requirements of the `TwoWayTransformer` protocol.
As of `Carlos 0.5`, all transformers natively support asynchronous computation, so you can have expensive transformations in your custom transformers without blocking other operations. In fact, the `ImageTransformer` that comes out of the box processes image transformations on a background queue.
As of `Carlos 0.5` you can also apply conditions to `TwoWayTransformers` used for value transformations. Just call the `conditioned` function on the transformer and pass your conditions (one for the forward transformation, one for the inverse transformation). The conditions can also be asynchronous and have to return a `AnyPublisher`, having the chance to return a specific error for the failure of the transformation.
```swift
let transformer = JSONTransformer().conditioned({ input in
Just(myCondition).eraseToAnyPublisher()
}, inverseCondition: { input in
Just(myCondition)eraseToAnyPublisher()
})
let cache = CacheProvider.dataCache().transformValues(transformer)
```
### Post-processing output
In some cases your cache level could return the right value, but in a sub-optimal format. For example, you would like to sanitize the output you're getting from the Cache as a whole, independently of the exact layer that returned it.
For these cases, the `postProcess` function introduced with `Carlos 0.4` could come helpful.
The function is available as a protocol extension of the `CacheLevel` protocol.
The `postProcess` function takes a `CacheLevel` and a `OneWayTransformer` with `TypeIn == TypeOut` as parameters and outputs a decorated `BasicCache` with the post-processing step embedded in.
```swift
// Let's create a simple "to uppercase" transformer
let transformer = OneWayTransformationBox(transform: { Just($0.uppercased() as String).eraseToAnyPublisher() })
// Our memory cache
let memoryCache = MemoryCacheLevel()
// Our decorated cache
let transformedCache = memoryCache.postProcess(transformer)
// Lowercase value set on the memory layer
memoryCache.set("test String", forKey: "key")
// We get the lowercase value from the undecorated memory layer
memoryCache.get("key").sink { value in
let x = value
}
// We get the uppercase value from the decorated cache, though
transformedCache.get("key").sink { value in
let x = value
}
```
Since `Carlos 0.5` you can also apply conditions to `OneWayTransformers` used for post processing transformations. Just call the `conditioned` function on the transformer and pass your condition. The condition can also be asynchronous and has to return a `AnyPublisher`, having the chance to return a specific error for the failure of the transformation. Keep in mind that the condition will actually take the output of the cache as the input, not the key used to fetch this value! If you want to apply conditions based on the key, use `conditionedPostProcess` instead, but keep in mind this doesn't support using `OneWayTransformer` instances yet.
```swift
let processer = OneWayTransformationBox(transform: { value in
Future { promise in
if let value = String(data: value as Data, encoding: .utf8)?.uppercased().data(using: .utf8) as NSData? {
promise(.success(value))
} else {
promise(.failure(FetchError.conditionNotSatisfied))
}
}
}).conditioned { value in
Just(value.length < 1000).eraseToAnyPublisher()
}
let cache = CacheProvider.dataCache().postProcess(processer)
```
### Conditioned output post-processing
Extending the case for simple [output post-processing](#post-processing-output), you can also apply conditional transformations based on the key used to fetch the value.
For these cases, the `conditionedPostProcess` function introduced with `Carlos 0.6` could come helpful.
The function is available as a protocol extension of the `CacheLevel` protocol.
The `conditionedPostProcess` function takes a `CacheLevel` and a conditioned transformer conforming to `ConditionedOneWayTransformer` as parameters and outputs a decorated `CacheLevel` with the conditional post-processing step embedded in.
```swift
// Our memory cache
let memoryCache = MemoryCacheLevel()
// Our decorated cache
let transformedCache = memoryCache.conditionedPostProcess(ConditionedOneWayTransformationBox(conditionalTransformClosure: { (key, value) in
if key == "some sentinel value" {
return Just(value.uppercased()).eraseToAnyPublisher()
} else {
return Just(value).eraseToAnyPublisher()
}
})
// Lowercase value set on the memory layer
memoryCache.set("test String", forKey: "some sentinel value")
// We get the lowercase value from the undecorated memory layer
memoryCache.get("some sentinel value").sink { value in
let x = value
}
// We get the uppercase value from the decorated cache, though
transformedCache.get("some sentinel value").sink { value in
let x = value
}
```
### Conditioned value transformation
Extending the case for simple [value transformation](#value-transformations), you can also apply conditional transformations based on the key used to fetch or set the value.
For these cases, the `conditionedValueTransformation` function introduced with `Carlos 0.6` could come helpful.
The function is available as a protocol extension of the `CacheLevel` protocol.
The `conditionedValueTransformation` function takes a `CacheLevel` and a conditioned transformer conforming to `ConditionedTwoWayTransformer` as parameters and outputs a decorated `CacheLevel` with a modified `OutputType` (equal to the transformer's `TypeOut`, as in the normal value transformation case) with the conditional value transformation step embedded in.
```swift
// Our memory cache
let memoryCache = MemoryCacheLevel()
// Our decorated cache
let transformedCache = memoryCache.conditionedValueTransformation(ConditionedTwoWayTransformationBox(conditionalTransformClosure: { (key, value) in
if key == "some sentinel value" {
return Just(1).eraseToAnyPublisher()
} else {
return Just(0).eraseToAnyPublisher()
}
}, conditionalInverseTransformClosure: { (key, value) in
if key > 0 {
return Just("Positive").eraseToAnyPublisher()
} else {
return Just("Null or negative").eraseToAnyPublisher()
}
})
// Value set on the memory layer
memoryCache.set("test String", forKey: "some sentinel value")
// We get the same value from the undecorated memory layer
memoryCache.get("some sentinel value").sink { value in
let x = value
}
// We get 1 from the decorated cache, though
transformedCache.get("some sentinel value").sink { value in
let x = value
}
// We set "Positive" on the decorated cache
transformedCache.set(5, forKey: "test")
```
### Composing transformers
As of `Carlos 0.4`, it's possible to compose multiple `OneWayTransformer` objects.
This way, one can create several transformer modules to build a small library and then combine them as more convenient depending on the application.
You can compose the transformers in the same way you do with normal `CacheLevel`s: with the `compose` protocol extension:
```swift
let firstTransformer = ImageTransformer() // NSData -> UIImage
let secondTransformer = ImageTransformer().invert() // Trivial UIImage -> NSData
let identityTransformer = firstTransformer.compose(secondTransformer)
```
The same approach can be applied to `TwoWayTransformer` objects (that by the way are already `OneWayTransformer` as well).
Many transformer modules will be provided by default with `Carlos`.
### Pooling requests
When you have a working cache, but some of your levels are expensive (say a Network fetcher or a database fetcher), **you may want to pool requests in a way that multiple requests for the same key, coming together before one of them completes, are grouped so that when one completes all of the other complete as well without having to actually perform the expensive operation multiple times**.
This functionality comes with `Carlos`.
```swift
let cache = (memoryLevel.compose(diskLevel).compose(networkLevel)).pooled()
```
Keep in mind that the key must conform to the `Hashable` protocol for the `pooled` function to work:
```swift
extension Image: Hashable {
var hashValue: Int {
return identifier.hashValue
}
}
extension Image: Equatable {}
func ==(lhs: Image, rhs: Image) -> Bool {
return lhs.identifier == rhs.identifier && lhs.URL == rhs.URL
}
```
Now we can execute multiple fetches for the same `Image` value and be sure that only one network request will be started.
### Batching get requests
Since `Carlos 0.7` you can pass a list of keys to your `CacheLevel` through `batchGetSome`.
This returns a `AnyPublisher` that succeeds when all the requests for the specified keys *complete*, not necessarily succeeding. You will only get the successful values in the success callback, though.
Since `Carlos 0.9` you can transform your `CacheLevel` into one that takes a list of keys through `allBatch`.
Calling `get` on such a `CacheLevel` returns a `AnyPublisher` that succeeds only when the requests for **all** of the specified keys succeed, and fails **as soon as one** of the requests for the specified keys fails.
If you cancel the `AnyPublisher` returned by this `CacheLevel`, all of the pending requests are canceled, too.
An example of the usage:
```swift
let cache = MemoryCacheLevel()
for iter in 0..<99 {
cache.set(iter, forKey: "key_\(iter)")
}
let keysToBatch = (0..<100).map { "key_\($0)" }
cache.batchGetSome(keysToBatch).sink(
receiveCompletion: { completion in
print("Failed because \($0)")
},
receiveValue: { values in
print("Got \(values.count) values in total")
}
)
```
In this case the `allBatch().get` call would fail because there are only 99 keys set and the last request will make the whole batch fail, with a `valueNotInCache` error. The `batchGetSome().get` will succeed instead, printing `Got 99 values in total`.
Since `allBatch` returns a new `CacheLevel` instance, it can be composed or transformed just like any other cache:
In this case `cache` is a cache that takes a sequence of `String` keys and returns a `AnyPublisher` of a list of `Int` values, but is limited to 3 concurrent requests (see the next paragraph for more information on limiting concurrent requests).
### Conditioning caches
Sometimes we may have levels that should only be queried under some conditions. Let's say we have a `DatabaseLevel` that should only be triggered when users enable a given setting in the app that actually starts storing data in the database. We may want to avoid accessing the database if the setting is disabled in the first place.
```swift
let conditionedCache = cache.conditioned { key in
Just(appSettingIsEnabled).eraseToAnyPublisher()
}
```
The closure gets the key the cache was asked to fetch and has to return a `AnyPublisher` object indicating whether the request can proceed or should skip the level, with the possibility to fail with a specific `Error` to communicate the error to the caller.
At runtime, if the variable `appSettingIsEnabled` is `false`, the `get` request will skip the level (or fail if this was the only or last level in the cache). If `true`, the `get` request will be executed.
### Multiple cache lanes
If you have a complex scenario where, depending on the key or some other external condition, either one or another cache should be used, then the `switchLevels` function could turn useful.
Usage:
```swift
let lane1 = MemoryCacheLevel() // The two lanes have to be equivalent (same key type, same value type).
let lane2 = CacheProvider.dataCache() // Keep in mind that you can always use key transformation or value transformations if two lanes don't match by default
let switched = switchLevels(lane1, lane2) { key in
if key.scheme == "http" {
return .cacheA
} else {
return .cacheB // The example is just meant to show how to return different lanes
}
}
```
Now depending on the scheme of the key URL, either the first lane or the second will be used.
### Listening to memory warnings
If we store big objects in memory in our cache levels, we may want to be notified of memory warning events. This is where the `listenToMemoryWarnings` and `unsubscribeToMemoryWarnings` functions come handy:
```swift
let token = cache.listenToMemoryWarnings()
```
and later
```swift
unsubscribeToMemoryWarnings(token)
```
With the first call, the cache level and all its composing levels will get a call to `onMemoryWarning` when a memory warning comes.
With the second call, the behavior will stop.
Keep in mind that this functionality is not yet supported by the WatchOS 2 framework `CarlosWatch.framework`.
### Normalization
In case you need to store the result of multiple `Carlos` composition calls in a property, it may be troublesome to set the type of the property to `BasicCache` as some calls return different types (e.g. `PoolCache`). In this case, you can `normalize` the cache level before assigning it to the property and it will be converted to a `BasicCache` value.
```swift
import Carlos
class CacheManager {
let cache: BasicCache
init(injectedCache: BasicCache) {
self.cache = injectedCache
}
}
[...]
let manager = CacheManager(injectedCache: CacheProvider.dataCache().pooled()) // This won't compile
let manager = CacheManager(injectedCache: CacheProvider.dataCache().pooled().normalize()) // This will
```
As a tip, always use `normalize` if you need to assign the result of multiple composition calls to a property. The call is a no-op if the value is already a `BasicCache`, so there will be no performance loss in that case.
### Creating custom levels
Creating custom levels is easy and encouraged (after all, there are multiple cache libraries already available if you only need memory, disk and network functionalities!).
Let's see how to do it:
```swift
class MyLevel: CacheLevel {
typealias KeyType = Int
typealias OutputType = Float
func get(_ key: KeyType) -> AnyPublisher {
Future {
// Perform the fetch and either succeed or fail
}.eraseToAnyPublisher()
}
func set(_ value: OutputType, forKey key: KeyType) -> AnyPublisher {
Future {
// Store the value (db, memory, file, etc) and call this on completion:
}.eraseToAnyPublisher()
}
func clear() {
// Clear the stored values
}
func onMemoryWarning() {
// A memory warning event came. React appropriately
}
}
```
The above class conforms to the `CacheLevel` protocol.
First thing we need is to declare what key types we accept and what output types we return. In this example case, we have `Int` keys and `Float` output values.
The required methods to implement are 4: `get`, `set`, `clear` and `onMemoryWarning`.
This sample cache can now be pipelined to a list of other caches, transforming its keys or values if needed as we saw in the earlier paragraphs.
### Creating custom fetchers
With `Carlos 0.4`, the `Fetcher` protocol was introduced to make it easier for users of the library to create custom fetchers that can be used as read-only levels in the cache. An example of a "`Fetcher` in disguise" that has always been included in `Carlos` is `NetworkFetcher`: you can only use it to read from the network, not to write (`set`, `clear` and `onMemoryWarning` were **no-ops**).
This is how easy it is now to implement your custom fetcher:
```swift
class CustomFetcher: Fetcher {
typealias KeyType = String
typealias OutputType = String
func get(_ key: KeyType) -> Anypublisher {
return Just("Found an hardcoded value :)").eraseToAnyPublisher()
}
}
```
You still need to declare what `KeyType` and `OutputType` your `CacheLevel` deals with, of course, but then you're only required to implement `get`. Less boilerplate for you!
### Built-in levels
`Carlos` comes with 3 cache levels out of the box:
- `MemoryCacheLevel`
- `DiskCacheLevel`
- `NetworkFetcher`
- Since the `0.5` release, a `UserDefaultsCacheLevel`
**MemoryCacheLevel** is a volatile cache that internally stores its values in an `NSCache` instance. The capacity can be specified through the initializer, and it supports clearing under memory pressure (if the level is [subscribed to memory warning notifications](#listening-to-memory-warnings)).
It accepts keys of any given type that conforms to the `StringConvertible` protocol and can store values of any given type that conforms to the `ExpensiveObject` protocol. `Data`, `NSData`, `String`, `NSString` `UIImage`, `URL` already conform to the latter protocol out of the box, while `String`, `NSString` and `URL` conform to the `StringConvertible` protocol.
This cache level is thread-safe.
**DiskCacheLevel** is a persistent cache that asynchronously stores its values on disk. The capacity can be specified through the initializer, so that the disk size will never get too big.
It accepts keys of any given type that conforms to the `StringConvertible` protocol and can store values of any given type that conforms to the `NSCoding` protocol.
This cache level is thread-safe, and currently the only `CacheLevel` that can fail when calling `set`, with a `DiskCacheLevelError.diskArchiveWriteFailed` error.
**NetworkFetcher** is a cache level that asynchronously fetches values over the network.
It accepts `URL` keys and returns `NSData` values.
This cache level is thread-safe.
**NSUserDefaultsCacheLevel** is a persistent cache that stores its values on a `UserDefaults` persistent domain with a specific name.
It accepts keys of any given type that conforms to the `StringConvertible` protocol and can store values of any given type that conforms to the `NSCoding` protocol.
It has an internal soft cache used to avoid hitting the persistent storage too often, and can be cleared without affecting other values saved on the `standardUserDefaults` or on other persistent domains.
This cache level is thread-safe.
### Logging
When we decided how to handle logging in Carlos, we went for the most flexible approach that didn't require us to code a complete logging framework, that is the ability to plug-in your own logging library.
If you want the output of Carlos to only be printed if exceeding a given level, if you want to completely silent it for release builds, or if you want to route it to a file, or whatever else: just assign your logging handling closure to `Carlos.Logger.output`:
```swift
Carlos.Logger.output = { message, level in
myLibrary.log(message) //Plug here your logging library
}
```
## Tests
`Carlos` is thouroughly tested so that the features it's designed to provide are safe for refactoring and as much as possible bug-free.
We use [Quick](https://github.com/Quick/Quick) and [Nimble](https://github.com/Quick/Nimble) instead of `XCTest` in order to have a good BDD test layout.
As of today, there are around **1000 tests** for `Carlos` (see the folder `Tests`), and overall the tests codebase is *double the size* of the production codebase.
## Future development
`Carlos` is under development and [here](https://github.com/WeltN24/Carlos/issues) you can see all the open issues. They are assigned to milestones so that you can have an idea of when a given feature will be shipped.
If you want to contribute to this repo, please:
- Create an issue explaining your problem and your solution
- Clone the repo on your local machine
- Create a branch with the issue number and a short abstract of the feature name
- Implement your solution
- Write tests (untested features won't be merged)
- When all the tests are written and green, create a pull request, with a short description of the approach taken
## Apps using Carlos
- [Die Welt Edition](https://itunes.apple.com/de/app/welt-edition-digitale-zeitung/id372746348?mt=8)
- [Welt news](https://itunes.apple.com/de/app/welt-news-aktuelle-nachrichten/id340021100?mt=8)
Using Carlos? Please let us know through a Pull request, we'll be happy to mention your app!
## Authors
`Carlos` was made in-house by WeltN24
### Contributors:
Vittorio Monaco, [vittorio.monaco@weltn24.de](mailto:vittorio.monaco@weltn24.de), [@vittoriom](https://github.com/vittoriom) on Github, [@Vittorio_Monaco](https://twitter.com/Vittorio_Monaco) on Twitter
Esad Hajdarevic, @esad
## License
`Carlos` is available under the MIT license. See the LICENSE file for more info.
## Acknowledgements
`Carlos` internally uses:
The **DiskCacheLevel** class is inspired by [Haneke](https://github.com/Haneke/HanekeSwift). The source code has been heavily modified, but adapting the original file has proven valuable for `Carlos` development.
================================================
FILE: Sources/Carlos/CacheLevels/BatchAllCache.swift
================================================
import Combine
/// A reified batchGetAll
public final class BatchAllCache: CacheLevel where KeySeq.Iterator.Element == Cache.KeyType {
/// A sequence of keys for the wrapped cache
public typealias KeyType = KeySeq
/// An array of output elements
public typealias OutputType = [Cache.OutputType]
private let cache: Cache
public init(cache: Cache) {
self.cache = cache
}
/**
Dispatch each key in the sequence in parallel
Merge the results -- if any key fails, it all fails
*/
public func get(_ key: KeyType) -> AnyPublisher {
let all = key.map(cache.get)
return all.publisher
.setFailureType(to: Error.self)
.flatMap { $0 }
.collect(all.count)
.eraseToAnyPublisher()
}
/**
Zip the keys with the values and set them all
*/
public func set(_ value: OutputType, forKey key: KeyType) -> AnyPublisher {
let initial = Just(())
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
return zip(value, key)
.map(cache.set)
.reduce(initial) { previous, current in
previous.flatMap { current }
.eraseToAnyPublisher()
}
}
public func remove(_ key: KeyType) -> AnyPublisher {
let all = key.map(cache.remove)
return all.publisher
.setFailureType(to: Error.self)
.collect(all.count)
.map { _ in () }
.eraseToAnyPublisher()
}
public func clear() {
cache.clear()
}
public func onMemoryWarning() {
cache.onMemoryWarning()
}
}
extension CacheLevel {
/**
Wrap a cache into a , [V]> cache where
each k in Sequence is dispatched in parallel and if any K fails,
it all fails
*/
public func allBatch() -> BatchAllCache {
BatchAllCache(cache: self)
}
}
================================================
FILE: Sources/Carlos/CacheLevels/Composed.swift
================================================
import Combine
import Foundation
extension CacheLevel {
/**
Composes two cache levels
- parameter cache: The second cache level
- returns: A new cache level that is the result of the composition of the two cache levels
*/
public func compose(_ cache: A) -> BasicCache where A.KeyType == KeyType, A.OutputType == OutputType {
BasicCache(
getClosure: { key in
self.get(key)
.catch { _ -> AnyPublisher in
Logger.log("Composed| error on getting value for key \(key) on cache \(String(describing: self)).", .info)
return cache.get(key)
.flatMap { [weak self] value -> AnyPublisher<(OutputType, Void), Error> in
guard let self = self else {
return Empty(completeImmediately: true).eraseToAnyPublisher()
}
let get = Just(value).setFailureType(to: Error.self)
let set = self.set(value, forKey: key)
return Publishers.Zip(get, set)
.eraseToAnyPublisher()
}
.map(\.0)
.eraseToAnyPublisher()
}.eraseToAnyPublisher()
},
setClosure: { value, key in
Publishers.Zip(
self.set(value, forKey: key),
cache.set(value, forKey: key)
)
.map { _ in () }
.eraseToAnyPublisher()
},
removeClosure: { key in
Publishers.Zip(self.remove(key), cache.remove(key))
.map { _ in () }
.eraseToAnyPublisher()
},
clearClosure: {
self.clear()
cache.clear()
},
memoryClosure: {
self.onMemoryWarning()
cache.onMemoryWarning()
}
)
}
}
================================================
FILE: Sources/Carlos/CacheLevels/Conditioned.swift
================================================
import Combine
import Foundation
extension CacheLevel {
/**
Wraps the CacheLevel with a boolean condition on the key that controls when a get call should fail unconditionally
- parameter condition: The condition closure that takes a key and returns true if the key can be fetched, or false if the request should fail unconditionally. The closure can also pass a specific error in case it wants to explicitly communicate why it failed. The condition can be asynchronous and has to return a Future
- returns: A new BasicCache that will check for the condition before every get is dispatched to the decorated cache level
The condition doesn't apply to the set, clear, onMemoryWarning calls
*/
public func conditioned(_ condition: @escaping (KeyType) -> AnyPublisher) -> BasicCache {
BasicCache(
getClosure: conditionedClosure(get, condition: condition),
setClosure: set,
removeClosure: remove,
clearClosure: clear,
memoryClosure: onMemoryWarning
)
}
}
private func conditionedClosure(_ closure: @escaping (A) -> AnyPublisher, condition: @escaping (A) -> AnyPublisher) -> ((A) -> AnyPublisher) {
return { input in
condition(input).flatMap { (passesCondition: Bool) -> AnyPublisher in
if passesCondition {
return closure(input)
} else {
return Fail(error: FetchError.conditionNotSatisfied).eraseToAnyPublisher()
}
}.eraseToAnyPublisher()
}
}
extension OneWayTransformer {
/**
Wraps the transformer with a boolean condition on the input that controls when a transformation should fail unconditionally.
- parameter condition: The condition closure that takes an input and returns true if the input can be transformed, or false if the request should fail unconditionally. The closure can also pass a specific error in case it wants to explicitly communicate why it failed. The condition can be asynchronous and has to return a Future
- returns: A new OneWayTransformer that will check for the condition before every transformation is dispatched to the decorated transformer
*/
public func conditioned(_ condition: @escaping (TypeIn) -> AnyPublisher) -> OneWayTransformationBox {
OneWayTransformationBox(transform: conditionedClosure(transform, condition: condition))
}
}
extension TwoWayTransformer {
/**
Wraps the transformer with a boolean condition on the input and a boolean condition on the "inverse input" that controls when a transformation on either side should fail unconditionally.
- parameter condition: The condition closure used for normal transformations that takes an input and returns true if the input can be transformed, or false if the request should fail unconditionally. The closure can also pass a specific error in case it wants to explicitly communicate why it failed. The condition can be asynchronous and has to return a Future
- parameter inverseCondition: The condition closure used for inverse transformations that takes a TypeOut argument and returns true if the input can be transformed, or false if the request should fail unconditionally. The closure can also pass a specific error in case it wants to explicitly communicate why it failed. The condition can be asynchronous and has to return a Future.
- returns: A new TwoWayTransformer that will check for the conditions before every transformation is dispatched to the decorated transformer
*/
public func conditioned(_ condition: @escaping (TypeIn) -> AnyPublisher, inverseCondition: @escaping (TypeOut) -> AnyPublisher) -> TwoWayTransformationBox {
TwoWayTransformationBox(
transform: conditionedClosure(transform, condition: condition),
inverseTransform: conditionedClosure(inverseTransform, condition: inverseCondition)
)
}
}
================================================
FILE: Sources/Carlos/CacheLevels/DiskCacheLevel.swift
================================================
import Combine
import Foundation
public enum DiskCacheLevelError: Error {
case diskArchiveWriteFailed
}
/// This class is a disk cache level. It has a configurable total size that defaults to 100 MB.
public final class DiskCacheLevel: CacheLevel {
/// At the moment the disk cache level only accepts keys that can be converted to string values
public typealias KeyType = K
/// The output type of the cache, should conform to NSCoding
public typealias OutputType = T
private let path: String
private var size: UInt64 = 0
private let fileManager: FileManager
/// The capacity of the cache
public var capacity: UInt64 = 0 {
didSet {
cacheQueue.async {
self.controlCapacity()
}
}
}
private lazy var cacheQueue: DispatchQueue = {
DispatchQueue(label: "\(CarlosGlobals.queueNamePrefix)\((self.path as NSString).lastPathComponent)")
}()
/**
This method is a no-op since all the contents of the cache are stored on disk, so removing them would have no benefit for memory pressure
*/
public func onMemoryWarning() {}
/**
Initializes a new disk cache level
- parameter path: The path to the disk storage. Defaults to a Carlos specific folder in the Caches sandbox folder.
- parameter capacity: The total capacity in bytes for the disk cache. Defaults to 100 MB
- parameter fileManager: The file manager to use. Defaults to the default NSFileManager. It's here mainly for dependency injection testing purposes.
*/
public init(
path: String = (CarlosGlobals.caches as NSString).appendingPathComponent(CarlosGlobals.queueNamePrefix + "default"),
capacity: UInt64 = 100 * 1024 * 1024,
fileManager: FileManager = FileManager.default
) {
self.path = path
self.fileManager = fileManager
self.capacity = capacity
_ = try? fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: [:])
cacheQueue.async {
self.calculateSize()
self.controlCapacity()
}
}
/**
Asynchronously sets a value for the given key
- parameter value: The value to save on disk
- parameter key: The key for the value
*/
public func set(_ value: T, forKey key: K) -> AnyPublisher {
Logger.log("DiskCacheLevel| Setting a value for the key \(key.toString()) on the disk cache \(self)", .info)
return Just((value, key))
.setFailureType(to: Error.self)
.subscribe(on: cacheQueue)
.flatMap { [weak self] payload -> AnyPublisher in
guard let self = self else {
return Empty(completeImmediately: true).eraseToAnyPublisher()
}
return self.setDataSync(payload.0, key: payload.1)
}
.eraseToAnyPublisher()
}
/**
Asynchronously gets the value for the given key
- parameter key: The key for the value
- returns: A Future where you can call onSuccess and onFailure to be notified of the result of the fetch
*/
public func get(_ key: KeyType) -> AnyPublisher {
AnyPublisher.create { [weak self] promise in
guard let self = self else {
return
}
let path = self.pathForKey(key)
if let data = try? Data(contentsOf: URL(fileURLWithPath: path)),
let obj = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? T
{
Logger.log("DiskCacheLevel| Fetched \(key.toString()) on disk level", .info)
promise(.success(obj))
_ = self.updateDiskAccessDateAtPath(path)
} else {
// Remove the file (maybe corrupted)
Logger.log("DiskCacheLevel| Failed fetching \(key.toString()) in path: \(path) on the disk cache", .info)
_ = try? self.fileManager.removeItem(atPath: path)
promise(.failure(FetchError.valueNotInCache))
}
}
.subscribe(on: cacheQueue)
.eraseToAnyPublisher()
}
public func remove(_ key: K) -> AnyPublisher {
AnyPublisher.create { [weak self] promise in
guard let path = self?.pathForKey(key) else {
return
}
do {
Logger.log("DiskCacheLevel| Removing \(key.toString()) at path: \(path) on the disk cache", .info)
try self?.fileManager.removeItem(atPath: path)
self?.calculateSize()
promise(.success(()))
} catch {
Logger.log("DiskCacheLevel| Failed to remove \(key.toString()) at path: \(path) on the disk cache", .error)
promise(.failure(error))
}
}
.subscribe(on: cacheQueue)
.eraseToAnyPublisher()
}
/**
Asynchronously clears the contents of the cache
All the cached files will be removed from the disk storage
*/
public func clear() {
cacheQueue.async {
self.itemsInDirectory(self.path).forEach { filePath in
_ = try? self.fileManager.removeItem(atPath: filePath)
}
self.calculateSize()
}
}
// MARK: Private
private func removeData(_ key: K) {
cacheQueue.async {
self.removeFileAtPath(self.pathForKey(key))
}
}
private func pathForKey(_ key: K) -> String {
let md5PathComponent = key.toString().MD5String()
let strippedMd5PathComponent = stripSpecialCharactersForPath(from: md5PathComponent)
return (path as NSString).appendingPathComponent(strippedMd5PathComponent)
}
private func stripSpecialCharactersForPath(from string: String) -> String {
let okayChars = Set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ1234567890")
return string.filter { okayChars.contains($0) }
}
private func sizeForFileAtPath(_ filePath: String) -> UInt64 {
var size: UInt64 = 0
do {
let attributes: NSDictionary = try fileManager.attributesOfItem(atPath: filePath) as NSDictionary
size = attributes.fileSize()
} catch {}
return size
}
private func calculateSize() {
size = itemsInDirectory(path).reduce(0) { accumulator, filePath in
accumulator + sizeForFileAtPath(filePath)
}
}
private func controlCapacity() {
if size > capacity {
enumerateContentsOfDirectorySortedByAscendingModificationDateAtPath(path) { (URL, stop: inout Bool) in
removeFileAtPath(URL.path)
stop = size <= capacity
}
}
}
private func setDataSync(_ data: T, key: K) -> AnyPublisher {
let path = pathForKey(key)
let previousSize = sizeForFileAtPath(path)
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: data, requiringSecureCoding: false)
try data.write(to: URL(fileURLWithPath: path), options: .atomicWrite)
_ = updateDiskAccessDateAtPath(path)
let newSize = sizeForFileAtPath(path)
if newSize > previousSize {
size += newSize - previousSize
controlCapacity()
} else {
size -= previousSize - newSize
}
return Just(())
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
} catch {
Logger.log("DiskCacheLevel| Failed to write key \(key.toString()) on the disk cache", .error)
return Fail(error: DiskCacheLevelError.diskArchiveWriteFailed).eraseToAnyPublisher()
}
}
private func updateDiskAccessDateAtPath(_ path: String) -> Bool {
var result = false
do {
try fileManager.setAttributes([
FileAttributeKey.modificationDate: Date()
], ofItemAtPath: path)
result = true
} catch _ {}
return result
}
private func removeFileAtPath(_ path: String) {
do {
if let attributes: NSDictionary = try fileManager.attributesOfItem(atPath: path) as NSDictionary? {
try fileManager.removeItem(atPath: path)
size -= attributes.fileSize()
}
} catch _ {}
}
private func itemsInDirectory(_ directory: String) -> [String] {
var items: [String] = []
do {
items = try fileManager.contentsOfDirectory(atPath: directory).map {
(directory as NSString).appendingPathComponent($0)
}
} catch _ {}
return items
}
private func enumerateContentsOfDirectorySortedByAscendingModificationDateAtPath(_ path: String, usingBlock block: (URL, inout Bool) -> Void) {
let property = URLResourceKey.contentModificationDateKey
do {
let directoryURL = URL(fileURLWithPath: path)
let contents = try fileManager.contentsOfDirectory(at: directoryURL, includingPropertiesForKeys: [property], options: [])
let sortedContents = contents.sorted(by: { URL1, URL2 in
var value1: AnyObject?
do {
try (URL1 as NSURL).getResourceValue(&value1, forKey: property)
} catch _ {
return true
}
var value2: AnyObject?
do {
try (URL2 as NSURL).getResourceValue(&value2, forKey: property)
} catch _ {
return false
}
if let date1 = value1 as? Date, let date2 = value2 as? Date {
return date1.compare(date2) == .orderedAscending
}
return false
})
for value in sortedContents {
var stop = false
block(value, &stop)
if stop {
break
}
}
} catch _ {}
}
}
================================================
FILE: Sources/Carlos/CacheLevels/Fetcher.swift
================================================
//
// Fetcher.swift
//
//
// Created by Lisovyi, Ivan on 24.10.20.
//
import Combine
import Foundation
/// An abstraction for a generic cache level that can only fetch values but not store them
public protocol Fetcher: CacheLevel {}
/// Extending the Fetcher protocol to have a default no-op implementation for clear, onMemoryWarning and set
extension Fetcher {
/// No-op
public func remove(_ key: KeyType) -> AnyPublisher {
Empty(completeImmediately: true).eraseToAnyPublisher()
}
/// No-op
public func clear() {}
/// No-op
public func onMemoryWarning() {}
/// No-op
public func set(_: OutputType, forKey _: KeyType) -> AnyPublisher {
Empty(completeImmediately: true).eraseToAnyPublisher()
}
}
================================================
FILE: Sources/Carlos/CacheLevels/MemoryCacheLevel.swift
================================================
import Combine
import Foundation
/// This class is a memory cache level. It internally uses NSCache, and has a configurable total cost limit that defaults to 50 MB.
public final class MemoryCacheLevel: CacheLevel where T: ExpensiveObject {
/// At the moment the memory cache level only accepts String keys
public typealias KeyType = K
public typealias OutputType = T
private let internalCache: NSCache
/**
Initializes a new memory cache level
- parameter cost: The total cost limit for the memory cache. Defaults to 50 MB
*/
public init(capacity: Int = 50 * 1024 * 1024) {
internalCache = NSCache()
internalCache.totalCostLimit = capacity
}
/**
Synchronously gets a value for the given key
- parameter key: The key for the value
- returns: A Future where you can call onSuccess and onFailure to be notified of the result of the fetch
*/
public func get(_ key: KeyType) -> AnyPublisher {
AnyPublisher.create { [weak self] promise in
if let result = self?.internalCache.object(forKey: key.toString() as NSString) as? T {
Logger.log("MemoryCacheLevel| Fetched \(key.toString()) on memory level.", .info)
promise(.success(result))
} else {
Logger.log("MemoryCacheLevel| Failed fetching \(key.toString()) on the memory cache.", .info)
promise(.failure(FetchError.valueNotInCache))
}
}
.eraseToAnyPublisher()
}
public func remove(_ key: K) -> AnyPublisher {
AnyPublisher.create { [weak self] promise in
Logger.log("MemoryCacheLevel| Removing \(key.toString()) on memory level.", .info)
self?.internalCache.removeObject(forKey: key.toString() as NSString)
promise(.success(()))
}
}
/**
Clears the contents of the cache
*/
public func onMemoryWarning() {
clear()
}
/**
Sets a value for the given key
- parameter value: The value to set
- parameter key: The key for the value
*/
public func set(_ value: T, forKey key: K) -> AnyPublisher {
AnyPublisher.create { [weak self] promise in
self?.internalCache.setObject(value, forKey: key.toString() as NSString, cost: value.cost)
promise(.success(()))
}
}
/**
Clears the contents of the cache
*/
public func clear() {
internalCache.removeAllObjects()
}
}
================================================
FILE: Sources/Carlos/CacheLevels/NSUserDefaultsCacheLevel.swift
================================================
import Combine
import Foundation
/**
Default name for the persistent domain used by the NSUserDefaultsCacheLevel
Keep in mind that using this domain for multiple cache levels at the same time could lead to undesired results!
For example, if one of the cache levels get cleared, also the other will be affected unless they save something before leaving the app.
The behavior is not 100% certain and this possibility is discouraged.
*/
public let DefaultUserDefaultsDomainName = "CarlosPersistentDomain"
/// This class is a NSUserDefaults cache level. It has a configurable domain name so that multiple levels can be included in the same sandboxed app.
public final class NSUserDefaultsCacheLevel: CacheLevel {
/// The key type of the cache, should be convertible to String values
public typealias KeyType = K
/// The output type of the cache, should conform to NSCoding
public typealias OutputType = T
private let domainName: String
private let lock: UnfairLock
private let userDefaults: UserDefaults
private var internalDomain: [String: Data]?
private var safeInternalDomain: [String: Data] {
if let internalDomain = internalDomain {
return internalDomain
} else {
let fetchedDomain = (userDefaults.persistentDomain(forName: domainName) as? [String: Data]) ?? [:]
internalDomain = fetchedDomain
return fetchedDomain
}
}
/**
Creates a new instance of this NSUserDefaults-based cache level.
- parameter name: The name to use for the persistent domain on NSUserDefaults. Should be unique in your sandboxed app
*/
public init(name: String = DefaultUserDefaultsDomainName) {
domainName = name
lock = UnfairLock()
userDefaults = UserDefaults.standard
internalDomain = safeInternalDomain
}
/**
Sets a new value for the given key
- parameter value: The value to set for the given key
- parameter key: The key you want to set
This method will convert the value to NSData by using NSCoding and save the data on the persistent domain.
A soft-cache is used to avoid hitting the persistent domain everytime you are going to fetch values from this cache. The operation is thread-safe
*/
public func set(_ value: OutputType, forKey key: KeyType) -> AnyPublisher {
AnyPublisher.create { [weak self] promise in
guard let self = self else {
return
}
var softCache = self.safeInternalDomain
Logger.log("Setting a value for the key \(key.toString()) on the user defaults cache \(self)")
if let data = try? NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: false) {
softCache[key.toString()] = data
self.internalDomain = softCache
self.userDefaults.setPersistentDomain(softCache, forName: self.domainName)
promise(.success(()))
} else {
Logger.log("Failed setting a value for the key \(key.toString()) on the user defaults cache \(self)")
promise(.failure(FetchError.invalidCachedData))
}
}
.eraseToAnyPublisher()
}
/**
Fetches a value on the persistent domain for the given key
- parameter key: The key you want to fetch
- returns: The result of this fetch on the cache
A soft-cache is used to avoid hitting the persistent domain everytime. This operation is thread-safe
*/
public func get(_ key: KeyType) -> AnyPublisher {
AnyPublisher.create { [weak self] promise in
guard let self = self else {
return
}
if let cachedValue = self.safeInternalDomain[key.toString()] {
if let unencodedObject = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(cachedValue) as? T {
Logger.log("Fetched \(key.toString()) on user defaults level (domain \(self.domainName)")
promise(.success(unencodedObject))
} else {
Logger.log("Failed fetching \(key.toString()) on the user defaults cache (domain \(self.domainName), corrupted data")
promise(.failure(FetchError.invalidCachedData))
}
} else {
Logger.log("Failed fetching \(key.toString()) on the user defaults cache (domain \(self.domainName), no data")
promise(.failure(FetchError.valueNotInCache))
}
}
.eraseToAnyPublisher()
}
public func remove(_ key: K) -> AnyPublisher {
AnyPublisher.create { [weak self] promise in
self?.userDefaults.removeObject(forKey: key.toString())
promise(.success(()))
}
}
/**
Completely clears the contents of this cache
Please keep in mind that if the same name is used for multiple cache levels, the contents of these caches will also be cleared, at least from a persistence point of view.
The soft caches of the other levels will still contain consistent values, though, so setting a value on one of these levels will result in the whole previous content of the cache to be persisted on NSUserDefaults, even this may or may not be the desired behavior.
The conclusion is that you should only use the same name for multiple cache levels if you are aware of the consequences. In general the behavior may not be the expected one.
The operation is thread-safe
*/
public func clear() {
lock.locked {
userDefaults.removePersistentDomain(forName: domainName)
internalDomain = [:]
}
}
/**
Clears the contents of the soft cache for this cache level.
Fetching or setting a value after this call is safe, since the content will be pre-fetched from the disk immediately before.
The operation is thread-safe
*/
public func onMemoryWarning() {
lock.locked {
internalDomain = nil
}
}
}
================================================
FILE: Sources/Carlos/CacheLevels/NetworkFetcher.swift
================================================
import Foundation
import Combine
public enum NetworkFetcherError: Error {
/// Used when the status code of the network response is not included in the range 200..<300
case statusCodeNotOk
/// Used when the network response had an invalid size
case invalidNetworkResponse
/// Used when the network request didn't manage to retrieve data
case noDataRetrieved
}
/// This class is a network cache level, mostly acting as a fetcher (meaning that calls to the set method won't have any effect). It internally uses NSURLSession to retrieve values from the internet
open class NetworkFetcher: Fetcher {
/// The network cache accepts only NSURL keys
public typealias KeyType = URL
/// The network cache returns only NSData values
public typealias OutputType = NSData
/**
Initializes a new instance of a NetworkFetcher
*/
public init() {}
/**
Asks the cache to get a value for the given key
- parameter key: The key for the value. It represents the URL to fetch the value
- returns: A Future that you can use to get the asynchronous results of the network fetch
*/
open func get(_ key: KeyType) -> AnyPublisher {
URLSession.shared.dataTaskPublisher(for: key)
.tryMap { [weak self] data, response -> NSData in
guard let response = response as? HTTPURLResponse else {
throw NetworkFetcherError.invalidNetworkResponse
}
guard 200..<300 ~= response.statusCode else {
throw NetworkFetcherError.statusCodeNotOk
}
if self?.validate(response, withData: data) == true {
return data as NSData
}
throw NetworkFetcherError.invalidNetworkResponse
}
.eraseToAnyPublisher()
}
private func validate(_ response: HTTPURLResponse, withData data: Data) -> Bool {
var responseIsValid = true
let expectedContentLength = response.expectedContentLength
if expectedContentLength > -1 {
responseIsValid = Int64(data.count) >= expectedContentLength
}
return responseIsValid
}
}
================================================
FILE: Sources/Carlos/CacheLevels/PoolCache.swift
================================================
import Combine
import Foundation
extension CacheLevel where KeyType: Hashable {
/// Wraps the CacheLevel with a requests pool
///
/// - Returns: A `PoolCache` that will pool requests coming to the decorated cache.
/// This means that multiple requests for the same key will be pooled and only one will be actually done
/// (so that expensive operations like network or file system fetches will only be done once).
public func pooled() -> PoolCache {
PoolCache(internalCache: self)
}
}
/// A CacheLevel that pools incoming get requests.
///
/// This means that multiple requests for the same key will be pooled and only one will be actually executed
/// (so that expensive operations like network or file system fetches will only be done once).
public final class PoolCache: CacheLevel where C.KeyType: Hashable {
public typealias KeyType = C.KeyType
public typealias OutputType = C.OutputType
private let internalCache: C
private let lock: UnfairLock
private var requestsPool: [C.KeyType: AnyPublisher] = [:]
/// Creates a new instance of a pooled cache
///
/// - Parameter internalCache: The CacheLevel instance that this pooled cache will manage
public init(internalCache: C) {
self.internalCache = internalCache
lock = UnfairLock()
}
/// Asks the cache to get the value for the given key
///
/// - Parameter key: The key for the value
///
/// - Returns: A `Publisher` that could either have been just created or it could have been reused from a pool of pending Publishers
/// if there is a Publisher for the same key going on at the moment.
public func get(_ key: KeyType) -> AnyPublisher