首页 > 解决方案 > Realm App Architecture with Full Sync - 如何在多领域架构中同步

问题描述

遵循 App Architecture Guidelines,我想在我的按需领域中保持一些公共数据同步。

举个简单的例子:

  1. 汽车为所有客户同步并存储在/commonRealm
  2. 行程仅为相关用户同步并存储在/~/driverRealm

下面是应用程序的最小架构

const CarSchema = {
    name: 'Car',
    primaryKey: 'id',
    properties: {
        id: {type: 'string'},
        make: {type: 'string'},
        model: {type: 'string'}
    }
};

const TripSchema = {
    name: 'Trip',
    primaryKey: 'id',
    properties: {
        id: {type: 'string'},
        name: {type: 'string'},
        car: {type: 'Car'}
    }
};

commonRealm 仅包含 CarSchema。driverReam 包括 CarSchema 和 TripSchema。驾驶员永远不会更新汽车,其目的实际上是为了查找。

我找不到如何让 Cars 在领域之间保持同步的示例,以便将 commonRealm 中的任何更新传播到驱动程序的 driverRealm。

使用 subscribe() 收听 commonRealm Cars 对象并没有帮助,因为您无法访问已删除的数据等。

从我可以从文档中推断出,这个架构实际上应该是这样的:

const CarSchema = {
    name: 'Car',
    primaryKey: 'id',
    properties: {
        id: {type: 'string'},
        make: {type: 'string'},
        model: {type: 'string'}
    }
};

const TripSchema = {
    name: 'Trip',
    primaryKey: 'id',
    properties: {
        id: {type: 'string'},
        name: {type: 'string'},
        carId: {type: 'string'} // foreign key
    }
};

commonRealm 将包含 CarSchema,而 driverReam 将仅包含带有 carId 的 TripSchema,作为 Car.id 的类型字符串,而不是 Car 的类型。由客户端运行地图/循环以从 commonRealm 手动加入 Car,例如:

        let tripsWithCar = [];
        trips.forEach(trip => {
            trip.car = commonRealm.objects('Car').filtered(`id == "${trip.carId}"`)[0];
            tripsWithCar.push(trip);
        });

如果这是正确的方法,我想从有更多经验的开发人员那里获得一些反馈。

标签: realm

解决方案


推荐阅读