我通过SyncCredentials登录到Realm,允许创建用户,如下所示:

SyncCredentials credentials = SyncCredentials.usernamePassword(username, password, true);
SyncUser.logInAsync(credentials, AUTH_URL, new SyncUser.Callback<SyncUser>() {
}


然后,我想使用以下代码在Realm上删除此用户:

String id = SyncUser.current().getIdentity();
PermissionUser permissionUser = realm.where(PermissionUser.class).equalTo("id", id).findFirst();
if (permissionUser != null) {
    permissionUser.getPrivateRole().removeMember(id);
    permissionUser.getPrivateRole().deleteFromRealm();
    if (permissionUser.getRoles() != null) {
        permissionUser.getRoles().deleteAllFromRealm();
    }
    permissionUser.deleteFromRealm();
}


该代码已成功运行,但是我已经在Realm Studio上进行了检查,该用户仍然存在。
请帮助我这个问题,非常感谢。

最佳答案

数据的所有更改都必须在事务中发生

来源:https://realm.io/docs/java/latest/

例:

// obtain the results of a query
final RealmResults<Dog> results = realm.where(Dog.class).findAll();

realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
    // remove single match
    results.deleteFirstFromRealm();
    results.deleteLastFromRealm();

    // remove a single object
    Dog dog = results.get(5);
    dog.deleteFromRealm();

    // Delete all matches
    results.deleteAllFromRealm();
}
});

10-05 23:57