1

我正在尝试在已经包含一些数据的设备上打开领域同步,这些数据已经存在于服务器上。当新用户连接到领域时,它应该将本地领域数据与同步领域数据合并。但是此代码在初始同步发生之前启动。由于尚未收到来自服务器的数据,因此应用程序会在同步领域中创建一些记录。同步完成后,我两次看到相同的数据。我刚刚创建的记录和从服务器获取的数据。具有相同的主键。

有关示例,请参见下面的代码:

RLMRealmConfiguration *config = [[RLMRealmConfiguration alloc] init];
config.syncConfiguration = [[RLMSyncConfiguration alloc] initWithUser:user realmURL:self.realmURL];
NSError *error = nil;
RLMRealm *newRealm = [RLMRealm realmWithConfiguration:config error:&error];
if(newRealm != nil && error == nil)
{
    [newRealm beginWriteTransaction];

    for(ModelFolder *folder in [ModelFolder allObjectsInRealm:curRealm])
    {
        ModelFolder *newFolder = [ModelFolder objectInRealm:newRealm forPrimaryKey:folder.uuid];
        if(newFolder == nil)
            [ModelFolder createInRealm:newRealm withValue:folder];
    }

    [newRealm commitWriteTransaction];
}

有没有办法检测到,该领域已完成初始同步?

UPD:更多细节。

ModelFolder包含@property RLMArray<ModelBookmark *><ModelBookmark> *bookmarks;并且当我创建文件夹时,这等于将在几秒钟内获取它们正确合并的某个文件夹。但。Folder 对象内的书签没有重复数据删除,我们得到如下内容:

RLMResults <0x802082d0> (
 [0] ModelFolder {
  uuid = 2615AB34-1C08-4E7B-8D49-6E02EDBCDF89;
  name = (null);
  descr = (null);
  shareURL = (null);
  date = 1484566331137;
  bookmarks = RLMArray <0x806c78d0> (
   [0] ModelBookmark {
    uuid = C752FCEB-65CB-47C8-8CF4-6CA44C119ECC;
    name = (null);
    descr = (null);
    shareURL = (null);
    date = 1484566331137;
    folderUuid = 2615AB34-1C08-4E7B-8D49-6E02EDBCDF89;
    longitude = 27.54834598813616;
    latitude = 53.91333128839566;
    mapZoom = 11.73785983313041;
    category = 0;
    visible = 1;
   },
   [1] ModelBookmark {
    uuid = C752FCEB-65CB-47C8-8CF4-6CA44C119ECC;
    name = (null);
    descr = (null);
    shareURL = (null);
    date = 1484566331137;
    folderUuid = 2615AB34-1C08-4E7B-8D49-6E02EDBCDF89;
    longitude = 27.54834598813616;
    latitude = 53.91333128839566;
    mapZoom = 11.73785983313041;
    category = 0;
    visible = 1;
   }
  );
  tracks = RLMArray <0x806fb120> (

  );
  opened = 1;
 }
)
4

1 回答 1

1

不幸的是,目前不支持合并有序列表(直到实现https://github.com/realm/realm-core/issues/1206)。现在您必须手动删除列表项,您可以使用我们在 RealmTasks 应用程序中使用的相同解决方法,请参阅https://github.com/realm/RealmTasks/pull/180了解实现细节。

于 2017-01-16T12:51:05.977 回答