kotlin.runCatching()用法
private fun doDelayAppUse(activity: Activity) {
viewModelScope.launch {
kotlin.runCatching {
Result.success(
PhoneDataManager.instance().applyAppDelayUse(ApplyDelayAppBean().apply {
deviceId = DeviceInfoUtils.getChipId(activity)
pkgName = mPkgName
minute = _selectTimeInMinutes
appName = mAppName
})
)
}.onSuccess {
it.onSuccess { response ->
if (response.isSuccessful) {
setApplyTimestamp(
activity,
mPkgName,
System.currentTimeMillis()
)
} else {
onError(response.code, response.message)
}
Toast.makeText(activity, response.message, Toast.LENGTH_SHORT).show()
}
knowBtnClick(activity)
}.onFailure {
onFailure(it, activity)
knowBtnClick(activity)
}
}
}
PhoneDataManager.kt
override suspend fun applyAppDelayUse(applyDelayAppBean: ApplyDelayAppBean): ResponseBean<Void> {
return mRemoteRepository.applyAppDelayUse(applyDelayAppBean)
}
Result.kt
public value class Result<out T> @PublishedApi internal constructor(
@PublishedApi
internal val value: Any?
) : Serializable {
// discovery
/**
* Returns `true` if this instance represents a successful outcome.
* In this case [isFailure] returns `false`.
*/
public val isSuccess: Boolean get() = value !is Failure
/**
* Returns `true` if this instance represents a failed outcome.
* In this case [isSuccess] returns `false`.
*/
public val isFailure: Boolean get() = value is Failure
// companion with constructors
/**
* Companion object for [Result] class that contains its constructor functions
* [success] and [failure].
*/
public companion object {
/**
* Returns an instance that encapsulates the given [value] as successful value.
*/
@Suppress("INAPPLICABLE_JVM_NAME")
@InlineOnly
@JvmName("success")
public inline fun <T> success(value: T): Result<T> =
Result(value)
/**
* Returns an instance that encapsulates the given [Throwable] [exception] as failure.
*/
@Suppress("INAPPLICABLE_JVM_NAME")
@InlineOnly
@JvmName("failure")
public inline fun <T> failure(exception: Throwable): Result<T> =
Result(createFailure(exception))
}
internal class Failure(
@JvmField
val exception: Throwable
) : Serializable {
override fun equals(other: Any?): Boolean = other is Failure && exception == other.exception
override fun hashCode(): Int = exception.hashCode()
override fun toString(): String = "Failure($exception)"
}
}
@PublishedApi
@SinceKotlin("1.3")
internal fun createFailure(exception: Throwable): Any =
Result.Failure(exception)
@InlineOnly
@SinceKotlin("1.3")
public inline fun <T> Result<T>.onFailure(action: (exception: Throwable) -> Unit): Result<T> {
contract {
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
}
exceptionOrNull()?.let { action(it) }
return this
}
/**
* Performs the given [action] on the encapsulated value if this instance represents [success][Result.isSuccess].
* Returns the original `Result` unchanged.
*/
@InlineOnly
@SinceKotlin("1.3")
public inline fun <T> Result<T>.onSuccess(action: (value: T) -> Unit): Result<T> {
contract {
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
}
if (isSuccess) action(value as T)
return this
}
kotlin.runCatching
@InlineOnly
@SinceKotlin("1.3")
public inline fun <R> runCatching(block: () -> R): Result<R> {
return try {
Result.success(block())
} catch (e: Throwable) {
Result.failure(e)
}
}
block: () -> R 传入的就是doDelayAppUse方法中viewModelScope.launch代码块中的内容
1. block执行完毕后:
如果没有异常就返回Result<Result<ResponseBean<Void>>>
如果有异常就返回Result<Failure<Throwable>>
2. kotlin.runCatching {}执行完毕后返回Result对象, 紧接着调用onSucess
public inline fun <T> Result<T>.onSuccess(action: (value: T) -> Unit): Result<T> {
contract {
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
}
if (isSuccess) action(value as T)
return this
}
如果Result中的value 不是Failure对象就执行action
public val isSuccess: Boolean get() = value !is Failure
3. onSuccess执行完毕后再执行onFailure
public inline fun <T> Result<T>.onFailure(action: (exception: Throwable) -> Unit): Result<T> {
contract {
callsInPlace(action, InvocationKind.AT_MOST_ONCE)
}
exceptionOrNull()?.let { action(it) }
return this
}
如果Result中的value是Failure就执行action