realmrealm-js

realm js: Access to invalidated Results objects


I use realm inside my React native application a try to query list of objects from realm db.

function* loadPlaces() {
    let realm;
    try {
        const filter = yield select(getFilter);
        realm = yield call(Realm.open, {schema: [PlaceSchema]});
        let places = realm.objects(PlaceSchema.name);
        if (filter.search) {
            places = places.filtered("name CONTAINS $0", filter.search);
        }
        switch (filter.sort) {
            case Sort.BY_NAME:
                places = places.sorted('name');
                break;
        }
        yield put(loadPlacesSucceed(places));
    } catch (e) {
        console.warn(e.message);
    } finally {
        if (realm) {
            realm.close();
        }
    }
}

After that I use resulted data in flatlist:

<FlatList
        data={this.props.items}
        keyExtractor={(item) => '' + item.id}
        renderItem={({item}) =>
            <PlaceItem item={item} onClick={(item) => this._onItemClicked(item)}/>}/>

And receive error:

Access to invalidated Results objects.

If i remove realm.close() error disapear, but I need to close realm after query.


Solution

  • Why do you think you need to close Realm after a query? If you close your Realm, you lose access to all auto-updating collections, such as Results, so you shouldn't close your Realm as long as you need access to a specific Results instance.