Olzhas Ilyubayev
Olzhas Ilyubayev

Reputation: 348

How to make a nested query in Realm Swift?

I have realm with lists of other realm-objects.

import RealmSwift

class Company: Object {
  dynamic var id = 0
  dynamic var city = 0
  dynamic var title = ""
  dynamic var address = ""
  dynamic var schedule = ""
  dynamic var workBreak = ""

  let categories = List<CompanyCategory>()
  let phones = List<CompanyPhone>()
  let sites = List<CompanySite>()
  let emails = List<CompanyEmail>()
  let services = List<CompanyService>()
  let branches = List<CompanyBranche>()

}

class CompanyCategory: Object {
  dynamic var id = 0
  dynamic var name = ""
}

So, I have category id and I want to see which companies is refer to this category. Company may have several categories.

I find solution in Java and it works in my android app. Can somebody help me to write right predication? Sorry for my bad english (:

Upvotes: 2

Views: 1968

Answers (1)

bdash
bdash

Reputation: 18308

Realm Swift uses Cocoa's NSPredicate to represent queries. You can find some examples of the supported syntax in the Filtering section of the Realm Swift documentation, and more information about the syntax on NSPredicate Cheatsheet. From those two documents you'll arrive at something like the following for your query:

realm.objects(Company).filter("ANY categories.id = %@", id)

Upvotes: 5

Related Questions