Realm事务导致GC扫描ui冻结



在Realm中进行交易时,我的ui正在冻结。在交易过程中获得大量GC扫描:

Starting a blocking GC Explicit
Explicit concurrent mark sweep GC freed 21(1312B) AllocSpace objects, 0(0B) LOS objects, 13% free, 106MB/122MB, paused 480us total 24.157ms

这是我第一次遇到这个问题。一直在很多项目中使用Realm,却没有遇到这样的事情。即使删除所有代码并只保留realm.beginnsack和realm.committransaction调用,它也会冻结,所以我将问题归结为事务本身。但考虑到如果在begin&commit与大小或查询本身无关。

public static void insertValueForKeyInSession(final String sessionUuid, final String key, final String value){
    Realm realm = App.getCoreRealmInstance(App.getContext());
    RealmQuery<DataResponse> query = realm.where(DataResponse.class);
    query.equalTo("sessionUuid", sessionUuid);
    query.equalTo("key", key);
    final RealmResults<DataResponse> result = query.findAll();
    if (result.size() > 0) {
        DataResponse response = result.get(0);
        realm.beginTransaction();
        response.setValue(value);
        realm.commitTransaction();
    } else {
        DataResponse dataResponse = new DataResponse();
        String uuid = UUID.randomUUID().toString();
        dataResponse.setSessionUuid(sessionUuid);
        dataResponse.setUuid(uuid);
        dataResponse.setKey(key);
        dataResponse.setValue(value);
        realm.beginTransaction();
        realm.copyToRealm(dataResponse);
        realm.commitTransaction();
    }
    realm.close();
    Log.d(TAG,"insertValue Stopped");
}

来源https://realm.io/docs/java/0.76.0/#writes它看起来像是"写操作会互相阻塞,如果其他写操作正在进行,就会阻塞正在进行的线程。"有可能还有其他写操作在进行吗?

我花了一些时间,但我还是解决了这个问题。Stil不知道确切的原因,如果有人能得到任何信息,就去做吧,太好了:)经过一次又一次的尝试,我终于开始思考我最近在这个项目中改变的所有事情。

我不得不使用Migration&配置,而之前我会通过普通的realm.getInstance(上下文上下文)获得一个领域实例。我已将代码更改为App.getCoreRealmInstance(App.getContext())。此方法:

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(context)
                .schemaVersion(dbVersion)
                .migration(new Migration())
                .build();
return Realm.getInstance(realmConfiguration);

我所做的更改:

  1. 以下方法在启动时只调用一次,并设置默认配置。。

    RealmConfigurationrealmConfiguration=新RealmConfiguration.Builder(上下文).schemaVersion(dbVersion).migration(new migration()).build();Realm.setDefaultConfiguration(realmConfiguration)

  2. App.getCoreRealmInstance方法现在只返回Realm.getDefaultInstance();

最新更新