gpt4 book ai didi

java - Android 中的 Realm 异步事务顺序结构

转载 作者:行者123 更新时间:2023-12-01 20:21:22 25 4
gpt4 key购买 nike

我想按顺序执行以下方法。这意味着,如果 A 方法结束,B 方法就会开始。如果 B 结束,C 开始,依此类推。

public void InitializeAPI {
public static void init_A(Context mContext) {
Realm.init(mContext);
Realm realm = Realm.getDefaultInstance();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
// Some working is here.
}
}, new Realm.Transaction.OnSuccess() {
// Some proceesing lines are here.
}, new Realm.Transaction.OnError() {
Log.d("AsyncTransaction", "ERROR");
});
// And there are lots of other TransactionAsync() line in this methods.
}
public static void init_B(Context mContext) {...}
public static void init_C(Context mContext) {...}
// and So many initialize methods.
}

如您所见,当我执行init_A(Context)时,异步事务将在后台运行。

但问题是init_B需要在init_A结束后执行。 init_C 也是如此。我该如何设计这件事?

当然,我知道有Realm.Transaction.OnSuccess(),但是,如果我在Realm.Transaction.OnSuccess()中写init_Binit_A 处,我认为这些代码不太整洁,也不利于维护。

最佳答案

为什么不使用这样的东西:

 realm.beginTransaction();

//... add or update objects here ...

realm.commitTransaction();

而不是异步调用。它将确保代码同步工作。

希望有帮助..!

关于java - Android 中的 Realm 异步事务顺序结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44624506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com