Use React Native Realm to Query through multiple List Objects

橙三吉。 提交于 2019-12-14 03:53:25

问题


First day using React Native and Realm, and I'm having a hard time figuring how to perform a query through two Realm List Objects.

Tasks have Reservations, which have Renters, which have first_name and last_name fields. I want my users to be able to search for tasks by the Renter's first and last names.

Essentially, "Give me all the tasks whose renter's first or last name begins with "xyz""

const TaskSchema = {
  name:'Task',
  properties: {
    reservations:{ type: LIST, objectType: ReservationSchema.name },
  }
},

const ReservationSchema = {
  name:'Reservation',
  properties: {
    renters:{ type: LIST, objectType: RenterSchema.name },
  },
}

const RenterSchema = {
  name:'Renter',
  properties: {
    first_name:{ type:STRING, optional:true },
    last_name:{ type:STRING, optional:true },
  },
}

I just can't figure out how to set up my query and predicates to accomplish this.


回答1:


You can filter through nested objects.

let tasks = realm.objects('Dog');
let xyzTasks = task.filtered('reservations.renters.first_name BEGINSWITH "xyz" OR reservations.renters.last_name BEGINSWITH "xyz"');

Reference: https://realm.io/docs/react-native/latest/#filtering



来源:https://stackoverflow.com/questions/39671131/use-react-native-realm-to-query-through-multiple-list-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!