java - Realm 异步任务

标签 java realm

我正在尝试使用 AsyncTransaction 来插入一些对象,但目前它失败了......

我尝试调试,但也没有成功......

查看我的代码:

realm.executeTransactionAsync(
                 new Realm.Transaction() {
                    @Override
                    public void execute(@NonNull Realm realm) {
                        Log.i("Insert", "Insert start");
                        realm.insert(character);
                    }
                }, new Realm.Transaction.OnSuccess() {
                    @Override
                    public void onSuccess() {
                        Log.i("Insert", "Insert complete");
                        finish();
                    }
                }, new Realm.Transaction.OnError() {
                    @Override
                    public void onError(Throwable error) {
                        Log.i("Insert","Error " + error.getMessage());
                    }
                });

当我调试时,我发现我没有进行任何异步事务的回调,没有日志,没有任何东西可以帮助我。

提前致谢,

编辑:

public RealmAsyncTask executeTransactionAsync(final Transaction transaction,
        @Nullable final Realm.Transaction.OnSuccess onSuccess,
        @Nullable final Realm.Transaction.OnError onError) {
    checkIfValid();

    //noinspection ConstantConditions
    if (transaction == null) {
        throw new IllegalArgumentException("Transaction should not be null");
    }

    // Avoid to call canDeliverNotification() in bg thread.
    final boolean canDeliverNotification = sharedRealm.capabilities.canDeliverNotification();

    // If the user provided a Callback then we have to make sure the current Realm has an events looper to deliver
    // the results.
    if ((onSuccess != null || onError != null)) {
        sharedRealm.capabilities.checkCanDeliverNotification("Callback cannot be delivered on current thread.");
    }

    // We need to use the same configuration to open a background SharedRealm (i.e Realm)
    // to perform the transaction
    final RealmConfiguration realmConfiguration = getConfiguration();
    // We need to deliver the callback even if the Realm is closed. So acquire a reference to the notifier here.
    final RealmNotifier realmNotifier = sharedRealm.realmNotifier;

    final Future<?> pendingTransaction = asyncTaskExecutor.submitTransaction(new Runnable() {
        @Override
        public void run() {
            if (Thread.currentThread().isInterrupted()) {
                return;
            }

            SharedRealm.VersionID versionID = null;
            Throwable exception = null;

            final Realm bgRealm = Realm.getInstance(realmConfiguration);
            bgRealm.beginTransaction();

    // NOTHING IS DONE AFTER IS POINT .....
            try {
                transaction.execute(bgRealm);

                if (Thread.currentThread().isInterrupted()) {
                    return;
                }

                bgRealm.commitTransaction();
                // The bgRealm needs to be closed before post event to caller's handler to avoid concurrency
                // problem. This is currently guaranteed by posting callbacks later below.
                versionID = bgRealm.sharedRealm.getVersionID();
            } catch (final Throwable e) {
                exception = e;
            } finally {
                try {
                    if (bgRealm.isInTransaction()) {
                        bgRealm.cancelTransaction();
                    }
                } finally {
                    bgRealm.close();
                }
            }

            final Throwable backgroundException = exception;
            final SharedRealm.VersionID backgroundVersionID = versionID;
            // Cannot be interrupted anymore.
            if (canDeliverNotification) {
                if (backgroundVersionID != null && onSuccess != null) {
                    realmNotifier.post(new Runnable() {
                        @Override
                        public void run() {
                            if (isClosed()) {
                                // The caller Realm is closed. Just call the onSuccess. Since the new created Realm
                                // cannot be behind the background one.
                                onSuccess.onSuccess();
                                return;
                            }

                            if (sharedRealm.getVersionID().compareTo(backgroundVersionID) < 0) {
                                sharedRealm.realmNotifier.addTransactionCallback(new Runnable() {
                                    @Override
                                    public void run() {
                                        onSuccess.onSuccess();
                                    }
                                });
                            } else {
                                onSuccess.onSuccess();
                            }
                        }
                    });
                } else if (backgroundException != null) {
                    realmNotifier.post(new Runnable() {
                        @Override
                        public void run() {
                            if (onError != null) {
                                onError.onError(backgroundException);
                            } else {
                                throw new RealmException("Async transaction failed", backgroundException);
                            }
                        }
                    });
                }
            } else {
                if (backgroundException != null) {
                    // FIXME: ThreadPoolExecutor will never throw the exception in the background.
                    // We need a redesign of the async transaction API.
                    // Throw in the worker thread since the caller thread cannot get notifications.
                    throw new RealmException("Async transaction failed", backgroundException);
                }
            }

        }
    });

    return new RealmAsyncTaskImpl(pendingTransaction, asyncTaskExecutor);
}

最佳答案

我找到了窍门。

在我的构造函数中,我将一些 RealmObject 添加到另一个,这会产生错误

(不能在非写事务上写入)

第二点,是我在父级上使用了 beginTransaction(),但它阻止了异步事务的另一部分

我更改了在第一部分上使用 RealmRecyclerView 的代码,并且不再遇到问题

谢谢

关于java - Realm 异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46883593/

相关文章:

java - 为什么我无法检索刚刚保留的实体?

java - 照片压缩上传至数据库

docker - 如何使用openjdk :7 Docker image and a Gradle wrapper?避免 `EC parameters error`

ios - 父类和子类的 Realm 过滤器

java - Realm java查询条件

java - 在 Java 中序列化静态属性

java - 将 JDBC 更改为 hibernate 后,应用程序运行速度非常慢

ios - iOS 应用程序中的 Realm 文件大小

ios - 我可以让 Realm Results 类使用协议(protocol)作为泛型吗?

ios - 如何从多个 Realm 模型创建一个 tableview?