Realm数据库全同步 – 如何实现全局评论系统

huangapple go评论67阅读模式
英文:

Realm Database Full Sync - How to implement a global comment system

问题

我需要实现一个共享的全局领域,用户可以在其中编写新记录并删除他们以前的记录,但不能删除其他用户的记录。

例如,一个评论系统。

我通过使用基于查询的同步来实现它的对象权限,但我无法理解如何通过完全同步来轻松实现它。

有人已经完成了吗?你是如何实现的?谢谢。

英文:

I need to implement a shared global realm where users can write new records and delete their previous ones but not delete other users records.

Example, a comment system.

I made it via object permission using query based sync but I cannot understand how to easily implement it via full-sync.

Has someone done it? How have you accomplished that? Thank you.

答案1

得分: 0

只返回翻译好的部分:

你可以获得相同的功能,只要每个用户都有自己的 Realm。完整同步 Realm 上的权限可以提供给其他用户,以允许他们对用户的 Realm 进行读写访问。

请参阅提供权限

您需要将全局 Realm 分解为各个个体 Realm,但这是一种选择。

如果您将所有用户数据存储在一个全局 Realm 上,这将会更具挑战性,因为您无法精细控制其他用户正在做什么。

但是,您可以在应用程序中实现逻辑来控制谁可以/不能处理对象。例如,一个 ToDo 对象可以有一个 'created_by_user_id' 属性。

class ToDoClass: Object {
   @obc dynamic var to_do_id = ""
   @obc dynamic var created_by_uid = ""
    
    override static func primaryKey() -> String? {
        return "to_do_id"
    }
}

并且使用应用程序逻辑,当另一个用户尝试删除该用户的 ToDo 时,可以比较 created_by_uid 属性和当前用户的 id,以查看它们是否匹配。如果不匹配,则不允许删除。

英文:

You can get that same functionality as long as each user has their own Realm. Permissions on a Full Sync Realm can be offered to other users to allow them read/write access to a users Realm.

See Offering Permissions

You would need to break down your global realm into individual ones but that's one option.

If you are storing all users data on one, global realm. It’s going to be a bit more challenging as you don’t have a fine grained control over what other users are doing.

However, you could implement logic in the app that controls who can/cannot work with an object. So for example, a ToDo object could have a ‘created_by_user_id’ property

class ToDoClass: Object {
   @obc dynamic var to_do_id = ""
   @obc dynamic var created_by_uid = ""

    override static func primaryKey() -> String? {
        return "to_do_id"
    }
}

and using app logic, when another user goes to delete that users ToDo, it could compare the created_by_uid property to the current users id to see if they match. If not, disallow delete.

答案2

得分: 0

我考虑使用一个Node.js服务器,当用户的数据库发生变化时,更新全局数据库。

它的工作原理如下(已经测试,我需要了解它的扩展性)。

我使用一个监听器来监视用户的数据库,这样用户可以在离线状态下插入行到他们的数据库,当他们重新联机时,Node.js服务器可以将数据复制到全局数据库上)。所以在客户端,我会在需要离线操作的类上拥有一个同步状态属性(本地、同步、已删除)。

英文:

I was thinking of using a node js server that update a global database when something happens in the users databases.

It works like this (tested, I need to understand how it scales.

I use a listener for users databases, so users can insert rows in their database offline and when they go back online the node js server can replicate the data on the global database). So on the client I’ll have a property with the sync status (local, synched, deleted ) on the classes I need offline.

huangapple
  • 本文由 发表于 2020年1月4日 00:08:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定