IOS Stuff

From PeformIQ Upgrade
Jump to navigation Jump to search
func request<T: Codable>(_ request: URLRequest) -> Observable<T> {
    return Observable<T>.create { observer in
        // blah blah blah
        let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
            do {
                let model: T = try JSONDecoder().decode(T.self, from: data ?? Data())
                observer.onNext(model)
            } catch let error {
                observer.onError(error)
            }
            observer.onCompleted()
        }
        task.resume()
        
        return Disposables.create {
            task.cancel()
        }
    }
}