0

这里我举个例子

class TestRealm {
    constructor(realmDB) {
        this.realmDB = realmDB;
        this.allRealm = this.realmDB.objects('Person');
    }

    query1(primary_key) {
        try {
            const response = this.allRealm.filtered(`_id == "${primary_key}"`);
            if (response.length === 0) {
                console.log('unable to find');
                return;
            }
            return response.toJSON()[0];
        }
        catch (e) {
            console.log(e);
        }
    }

    query2(primary_key) {
        try {
            const response = this.realmDB.objectForPrimaryKey('Person', `"${primary_key}"`);
            if (!response) {
                console.log('unable to find');
                return;
            }
            return response.toJSON();
        }
        catch (e) {
            console.log(e);
        }
    }
}

假设在打开领域后,我将领域对象传递给 TestRealm 构造函数。在这种情况下,哪个查询对大数据有效?

4

1 回答 1

0

让我们从排列术语开始

这是一个查询(又名过滤器),将返回一个领域结果对象

const response = this.allRealm.filtered(`_id == "${primary_key}"`);
        

不是查询,将返回该特定对象

const response = this.realmDB.objectForPrimaryKey('Person', `"${primary_key}"`);

所以你返回了两个不同的东西——response在第一个例子中也是一个实时更新的 Realm Results 对象,所以它比返回对象本身有更多的开销。

另请注意,对于“大数据”,第二个选项基本上不受数据集大小的影响(一般而言)

第二个例子要快得多。

于 2021-03-19T17:16:42.263 回答