提交 98fc4df9 编写于 作者: G guolindev

Add runInTransaction extension API for Kotlin.

上级 e3c00364
......@@ -19,6 +19,7 @@ package org.litepal.extension
import android.content.ContentValues
import org.litepal.LitePal
import org.litepal.crud.LitePalSupport
import java.lang.Exception
/**
* Extension of LitePal class for Kotlin api.
......@@ -616,4 +617,22 @@ inline fun <reified T> LitePal.isExist(vararg conditions: String?) = isExist(T::
* @param collection
* Holds all models to save.
*/
fun <T : LitePalSupport> Collection<T>.saveAll() = LitePal.saveAll(this)
\ No newline at end of file
fun <T : LitePalSupport> Collection<T>.saveAll() = LitePal.saveAll(this)
/**
* Open a transaction scope, all codes in the lambda will under transaction.
* If lambda return true, all db operations in lambda will be committed.
* Otherwise all db operations will be rolled back.
*/
inline fun LitePal.runInTransaction(block: () -> Boolean) {
beginTransaction()
val succeeded = try {
block()
} catch (e: Exception) {
false
}
if (succeeded) {
setTransactionSuccessful()
}
endTransaction()
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册