我正在为我的应用程序创建登录名,并希望保存从服务器检索到的用户详细信息。我正在使用mvvm。如何将收到的数据保存到roomdb中?
这是服务器发出的我的回复
{
"isSuccessful": true,
"message": "successful",
"user": [
{
"name": "Raymond Jezz",
"email": "rayjezz@gmail.com",
"phone": "254XXXXXXX"
}
]
}
我的用户模型
data class User(
val name: String,
val email:String,
val phone:String
)
登录响应模型
data class LoginResponse(
val isSuccessful:Boolean,
val message: String,
val user: List<User>
)
房间实体
@Entity(tableName = "user_table")
data class UserRoom(val name:String, val email:String, val phone:String) {
@PrimaryKey(autoGenerate = true)
var id: Int = 0
}
道课
@Dao
interface UserDao {
@Insert
fun insert(userRoom: UserRoom)
@Query("SELECT * FROM user_table")
fun getUserDetails(): LiveData<List<UserRoom>>
}
RoomDatabase
@Database(entities = [UserRoom::class], version = 1)
abstract class UserDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
private var instance: UserDatabase? = null
fun getInstance( context: Context): UserDatabase? {
if (instance == null) {
synchronized(UserDatabase::class.java) {
instance = Room.databaseBuilder(context.applicationContext, UserDatabase::class.java, "users_database")
.fallbackToDestructiveMigration()
.build()
}
}
return instance
}
}
}
存储库类
class UserRepository(application: Application) {
private lateinit var userDao: UserDao
private var userDetails: LiveData<List<UserRoom>>
init {
val database: UserDatabase? = UserDatabase.getInstance(application)
if (database != null) {
userDao = database.userDao()
}
userDetails = userDao.getUserDetails()
}
fun insert(userRoom: UserRoom){
val insertUserAsyncTask = InsertUserAsyncTask(userDao).execute(userRoom)
}
fun getUserDetails():LiveData<List<UserRoom>>{
return userDetails
}
private class InsertUserAsyncTask(userDao: UserDao) : AsyncTask<UserRoom, Unit, Unit>() {
val userDao = userDao
override fun doInBackground(vararg p0: UserRoom?) {
userDao.insert(p0[0]!!)
}
}
}
查看模型类
class LoginViewModel : ViewModel() {
fun userLogin(email: String, password: String): LiveData<String> {
val loginResponse = MutableLiveData<String>()
RetrofitClient.makeRetrofitApi().userLogin(email, password)
.enqueue(object : Callback<LoginResponse> {
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
loginResponse.value = t.message
}
override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
if (response.isSuccessful){
loginResponse.value = response.body()?.message.toString()
}else{
loginResponse.value = response.body()?.message.toString()
}
}
})
return loginResponse
}
}
答案 0 :(得分:1)
首先,在您的UserDao
中创建一个insertAll()
函数。
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAllUsers(userRoomList : List<UserRoom>);
然后,您应该编写适当的AsyncTask
类以将用户列表插入数据库。
class LoginViewModel : ViewModel() {
fun userLogin(email: String, password: String): LiveData<String> {
val loginResponse = MutableLiveData<String>()
RetrofitClient.makeRetrofitApi().userLogin(email, password).enqueue(object : Callback<LoginResponse> {
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
loginResponse.value = t.message
}
override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
if (response.isSuccessful) {
loginResponse.value = response.body()?.message.toString()
// create a List of User and get it by response.body().getUserList()
//iterate over this list and create another list of UserRoom class
//insert it into the database using the Repository class.
} else
loginResponse.value = response.body()?.message.toString()
}
});
return loginResponse
}
}