java—更新数据库上的对象并在本地对其进行更改,如何确保远程注册更改?

gijlo24d  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(159)

我有一个 Map<Integer, List<String>> 我从formserver接收到,integer表示对象的id,list表示用户对该对象的React。每当用户改变对某个对象的React时,列表就会改变,并且会发送一个请求来更新数据库中的Map。所以我想知道,如果我为了性能和主线程的非阻塞而在本地更改Map,并基于这些本地更改绘制视图的一些元素(“like”button、“favorite”button等)并在数据库上异步更新Map,如何确保数据库上的更改始终与本地更改匹配。服务器可能返回404或500等,并且Map没有在服务器上更新。在这种情况下,我们该怎么办?

addLike(exercise, Map<Integer, List<String>>){
  if (exercise_id is in the Map as Key and if there is no "like" in the List)
      add "like" to list;
      change the Map locally;
      addLikehelper(exercise_ID,"like", new OkHttPListener (){
                    @Override
                    public void onResponse(Response response) {
                         if (resonse.isSucceseful())
                            //everything fine
                         else 
                            //but what to do if its not? 
                            //we cannot throw any Exceptions? 
                            //should we just hope evrything will be fine?
                    }});

  return Map-reference;
}
use the map to change the color of button and add +1 to total sum of likes of that Object.

这是不是意味着每次我改变Map我都应该要求它回来,只是为了100%确定?
stackoverflow或facebook是如何做到的?一旦你喜欢的东西按钮立即改变颜色的计数上升,下次你登录时,这些变化仍然存在。他们怎么能这么肯定每个喜欢的人都会注册?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题