gpt4 book ai didi

android - 使用改造从服务器获取数据后如何将数据存储在房间数据库中

转载 作者:行者123 更新时间:2023-11-29 22:46:25 26 4
gpt4 key购买 nike

我正在为我的应用程序创建一个登录名,并希望保存从服务器检索到的用户详细信息。我正在使用 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>>
}

房间数据库

@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
}
}

最佳答案

首先,在您的 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
}
}

关于android - 使用改造从服务器获取数据后如何将数据存储在房间数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58363459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com