Skip to content

Instantly share code, notes, and snippets.

View Ravi61's full-sized avatar

Ravi Kumar Aggarwal Ravi61

View GitHub Profile
@Ravi61
Ravi61 / Lossless.swift
Created August 18, 2019 16:06
Lossless solutions for backpressure
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
@Ravi61
Ravi61 / Lossy.swift
Created August 18, 2019 15:54
Lossy operations for backpressure
/// 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))
}
@Ravi61
Ravi61 / ZipExample.swift
Created August 18, 2019 12:07
Zipping two Observables to explain Backpressure
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)")
})
@Ravi61
Ravi61 / AppDelegate.swift
Last active October 6, 2017 12:38
Code for AppDelegate
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
}
@Ravi61
Ravi61 / LocationAppDelegate.swift
Last active October 6, 2017 12:35
Code for LocationAppDelegate
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
@Ravi61
Ravi61 / UserNotificationDelegate.swift
Last active October 6, 2017 12:35
Code for UserNotificationDelegate
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
@Ravi61
Ravi61 / ResponsibilityDelegator.swift
Last active March 18, 2018 10:51
Code for ResponsibilityDelegator
import Foundation
import UIKit
class ResponsibilityDelegator: NSObject {
var workers: [UIApplicationDelegate]
init(workers: [UIApplicationDelegate]) {
self.workers = workers
}
@Ravi61
Ravi61 / SpaceshipPlanet.swift
Created July 13, 2017 19:08
Code to parse JSON in an enum
struct Spaceship: Codable {
var name: String
var model: String
}
enum SpaceshipPlanet {
case spaceship(Spaceship)
case planet(String)
}
@Ravi61
Ravi61 / SpaceshipPlanet.json
Created July 13, 2017 18:56
JSON for using enum in parsing
[
{
"spaceship": {
"name": "Imperial shuttle",
"model": "Lambda-class T-4a shuttle"
}
},
{
"planet": "Alderaan"
},
@Ravi61
Ravi61 / DictionaryDecoding.swift
Created July 13, 2017 18:42
Model for Dictionary decoding
struct Spaceship: Codable {
var model: String
var movie: String
}
//...
let jsonDecoder = JSONDecoder()
do {
let spaceships = try jsonDecoder.decode([String: Spaceship].self, from: jsonData!)