如何防止 Realmjs 中的多个 beginTransactions?
How to prevent multi beginTransactions in Realmjs?
我创建了一个函数来处理交易,然后我在多个地方调用它。当我打开新交易时另一笔交易尚未完成时我崩溃了。
这是我的代码:
const RealmMakeTransaction = async (action) => {
try {
realm.GetInstance().beginTransaction();
let response = await action();
realm.GetInstance().commitTransaction();
return response;
} catch (e) {
realm.GetInstance().cancelTransaction();
}
};
您可以在调用 beginTransaction()
之前通过调用 realm.GetInstance().isInTransaction
轻松检查 realm
是否已经在交易中
您的代码将如下所示:
const RealmMakeTransaction = async (action) => {
//use single instance
let realm = realm.GetInstance();
try {
if( realm.isInTransaction)
realm.cancelTransaction();
realm.beginTransaction();
let response = await action();
realm.commitTransaction();
return response;
} catch (e) {
realm.cancelTransaction();
realm.close();
}
};
我创建了一个函数来处理交易,然后我在多个地方调用它。当我打开新交易时另一笔交易尚未完成时我崩溃了。
这是我的代码:
const RealmMakeTransaction = async (action) => {
try {
realm.GetInstance().beginTransaction();
let response = await action();
realm.GetInstance().commitTransaction();
return response;
} catch (e) {
realm.GetInstance().cancelTransaction();
}
};
您可以在调用 beginTransaction()
之前通过调用 realm.GetInstance().isInTransaction
realm
是否已经在交易中
您的代码将如下所示:
const RealmMakeTransaction = async (action) => {
//use single instance
let realm = realm.GetInstance();
try {
if( realm.isInTransaction)
realm.cancelTransaction();
realm.beginTransaction();
let response = await action();
realm.commitTransaction();
return response;
} catch (e) {
realm.cancelTransaction();
realm.close();
}
};