如何改进 Realm 查询以避免阻塞 UI?

时间:2021-08-12 04:32:30

标签: javascript mongodb vue.js electron realm

以下是我的查询之一:

async function getCatlogEntities (articleId) {
  // fetch catalog entities
  let catalogEntities = await realm.objects('catalogentity')

  // stringify
  catalogEntities = JSON.parse(JSON.stringify(catalogEntities))

  // fetch taxanomies
  let taxonomies = await publicRealm.objects('taxonomy')

  // stringify
  taxonomies = JSON.parse(JSON.stringify(taxonomies))

  // fetch catalog article entities
  let catalogArticleEntities = await realm.objects('catalogarticleentity')

  // stringify
  catalogArticleEntities = JSON.parse(JSON.stringify(catalogArticleEntities))
  console.log('catalogEntities', catalogEntities)
  // create the required response
  const articleEntities = _.map(catalogEntities, entity => {
    // find taxonomies
    const findTaxonomy = _.find(taxonomies, {id: entity.taxonomy_id})
    entity.taxonomy = !_.isEmpty(findTaxonomy) ? findTaxonomy : {}
    console.log('present catalog entity', entity)
    if (!_.isEmpty(entity.article_entity)) {
      entity.article_entity = _.filter(entity.article_entity, articleEntity => {
        const findArticleEntity = _.find(catalogArticleEntities, {id: articleEntity})
        console.log('findArticleEntity', findArticleEntity)
        // filter out the entities if not found OR article id present and didn't matched with current article id
        if (!_.isEmpty(findArticleEntity) && ((articleId && findArticleEntity.article_id === articleId) || (!articleId))) return findArticleEntity
      })
    }
    return entity
  })
  return articleEntities
}

我可以如何改进它,使其不会阻塞 UI 线程? 我遇到了当数据太重或者我们正在执行多个查询并将它们组合起来时(因为 Realm 不支持 join(聚合)),UI 被阻塞了 7-8 秒钟。

0 个答案:

没有答案