简体   繁体   English

SQL 数据库设计问题 android 房间数据库

[英]SQL database design issue in android room db

I'm building an app that fetches timeline posts (as in social media app) from server, save it to local room db and show them to UI via paging.我正在构建一个从服务器获取时间线帖子(如在社交媒体应用程序中)的应用程序,将其保存到本地房间数据库并通过分页将它们显示给 UI。 My issue is, on what crteria, i should fetch posts from local db?我的问题是,在什么条件下,我应该从本地数据库获取帖子?

Approach i've tried:我尝试过的方法:

  1. Fetch posts based on post id (primary key) descending, but it fails in below case根据帖子 id(主键)降序获取帖子,但在以下情况下失败

Suppose User A has 2 posts.假设用户 A 有 2 个帖子。 Later, user B create 2 posts (thus, having newer postId).稍后,用户 B 创建 2 个帖子(因此,具有更新的 postId)。 Now, when user B follows User A, backend will return posts in this format: 2 post of user A + 2 posts of user B. but (saving and) fetching posts from local db based on postId descending would make post of user B to appear below post of user A in user A's timeline, which seems incorrect.现在,当用户 B 关注用户 A 时,后端将以这种格式返回帖子:用户 A 的 2 个帖子 + 用户 B 的 2 个帖子。但是(保存和)基于 postId 降序从本地数据库获取帖子会使用户 B 的帖子出现在用户 A 的时间轴中用户 A 的帖子下方,这似乎不正确。 Same could be thought for ascending order in reverse case.在相反的情况下,可以考虑升序。

2.Use a timestamp column in db corresponding to insertion time of post in db and fetch posts based on timestamp. 2.使用数据库中的时间戳列,对应数据库中帖子的插入时间,并根据时间戳获取帖子。 But all post in batch insertion get same value, whether its Instant.now() or System.currentTimeMillis()但是批量插入的所有帖子都得到相同的值,无论是Instant.now()还是System.currentTimeMillis()

first option is correct, fetch posts based on post id (primary key) descending.第一个选项是正确的,根据帖子 id(主键)降序获取帖子。 I think your case is incorrect, see example: A posts 1, 2 B posts 3, 4 B follow's A so his timeline would fetch from local db based on postId desc 4 -> 3 -> 2 -> 1 two posts of B then two posts of A like it should be.我认为您的情况不正确,请参见示例:A 发布 1、2 B 发布 3、4 B 关注 A,因此他的时间线将根据 postId desc 4 -> 3 -> 2 -> 1 从本地数据库获取 B 然后的两个帖子两篇A应该是这样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM