Google Drive Realtime API:在层次结构数据模型中进行原子更新,同时保持排序顺序 [英] Google Drive Realtime API: doing atomic updates in a hierarchical data model while maintaining sort order

查看:59
本文介绍了Google Drive Realtime API:在层次结构数据模型中进行原子更新,同时保持排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用Google Drive Realtime API构建待办事项应用程序.它具有分层结构,因此任务可以具有子任务,可以具有子任务等.任务可以自由移动,这意味着它在同级兄弟之间的位置/索引可以更改,但也可以移动交给另一个父母.

现在,我知道您在使用Realtime API时必须注意使这些动作具有原子性. Google工程师在一个视频中解释说,执行此操作的最佳方法是对分层模型进行平面表示:每个项目/任务都有一个指向其父项的属性.这样,将任务移到另一父级上只需要执行一项操作:更改父级属性(而不是将其从一个父级的子级列表中删除并将其添加到新父级的子级列表中:两个操作>不是原子的).

这对于我的项目可以很好地工作,除了我还需要在父任务中保持子任务的顺序.当任务仅知道其父级时,我该怎么做? 非常感谢,鲁本

解决方案

我认为这应该可行:按照建议,让每个任务都指向其父任务.此外,让每个任务保留其子任务的排序列表.然后,当您向用户显示子任务列表时,请过滤掉未指向正确父任务的任务.您有时会在多个父母的陪同下执行相同的任务,但用户永远不会看到它.

危险是,由于种族状况,您最终拥有的任务没有列入其他任务的孩子名单中,但我认为这不可能发生.

另一种方法是让每个任务存储一个排序顺序"值,该值是一个实数. UI会在显示之前对它们进行排序.如果两个用户在同一位置添加任务,则有可能获得相同的值,但这并不重要,因为在这种情况下,两个任务的顺序定义不明确.

I'm planning to build a todo app using the Google Drive Realtime API. It will have a hierarchical structure, so tasks can have sub-tasks, which can have sub-tasks, etc. A task can be moved around freely, which means it's position/index between it's siblings can change, but it's also possible to move it to another parent.

Now I know you have to take care on making these moves atomic when using the Realtime API. In a video Google engineers explain that the best way to do this is to have a flat representation of your hierarchical model: each item/tasks has a property that points to it's parent. This way moving a task to another parent takes just one operation: changing the parent property (instead of removing it from a child list of one parent and adding it to the child list of the new parent: two operation > not atomic).

This can work fine for my project, except that I also need to maintain the order of the child tasks within a parent tasks. How can I do this when a task only knows it's parent? many thanks, Ruben

解决方案

I think this should work: have each task point to its parent task, as recommended. In addition, have each task keep a sorted list of its child tasks. Then, when you display a list of child tasks to the user, filter out the ones that don't point back to the correct parent. You'll occasionally have the same task under multiple parents, but the user will never see it.

The danger would be that, due to a race condition, you end up having a task that is not listed in any other task's list of children, but I think that can't happen.

Another way would be to have each task store a "sort order" value, which is a real number. The UI sorts them before displaying. If two users add tasks at the same location, there is a risk of them getting the same value, but it shouldn't really matter since the order of the two tasks isn't well defined in that case.

这篇关于Google Drive Realtime API:在层次结构数据模型中进行原子更新,同时保持排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆