神刀安全网

Realm Objective‑C & Swift 0.100 – Queryable, Live Inverse Relationships!

We’re releasing version 0.100 ofRealm Objective‑C andRealm Swift today, adding another highly requested feature: making inverse relationships queryable and as live as their forward counterparts.

Queryable, Live Inverse Relationships

Let’s recap how relationships work in Realm. Relationships are unidirectional, and are declared using RLMArray / List or object properties. These properties can be used in queries, and are updated automatically to reflect changes to the property value made elsewhere in your app. Realm has historically provided a method to retrieve a snapshot of all objects that link to a specific object instance at the current point in time. Realm 0.100 improves on this by allowing you to declare linking objects properties that represent the inverse side of a relationship. These properties are live, auto-updating, queryable collections that provide the full capabilities of Realm’s other collection types. Unlike other Realm properties, linking objects properties cannot be modified directly. They’re modified automatically as a result of adding or removing the object from the RLMArray / List or object property that makes up the forward side of the relationship.

One-to-Many Relationships

@interface Dog : RLMObject @property NSString *name; @property NSInteger age; @property Person *owner; @end  @implementation Dog @end  @interface Person : RLMObject @property NSString *name; @property NSInteger age; @property (readonly) RLMLinkingObjects<Dog *> *dogs; @end  @implementation Person + (NSDictionary *)linkingObjectsProperties {     return @{         @"dogs": [RLMPropertyDescriptor descriptorWithClass:Dog.class propertyName:@"owner"],     }; } @end

class Dog: Object {     dynamic var name = ""     dynamic var age = 0     dynamic var owner: Person? }  class Person: Object {     dynamic var name = ""     dynamic var age = 0     let dogs = LinkingObjects(fromType: Dog.self, property: "owner") }

Many-to-Many Relationships

@interface Person : RLMObject @property NSString *name; @property NSInteger age; @property RLMArray<Person *><Person> *children; @property (readonly) RLMLinkingObjects<Person *> *parents; @end  @implementation Person + (NSDictionary *)linkingObjectsProperties {     return @{         @"parents": [RLMPropertyDescriptor descriptorWithClass:Person.class propertyName:@"children"],     }; } @end

class Person: Object {     dynamic var name = ""     dynamic var age = 0     let children = List<Person>()     let parents = LinkingObjects(fromType: Person.self, property: "children") }

Note that due to their computed nature, RLMLinkingObjects / LinkingObjects properties must be declared as readonly / let .

The new linking objects properties offer several significant advantages over the previous approach:

1) Linking Objects Are Live, Auto-updating Collections

Just like the other collection types in Realm, RLMLinkingObjects / LinkingObjects are live and auto-updating. When new relationships are formed with or removed from the object, the linking objects will update to reflect the currently linking objects. This means you are no longer working with a static point-in-time snapshot of the relationship that you need to keep updated yourself.

2) Linking Objects Can Be Used In Queries

To perform queries on linking objects with the old API you had to manually filter the objects in your own code, missing out on the performance & liveness provided by Realm’s native query system.

Linking objects properties are completely integrated with Realm’s query system, making it efficient and easy to filter over inverse relationships.

// People that have a child that have a parent named Diane. [PersonObject objectsWhere:@"ANY children.parents.name == 'Diane'"];  // People whose parents have an average age of > 65. [PersonObject objectsWhere:@"parents.@avg.age > 65"];

// 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")

3) Linking Objects Are Realm Collections

While the previous linking objects API returned a standard library array type, the new RLMLinkingObjects / LinkingObjects types conform to Realm’scollection type protocol. This makes them familiar to work with and provides all the functionality you’re used to from Realm’s RLMResults / Results types.

// Which of my parents are over the age of 56? [self.parents objectsWhere:@"age > 56"];  // Calculate the age of my parents. [self.parents averageOfProperty:"age"];

// 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")

Deprecations

The -[RLMObject linkingObjectsOfClass:forProperty:] and Object.linkingObjects(_:forProperty:) methods are deprecated and will be removed in a future release.

Other Changes

  • Queries that compare objects for equality now support multi-level key paths.
  • Queries that compare RLMArray / List properties using != now give the correct results.
  • Fix an assertion failure when a second write transaction is committed after a write transaction deleted the object containing an RLMArray / List which had an active notification block.

Thanks for reading. Now go forth and build amazing apps with Realm! As always, we’re around on StackOverflow , GitHub , or Twitter .

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Realm Objective‑C & Swift 0.100 – Queryable, Live Inverse Relationships!

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址