Protocols

The following protocols are available globally.

Identifiable

  • A protocol which defines a default identity for Realm Objects

    Declaring your Object subclass as conforming to this protocol will supply a default implementation for Identifiable‘s id which works for Realm Objects:

    // Automatically conforms to `Identifiable`
    class MyObjectType: Object, ObjectKeyIdentifiable {
        // ...
    }
    

    You can also manually conform to Identifiable if you wish, but note that using the object’s memory address does not work for managed objects.

    See more

    Declaration

    Swift

    @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
    public protocol ObjectKeyIdentifiable : ObjectBase, Identifiable

Combine

  • A type which can be passed to valuePublisher() or changesetPublisher().

    Declaration

    Swift

    @available(macOS 10.15, watchOS 6.0, iOS 13.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOS 13.0, *)
    public protocol RealmSubscribable
  • A homogenous collection of Objects which can be retrieved, filtered, sorted, and operated upon.

    See more

    Declaration

    Swift

    public protocol RealmCollection : RealmCollectionBase
  • A homogenous key-value collection of Objects which can be retrieved, filtered, sorted, and operated upon.

    See more

    Declaration

    Swift

    public protocol RealmKeyedCollection : ThreadConfined, CustomStringConvertible, Sequence
  • Delegate which is used for subscribing to changes on a MongoCollection.watch() stream.

    See more

    Declaration

    Swift

    public protocol ChangeEventDelegate : AnyObject
  • An enum type which can be stored on a Realm Object.

    Only @objc enums backed by an Int can be stored on a Realm object, and the enum type must explicitly conform to this protocol. For example:

    @objc enum MyEnum: Int, RealmEnum {
       case first = 1
       case second = 2
       case third = 7
    }
    
    class MyModel: Object {
       @objc dynamic enumProperty = MyEnum.first
       let optionalEnumProperty = RealmOptional<MyEnum>()
    }
    

    Declaration

    Swift

    public protocol RealmEnum : RealmOptionalType, _RealmSchemaDiscoverable
  • A protocol describing types that can parameterize a RealmOptional.

    Declaration

    Swift

    public protocol RealmOptionalType
  • An enum type which can be used with @Persisted.

    Persisting an enum in Realm requires that it have a raw value and that the raw value by a type which Realm can store. The enum also has to be explicitly marked as conforming to this protocol as Swift does not let us do so implicitly.

    enum IntEnum: Int, PersistableEnum {
       case first = 1
       case second = 2
       case third = 7
    }
    enum StringEnum: String, PersistableEnum {
       case first = "a"
       case second = "b"
       case third = "g"
    }
    

    If the Realm contains a value which is not a valid member of the enum (such as if it was written by a different sync client which disagrees on which values are valid), optional enum properties will return nil, and non-optional properties will abort the process.

    Declaration

    Swift

    public protocol PersistableEnum : RealmEnum, _OptionalPersistable, CaseIterable, RawRepresentable
  • A type which can be indexed.

    This protocol is merely a tag and declaring additional types as conforming to it will simply result in runtime errors rather than compile-time errors.

    Declaration

    Swift

    public protocol _Indexable
  • A type which can be made the primary key of an object.

    This protocol is merely a tag and declaring additional types as conforming to it will simply result in runtime errors rather than compile-time errors.

    Declaration

    Swift

    public protocol _PrimaryKey
  • A type which can be stored in a Realm List, MutableSet, or Results.

    Declaring additional types as conforming to this protocol will not make them actually work. Most of the logic for how to store values in Realm is not implemented in Swift and there is currently no extension mechanism for supporting more types.

    Declaration

    Swift

    public protocol RealmCollectionValue : _RealmSchemaDiscoverable, Hashable
  • A protocol describing types that can parameterize a RealmPropertyType.

    Declaration

    Swift

    public protocol RealmPropertyType

MinMaxType

  • Types of properties which can be used with the minimum and maximum value APIs.

    See

    min(ofProperty:), max(ofProperty:)

    Declaration

    Swift

    public protocol MinMaxType

AddableType

  • Types of properties which can be used with the sum and average value APIs.

    See

    sum(ofProperty:), average(ofProperty:)

    Declaration

    Swift

    public protocol AddableType
  • Objects of types which conform to ThreadConfined can be managed by a Realm, which will make them bound to a thread-specific Realm instance. Managed objects must be explicitly exported and imported to be passed between threads.

    Managed instances of objects conforming to this protocol can be converted to a thread-safe reference for transport between threads by passing to the ThreadSafeReference(to:) constructor.

    Note that only types defined by Realm can meaningfully conform to this protocol, and defining new classes which attempt to conform to it will not make them work with ThreadSafeReference.

    See more

    Declaration

    Swift

    public protocol ThreadConfined