1

我创建了一个函数来处理事务,然后我将它调用到多个地方。当我打开新事务时另一个事务尚未完成时,我崩溃了。

这是我的代码:

const RealmMakeTransaction = async (action) => {
    try {
        realm.GetInstance().beginTransaction();
        let response = await action();
        realm.GetInstance().commitTransaction();

        return response;

    } catch (e) {

        realm.GetInstance().cancelTransaction();
    }
};
4

1 回答 1

1

您可以在调用之前轻松检查是否realm已经在事务中beginTransaction()realm.GetInstance().isInTransaction

您的代码将如下所示:

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();
    }
};
于 2019-01-24T10:38:35.670 回答