3

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

4

2 回答 2

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();
}
});
于 2018-12-19T15:32:47.597 回答
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);
    }
});
于 2018-12-20T02:11:54.800 回答