---
title: "#234: Queryable, Live Inverse Realm Collections 🔮"
url: https://littlebitesofcocoa.com/234-queryable-live-inverse-realm-collections
author: Jake Marsh
date: 2016-05-05
published: 2016-05-05
topics: ["Models","Libraries","Realm"]
description: "In Bite #231, we took a look at Realm's new Fine-grained notifications functionality. Today we'll build upon this by checking out another new Realm feature..."
---

# #234: Queryable, Live Inverse Realm Collections 🔮

By Jake Marsh, May 5, 2016. HTML: https://littlebitesofcocoa.com/234-queryable-live-inverse-realm-collections

In [Bite #231](https://littlebitesofcocoa.com/231), we took a look at [Realm's](https://realm.io/) new <i>Fine-grained notifications</i> functionality. Today we'll build upon this by checking out another new [Realm](http://realm.io) feature: <i>Queryable, Live Inverse Collections</i>. Whew! That's a fancy name. This feature deserves one though, let's check it out.

Here's a **Realm object** with a relationship defined on it:

``` swift
class Person: Object {
  dynamic var name = ""
  let dogs = List<Dog>()
}
```

That `dogs` **property** can be used in a **query**, and it will even stay updated to reflect changes to the **property's** value made elsewhere in our app, automatically.

None of that is new though. What <i>is</i> new is the inverse of this mechanic.

Meet the all-new **LinkingObjects**:

``` swift
class Dog: Object {
  dynamic var name = ""
  dynamic var age = 0
  let owners = LinkingObjects(
    fromType: Person.self, 
    property: "dogs"
  )
}
```

Here's what we get for defining things this way:

**LinkingObjects** are live and auto-updating. When new **relationships** are formed or removed, they will update to reflect the new state.

**LinkingObjects** can be used In **queries**, natively. (Previously this would need to be done in our code):

``` swift
// People that have a child that have a parent named Diane.
realm.objects(Person).filter("ANY children.parents.name == 'Diane'")

// People whose parents have an average age of > 65.
realm.objects(Person).filter("parents.@avg.age > 65")
```

**LinkingObjects** behave like regular **Realm collections**:

``` swift
// Which of my parents are over the age of 56?
self.parents.filter("age > 56")
  
  // Calculate the age of my parents.
self.parents.average("age")
```

More info about _Realm_ can be found at [realm.io](http://realm.io)
