Created
June 14, 2020 21:44
-
-
Save standinga/45c2d6129a9fbf56a804d88b58560379 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import PlaygroundSupport | |
class MyViewController : UIViewController { | |
override func loadView() { | |
let view = UIView() | |
view.backgroundColor = .white | |
let label = UILabel() | |
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20) | |
label.text = "Hello World!" | |
label.textColor = .black | |
view.addSubview(label) | |
let button = UIButton() | |
button.frame = CGRect(x: 150, y: 240, width: 200, height: 20) | |
button.setTitle("Fetch", for: .normal) | |
button.backgroundColor = .systemBlue | |
button.addTarget(self, action: #selector(fetchAction), for: .touchUpInside) | |
view.addSubview(button) | |
self.view = view | |
} | |
@objc func fetchAction() { | |
print() | |
fetch(title: "useProtocolCachePolicy", policy: .useProtocolCachePolicy) | |
fetch(title: "returnCacheDataElseLoad", policy: .returnCacheDataElseLoad) | |
fetch(title: "reloadIgnoringLocalCacheData", policy: .reloadIgnoringLocalCacheData) | |
fetch(title: "reloadRevalidatingCacheData", policy: .reloadRevalidatingCacheData) | |
} | |
func fetch(title: String, policy: NSURLRequest.CachePolicy) { | |
let configuration = URLSessionConfiguration.default | |
configuration.requestCachePolicy = policy | |
let urlSession = URLSession(configuration: configuration) | |
var request = URLRequest(url: URL(string: "http://localhost:3000")!) | |
request.cachePolicy = policy | |
urlSession.dataTask(with: request) { data, response, error in | |
guard let data = data else { return } | |
print(title, String(data: data, encoding: .utf8)!) | |
}.resume() | |
} | |
} | |
// Present the view controller in the Live View window | |
PlaygroundPage.current.liveView = MyViewController() | |
PlaygroundPage.current.needsIndefiniteExecution = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment