无法删除领域用户

时间:2018-12-07 04:39:30

标签: realm realm-java

我通过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上进行了检查,该用户仍然存在。 请帮助我解决这个问题,非常感谢。

2 个答案:

答案 0 :(得分:1)

所有数据更改都必须在交易中发生

来源: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();
}
});

答案 1 :(得分:0)

谢谢大家,但我想在这里找到解决方案:

String url = RealmConstants.AUTH_URL + "/user/" + SyncUser.current().getIdentity();
OkHttpClient okHttpClient = RetrofitClient.ClientHolder.getOkHttpClient();
Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", token.value())
                .addHeader("Accept", "application/json, text/plain, */*")
                .addHeader("Content-Type", "application/json")
                .delete()
                .build();
okHttpClient.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(@NonNull Call call, @NonNull IOException e) {
        callback.onFailure(new ClientError(e.hashCode(), e.getMessage()));
    }

    @Override
    public void onResponse(@NonNull Call call, @NonNull Response response) {
        callback.onSuccess(null);
    }
});