This file contains 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
func multipleHeavyAsynchronousCallBackTask(start: Int, end: Int, completionHandler: @escaping ([Int]) -> Void) { | |
//1. Gets the array of numbers (A dummy heavy operation) | |
HeavyOperationApi.shared.heavyNumberArray(from: start, to: end) { numbers in | |
//2. Using the numbers array to fillter for Even number (A Dummy heavy operation) | |
HeavyOperationApi.shared.heavyFilterEvenNumberFrom(arrayOf: numbers) { evenNumbers in | |
// 3. Doing Random mapping of even numbers array (A Dummy Heavy operation) | |
HeavyOperationApi.shared.heavyRandomMapping(arrayOf: evenNumbers) { number in | |
return number + 10000000 | |
} completion: { transformedNumbrs in | |
completionHandler(transformedNumbrs) |
This file contains 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 Foundation | |
func printWithThreadInfo(tag: String) { | |
print("Thread \(Thread.current) - \(tag) - \(Thread.isMainThread)") | |
} | |
class SomeHeavyOperationTasks { | |
func someHeavySynchronousTask() -> [Int] { | |
var number: [Int] = [] |
This file contains 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
class WebApiClient: RequestExecutor { | |
private var configuration: URLSessionConfiguration | |
private var session: URLSession | |
init(sessionConfiguration: URLSessionConfiguration = URLSessionConfiguration.default) { | |
configuration = URLSession.shared.configuration | |
session = URLSession(configuration: configuration) |
This file contains 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
// Example of One-Shot piblisher with Stream Failure. | |
// As we find the value 0 in the stream, we throw an error. This error is eventually catched and replaced with Just(-1) publisher. | |
// But after a failed value will activates the catch operator and the pipeline will cease to react further. | |
struct SimpleError: Error {} | |
let numbers = [5, 4, 3, 2, 1, 0, 9, 8, 7, 6] | |
let cancellable = numbers.publisher | |
.tryMap({ val in | |
guard val != 0 else {throw SimpleError()} |
This file contains 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
enum APIError: Error, LocalizedError { | |
case invalidServerResponse | |
case unknown | |
var errorDescription: String? { | |
switch self { | |
case .unknown: | |
return "Unknown error" | |
case .invalidServerResponse: | |
return "Invalid Server Response" |
This file contains 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
var cancellable: AnyCancellable? | |
let url = URL(string: "https://jsonplaceholder.typicode.com/posts")! | |
cancellable = URLSession.shared.dataTaskPublisher(for: url) | |
.map { $0.data } // 1 | |
.decode(type: [Post].self, decoder: JSONDecoder()) // 2 | |
.replaceError(with: []) // 3 | |
.eraseToAnyPublisher() // 4 | |
.sink(receiveValue: { posts in |
This file contains 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
let urlSessionPublisher = URLSession.shared.dataTaskPublisher(for: url)//.map { $0.data } | |
let subscription = urlSessionPublisher.sink(receiveCompletion: { (error) in | |
print("error \(error)") | |
}) { (data,response) in | |
print("Data \(String(data: data, encoding: .utf8)) - Response - \(response)" ) | |
} |
This file contains 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
// An AssociatedType Protocol: Request type to be implemented by Concrete Requests | |
protocol Request { | |
associatedtype Response: Decodable // 1. A Response Type constrained to be decodable | |
associatedtype ResponseParser: ResponseParserType where ResponseParser.Response == Response // 2 Response parser contrained to be confirming to ResponseParserType | |
// 3 Basic properties required by a request | |
var baseUrl: String { get } | |
var path: String { get } | |
var method: String { get } | |
var header: [String: String]? { get } |
This file contains 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
class WebApiClient: RequestExecutor { | |
private var configuration: URLSessionConfiguration | |
private var session: URLSession | |
init(sessionConfiguration: URLSessionConfiguration = URLSessionConfiguration.default) { | |
configuration = URLSession.shared.configuration | |
session = URLSession(configuration: configuration) |
This file contains 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
// An AssociatedType Protocol: Request type to be implemented by Concrete Requests | |
protocol Request { | |
associatedtype Response: Decodable // 1. A Response Type constrained to be decodable | |
associatedtype ResponseParser: ResponseParserType where ResponseParser.Response == Response // 2 Response parser contrained to be confirming to ResponseParserType | |
// 3 Basic properties required by a request | |
var baseUrl: String { get } | |
var path: String { get } | |
var method: String { get } | |
var header: [String: String]? { get } |
NewerOlder