IOS Stuff

From PeformIQ Upgrade
Revision as of 23:40, 15 February 2019 by PeterHarding (talk | contribs) (Created page with " <pre> func request<T: Codable>(_ request: URLRequest) -> Observable<T> { return Observable<T>.create { observer in // blah blah blah let task = URLSession...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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()
        }
    }
}