Skip to content

Instantly share code, notes, and snippets.

View yukin01's full-sized avatar

Yuji Kinjo yukin01

  • Tokyo / Japan
View GitHub Profile
@yukin01
yukin01 / LoggerLikeRx.swift
Created February 6, 2019 08:22
RxSwift の Debug に寄せたロガー(メインスレッド判定もできる)
enum Logger {
static let dateFormatter: DateFormatter = {
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
return df
}()
static func log(_ item: String, file: String = #file, line: UInt = #line, function: String = #function) {
printLikeRxDebug(item, file, line, function)
}
static func isMainThread(file: String = #file, line: UInt = #line, function: String = #function) {
@yukin01
yukin01 / synchronize.swift
Created December 30, 2018 13:53
Single to Taple in RxSwift
func synchronize<T>(_ single: Single<T>) -> (element: T?, error: Error?) {
var element: T?
var error: Error?
let condition = NSCondition()
condition.lock()
_ = single.subscribe({ event in
condition.lock()
switch event {
case .success(let elm): element = elm
case .error(let err): error = err
@yukin01
yukin01 / RxRequest.swift
Last active April 29, 2023 20:31
Simple APIClient with RxSwift + URLSession + Codable
protocol RxRequestProtocol {
func get<U: Decodable>(returnType: U.Type, path: String, query: [String: String]?, token: String?) -> Observable<U>
func get(path: String, query: [String: String]?, token: String?) -> Observable<Void>
func post<T: Encodable, U: Decodable>(returnType: U.Type, path: String, query: [String: String]?, body: T, token: String?) -> Observable<U>
func post<T: Encodable>(path: String, query: [String: String]?, body: T, token: String?) -> Observable<Void>
func put<T: Encodable, U: Decodable>(returnType: U.Type, path: String, query: [String: String]?, body: T, token: String?) -> Observable<U>
func put<T: Encodable>(path: String, query: [String: String]?, body: T, token: String?) -> Observable<Void>
func delete<U: Decodable>(returnType: U.Type, path: String, query: [String: String]?, token: String?) -> Observable<U>
func delete(path: String, query: [String: String]?, token: String?) -> Observable<Void>