'How to add List of Object in Proto Datastore and Observe in view model
I'm trying to add a list of userserData object' in the proto datastore.
here is my protobuf schema
syntax = "proto3";
option java_package = "com.wkt.distriware";
option java_multiple_files = true;
message LoginResponse {
message UserData {
string username = 1;
string userType = 2;
string token = 3;
bool isActive = 4;
}
repeated UserData usersData = 1;
}
And this is the class that is responsible for updating and getting the userData.
class LoginResProtoDataStore(
context: Context
) {
private val dataStore = context.loginResponseDataStore
val userData: Flow<LoginResponse.UserData> = dataStore.data
.map { loginResponse ->
val userDataObject = LoginResponse.UserData.getDefaultInstance()
loginResponse.usersDataList.map { user ->
if(user.isActive) {
val userDataBuilder = userDataObject.toBuilder()
userDataBuilder.username = user.username
userDataBuilder.userType = user.userType
userDataBuilder.token = user.token
}
}
userDataObject
}
suspend fun updateUserData(user: User) {
dataStore.updateData { loginResponse ->
val userData = LoginResponse.UserData.newBuilder()
userData.username = user.username
userData.userType = user.userType
userData.token = user.token
loginResponse.toBuilder()
.addUsersData(userData).build()
}
}
}
The LoginResponse.UserData is generated class from protobuf schema. What I want here to do is to update the list in the proto datastore and observe using Kotlin Flow as live data. I can tell that the userdata is saving in that proto datastore but the problem is I don't know why the observer is not triggered every time the proto datastore is updated.
Thak you in advance :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
