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 aScheduler = SerialDispatchQueueScheduler(internalSerialQueueName: "backgroundQueue1") | |
let A = Observable.repeatElement("A", scheduler: aScheduler) | |
.throttle(.seconds(2), scheduler: aScheduler) | |
.share() | |
/// Observing every fourth second means first 3 outputs will be clubbed | |
/// Notice the count as 0, it means to send all the events in the interval | |
/// Why don't you experiment with count and put 2 there and see if it starts losing events? | |
_ = A.buffer(timeSpan: .seconds(4), count: 0, scheduler: MainScheduler.instance) | |
.subscribe(onNext: { value 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
/// We'll create a random Int emitting infinite Observable and | |
/// apply out lossy operators on it | |
let X = Observable<Int>.create { observer -> Disposable in | |
while true { | |
let randomInt = UInt32.random(in: 1...3) | |
print("Waiting for \(randomInt) second(s) to produce \(randomInt)") | |
sleep(randomInt) | |
observer.onNext(Int(randomInt)) | |
} | |
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 aScheduler = SerialDispatchQueueScheduler(internalSerialQueueName: "backgroundQueue1") | |
let A = Observable.repeatElement("A", scheduler: aScheduler).throttle(.seconds(1), scheduler: aScheduler) | |
let bScheduler = SerialDispatchQueueScheduler(internalSerialQueueName: "backgroundQueue2") | |
let B = Observable.repeatElement("B", scheduler: bScheduler).throttle(.seconds(2), scheduler: bScheduler) | |
_ = Observable.zip(A, B) | |
.subscribe(onNext: { (valueA, valueB) in | |
print("\(valueA) \(valueB)") | |
}) |
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 AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
let delegator = ResponsibilityDelegator(workers: [UserNotificationAppDelegate(), LocationAppDelegate()]) | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
delegator.application(application, didFinishLaunchingWithOptions: launchOptions) | |
return true | |
} |
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 LocationAppDelegate: NSObject, UIApplicationDelegate { | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil) -> Bool { | |
// initialize location manager | |
return true | |
} | |
func applicationWillEnterForeground(_ application: UIApplication) { | |
// start tracking |
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 UserNotificationAppDelegate: NSObject, UIApplicationDelegate { | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil) -> Bool { | |
// check if app lauched via notification | |
return true | |
} | |
func applicationWillTerminate(_ application: UIApplication) { | |
// schedule a local notification |
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 | |
import UIKit | |
class ResponsibilityDelegator: NSObject { | |
var workers: [UIApplicationDelegate] | |
init(workers: [UIApplicationDelegate]) { | |
self.workers = workers | |
} |
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
struct Spaceship: Codable { | |
var name: String | |
var model: String | |
} | |
enum SpaceshipPlanet { | |
case spaceship(Spaceship) | |
case planet(String) | |
} |
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
[ | |
{ | |
"spaceship": { | |
"name": "Imperial shuttle", | |
"model": "Lambda-class T-4a shuttle" | |
} | |
}, | |
{ | |
"planet": "Alderaan" | |
}, |
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
struct Spaceship: Codable { | |
var model: String | |
var movie: String | |
} | |
//... | |
let jsonDecoder = JSONDecoder() | |
do { | |
let spaceships = try jsonDecoder.decode([String: Spaceship].self, from: jsonData!) |
NewerOlder