java - Android arrayList does not perserve value when onChange is called
问题描述
I have the following observer to monitor a table change, when a change is trigger, it will call api to sync user information. This logic does the work but it happens that the OnChange call multiple time with same change set so it can make multiple api call which is not needed. I added a global list to capture inProcess items but the list is not synchronized between each of the OnChange call. Here is the code:
userViewModel.getAllNewUsers().observe(getActivity(), new Observer<List<User>>() {
@Override
public void onChanged(@Nullable List<User> users) {
Log.d(logTag, "Observeed " + users.size() + " new users, calling updateNewUsers");
List<User> userSyncList = new ArrayList<>();
Log.d(logTag, "userProcessingList size " + userProcessingList.size());
Log.d(logTag, "users size " + users.size());
if (userProcessingList.size() == 0 && users.size() > 0) {
Log.d(logTag, "Nothing is processing, let's sync all users");
userProcessingList.addAll(users);
userSyncList.addAll(users);
Log.d(logTag, "new userProcessingList size " + userProcessingList.size());
Log.d(logTag, "userSyncList " + userProcessingList.toString());
} else if (users.size() > 0) {
for (User user : users) {
if (!userProcessingList.contains(user)) {
Log.d(logTag, "userProcessingList is " + userProcessingList.toString());
userProcessingList.add(user);
userSyncList.add(user);
} else {
Log.d(logTag, "User already in syncing process, ignoring...");
}
}
}
if (userSyncList.size() > 0) {
updateNewUsers(userSyncList);
}
if (users.size() == 0 && userProcessingList.size() > 0) {
Log.d(logTag, "all users are in sync, let empty out the processing list");
userProcessingList.clear();
}
Log.d(logTag, "at the end userProcessingList size " + userProcessingList.size());
}
});
here is the log
09-16 23:35:34.040 5956-5956: Observeed 0 new users, calling updateNewUsers
09-16 23:35:34.040 5956-5956: userProcessingList size 0
09-16 23:35:34.040 5956-5956: users size 0
09-16 23:35:34.040 5956-5956: at the end userProcessingList size 0
09-16 23:35:34.047 5956-5956: Observeed 2 new users, calling updateNewUsers
09-16 23:35:34.047 5956-5956: userProcessingList size 0
09-16 23:35:34.047 5956-5956: users size 2
09-16 23:35:34.048 5956-5956: Nothing is processing, let's sync all users
09-16 23:35:34.048 5956-5956: new userProcessingList size 2 <-- THE LIST SIZE IS NOW 2
09-16 23:35:34.048 5956-5956: in updateViewModelForNewUsers
09-16 23:35:34.048 5956-5956: newUserCounter is -1
09-16 23:35:34.048 5956-5956: isVisible is true
09-16 23:35:34.050 5956-5956: at the end userProcessingList size 2 <-- THE LIST IS STILL 2
09-16 23:35:34.050 5956-5956: Observeed 2 new users, calling updateNewUsers
09-16 23:35:34.050 5956-5956: userProcessingList size 0 <-- LIST SIZE IS NOW 0 WHILE IT SHOULD BE 2
09-16 23:35:34.050 5956-5956: users size 2
09-16 23:35:34.050 5956-5956: Nothing is processing, let's sync all users
09-16 23:35:34.050 5956-5956: new userProcessingList size 2
09-16 23:35:34.050 5956-5956: in updateViewModelForNewUsers
09-16 23:35:34.050 5956-5956: newUserCounter is 0
09-16 23:35:34.050 5956-5956: isVisible is true
The list size will eventually be 2 after couple OnChange is called.
I try CopyOnWriteArrayList, and collect.synchronizedList with Synchorized(userProcessingList){} and both do not help. Please advise!
解决方案
推荐阅读
- c++ - 自动生成基于成员的操作?
- raspberry-pi - 尝试在 QEMU 下运行 Raspberry-Pi 映像,但 VM 内存限制为 256MB
- ios - SwiftUI 中的左右填充(不是前导和尾随)
- python - 我尝试在 django 中构建的测验 - 如何使其工作?
- java - 如何在以正确方式获取的事务之外初始化延迟加载的集合?
- flutter - 颤抖)我想知道如何从图库中获取图片
- javascript - oauth2 重定向在 openapi3.0/swagger UI 中失败
- r - 如何转置单列以适应 R 中时间序列中的其余数据?
- javascript - 尝试将 Enum[ ] 从儿子传递给父亲组件
- java - 多对多按添加到帖子的时间排序