Last active
October 4, 2016 05:10
-
-
Save kenichi-odo/e3d766e0b1473757909cc23c8126c250 to your computer and use it in GitHub Desktop.
RxAndroidのObservableを使った非同期処理を簡略にするメモ(Kotlin) ref: http://qiita.com/kenichi_odo/items/5b21495b03bd20e3224a
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 Thread<Type_> { | |
fun run(Background_: ((Type_?) -> Any) -> Any, Main_: (Type_?, Throwable?) -> Unit) { | |
var result: Type_? = null | |
var err: Throwable? = null | |
Observable | |
.create<Type_> { subscriber_ -> | |
Background_ { value_ -> | |
subscriber_.onNext(value_) | |
subscriber_.onCompleted() | |
} | |
} | |
.subscribeOn(Schedulers.newThread()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe({ result_ -> | |
result = result_ | |
}, { err_: Throwable -> | |
err = err_ | |
}, { | |
Main_(result, err) | |
}) | |
} | |
} |
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
Thread<String>().run( | |
Background_ = { Callback_ -> | |
val result = "" // 重い処理 | |
Callback_(result) | |
}, | |
Main_ = { result_, error_ -> | |
when (error_) { | |
null -> { | |
hoge(result_) // 結果受け取り | |
} | |
else -> { | |
// エラー処理 | |
} | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment