Wamba
Wamba

Reputation: 69

How to list LinkingObjects properties in Realm?

I need to list all properties of type LinkingObjects of a object.

class Dogs: Object {
    dynamic var name: String = ""
    dynamic var age: Int = 0
    dynamic var owner: Persons?
}


class Cats: Object {
    dynamic var name: String = ""
    dynamic var age: Int = 0
    dynamic var owner: Persons?
}


class Persons: Object {
    dynamic var name: String = ""
    dynamic var address: String = ""

    let dogs = LinkingObjects(fromType: Dogs.self, property: "owner")
    let cats = LinkingObjects(fromType: Cats.self, property: "owner")
}

ObjectSchema returns the schema correctly:

let person = Persons()
let schema = person.objectSchema
print(schema)

Result:

Persons {
 name {
    type = string;
    objectClassName = (null);
    linkOriginPropertyName = (null);
    indexed = NO;
    isPrimary = NO;
    optional = NO;
 }
 address {
    type = string;
    objectClassName = (null);
    linkOriginPropertyName = (null);
    indexed = NO;
    isPrimary = NO;
    optional = NO;
 }
 dogs {
    type = linking objects;
    objectClassName = Dogs;
    linkOriginPropertyName = owner;
    indexed = NO;
    isPrimary = NO;
    optional = NO;
 }
 cats {
    type = linking objects;
    objectClassName = Cats;
    linkOriginPropertyName = owner;
    indexed = NO;
    isPrimary = NO;
    optional = NO;
 }
}

However, objectSchema.properties does not return LinkingObjects properties.

let properties = schema.properties
print(properties)

Returns:

[name {
    type = string;
    objectClassName = (null);
    linkOriginPropertyName = (null);
    indexed = NO;
    isPrimary = NO;
    optional = NO;
}, address {
    type = string;
    objectClassName = (null);
    linkOriginPropertyName = (null);
    indexed = NO;
    isPrimary = NO;
    optional = NO;
}]

Where are the dogs and cats properties?

Thanks.

Upvotes: 1

Views: 1551

Answers (2)

Wamba
Wamba

Reputation: 69

I found the solution:

let computedProperties = Persons.sharedSchema()?.computedProperties

Upvotes: 1

Thomas Goyne
Thomas Goyne

Reputation: 8138

The LinkingObjects properties are listed in the computedProperties property of RLMObjectSchema, which is currently not public or present on the Swift version of the class. While it's possible to get to the private property given an instance of the obj-c class (via .valueForKey("computedProperties")), that won't work on the Swift ones and there isn't any good way to get to the obj-c RLMObjectSchema when using Realm Swift.

There's an existing feature request to expose this.

Upvotes: 0

Related Questions