Structures

The following structures are available globally.

  • This struct enables sequence-style enumeration for RLMObjects in Swift via RLMCollection.makeIterator

    See more

    Declaration

    Swift

    public struct RLMCollectionIterator : IteratorProtocol
  • This struct enables sequence-style enumeration for RLMDictionary in Swift via RLMDictionary.makeIterator

    See more

    Declaration

    Swift

    public struct RLMDictionaryIterator : IteratorProtocol
  • A Realm instance (also referred to as “a Realm”) represents a Realm database.

    Realms can either be stored on disk (see init(path:)) or in memory (see Configuration).

    Realm instances are cached internally, and constructing equivalent Realm objects (for example, by using the same path or identifier) produces limited overhead.

    If you specifically want to ensure a Realm instance is destroyed (for example, if you wish to open a Realm, check some property, and then possibly delete the Realm file and re-open it), place the code which uses the Realm within an autoreleasepool {} and ensure you have no other strong references to it.

    Warning

    warning Non-frozen RLMRealm instances are thread-confined and cannot be shared across threads or dispatch queues. Trying to do so will cause an exception to be thrown. You must obtain an instance of RLMRealm on each thread or queue you want to interact with the Realm on. Realms can be confined to a dispatch queue rather than the thread they are opened on by explicitly passing in the queue when obtaining the RLMRealm instance. If this is not done, trying to use the same instance in multiple blocks dispatch to the same queue may fail as queues are not always run on the same thread.
    See more

    Declaration

    Swift

    @frozen
    public struct Realm
    extension Realm: Equatable
  • LinkingObjects is an auto-updating container type. It represents zero or more objects that are linked to its owning model object through a property relationship.

    LinkingObjects can be queried with the same predicates as List<Element> and Results<Element>.

    LinkingObjects always reflects the current state of the Realm on the current thread, including during write transactions on the current thread. The one exception to this is when using for...in enumeration, which will always enumerate over the linking objects that were present when the enumeration is begun, even if some of them are deleted or modified to no longer link to the target object during the enumeration.

    LinkingObjects can only be used as a property on Object models. Properties of this type must be declared as let and cannot be dynamic.

    See more

    Declaration

    Swift

    @frozen
    public struct LinkingObjects<Element> where Element : ObjectBase, Element : RealmCollectionValue
    extension LinkingObjects: RealmSubscribable
    extension LinkingObjects: RealmCollection

AddableType

  • Results is an auto-updating container type in Realm returned from object queries.

    Results can be queried with the same predicates as List<Element>, and you can chain queries to further filter query results.

    Results always reflect the current state of the Realm on the current thread, including during write transactions on the current thread. The one exception to this is when using for...in enumeration, which will always enumerate over the objects which matched the query when the enumeration is begun, even if some of them are deleted or modified to be excluded by the filter during the enumeration.

    Results are lazily evaluated the first time they are accessed; they only run queries when the result of the query is requested. This means that chaining several temporary Results to sort and filter your data does not perform any unnecessary work processing the intermediate state.

    Once the results have been evaluated or a notification block has been added, the results are eagerly kept up-to-date, with the work done to keep them up-to-date done on a background thread whenever possible.

    Results instances cannot be directly instantiated.

    See more

    Declaration

    Swift

    @frozen
    public struct Results<Element> : Equatable where Element : RealmCollectionValue
    extension Results: RealmSubscribable
    extension Results: RealmCollection
    extension Results: Encodable where Element: Encodable
  • A type-erased RealmCollection.

    Instances of RealmCollection forward operations to an opaque underlying collection having the same Element type.

    See more

    Declaration

    Swift

    public struct AnyRealmCollection<Element> : RealmCollection, UntypedCollection where Element : RealmCollectionValue
    extension AnyRealmCollection: RealmSubscribable

Subscriptions

  • A subscription which wraps a Realm notification.

    See more

    Declaration

    Swift

    @available(macOS 10.15, watchOS 6.0, iOS 13.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOS 13.0, *)
    @frozen
    public struct ObservationSubscription : Subscription
  • A subscription which wraps a Realm AsyncOpenTask.

    See more

    Declaration

    Swift

    @available(macOS 10.15, watchOS 6.0, iOS 13.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOS 13.0, *)
    @frozen
    public struct AsyncOpenSubscription : Subscription

MapIndex

SingleMapEntry

  • Container for holding a single key-value entry in a Map. This is used where a tuple cannot be expressed as a generic arguement.

    See more

    Declaration

    Swift

    public struct SingleMapEntry<Key, Value> : _RealmMapValue, Hashable where Key : _MapKey, Value : RealmCollectionValue
  • Migration instances encapsulate information intended to facilitate a schema migration.

    A Migration instance is passed into a user-defined MigrationBlock block when updating the version of a Realm. This instance provides access to the old and new database schemas, the objects in the Realm, and provides functionality for modifying the Realm during the migration.

    See more

    Declaration

    Swift

    @frozen
    public struct Migration
  • Information about a specific property which changed in an Object change notification.

    See more

    Declaration

    Swift

    @frozen
    public struct PropertyChange
  • This class represents Realm model object schemas.

    When using Realm, ObjectSchema instances allow performing migrations and introspecting the database’s schema.

    Object schemas map to tables in the core database.

    See more

    Declaration

    Swift

    @frozen
    public struct ObjectSchema : CustomStringConvertible
    extension ObjectSchema: Equatable
  • Property instances represent properties managed by a Realm in the context of an object schema. Such properties may be persisted to a Realm file or computed from other data in the Realm.

    When using Realm, property instances allow performing migrations and introspecting the database’s schema.

    Property instances map to columns in the core database.

    See more

    Declaration

    Swift

    @frozen
    public struct Property : CustomStringConvertible
    extension Property: Equatable
  • An iterator for a RealmCollection instance.

    See more

    Declaration

    Swift

    @frozen
    public struct RLMIterator<Element> : IteratorProtocol where Element : RealmCollectionValue
  • An iterator for a RealmKeyedCollection instance.

    See more

    Declaration

    Swift

    @frozen
    public struct RLMMapIterator<Element> : IteratorProtocol where Element : _RealmMapValue
  • Schema instances represent collections of model object schemas managed by a Realm.

    When using Realm, Schema instances allow performing migrations and introspecting the database’s schema.

    Schemas map to collections of tables in the core database.

    See more

    Declaration

    Swift

    @frozen
    public struct Schema : CustomStringConvertible
    extension Schema: Equatable
  • A SortDescriptor stores a key path and a sort order for use with sorted(sortDescriptors:). It is similar to NSSortDescriptor, but supports only the subset of functionality which can be efficiently run by Realm’s query engine.

    See more

    Declaration

    Swift

    @frozen
    public struct SortDescriptor
    extension SortDescriptor: CustomStringConvertible
    extension SortDescriptor: Equatable
    extension SortDescriptor: ExpressibleByStringLiteral

StateRealmObject

  • A property wrapper type that instantiates an observable object.

    Create a state realm object in a SwiftUI/View, SwiftUI/App, or SwiftUI/Scene by applying the @StateRealmObject attribute to a property declaration and providing an initial value that conforms to the doc://com.apple.documentation/documentation/Combine/ObservableObject protocol:

    @StateRealmObject var model = DataModel()
    

    SwiftUI creates a new instance of the object only once for each instance of the structure that declares the object. When published properties of the observable realm object change, SwiftUI updates the parts of any view that depend on those properties. If unmanaged, the property will be read from the object itself, otherwise, it will be read from the underlying Realm. Changes to the value will update the view asynchronously:

    Text(model.title) // Updates the view any time `title` changes.
    

    You can pass the state object into a property that has the SwiftUI/ObservedRealmObject attribute.

    Get a SwiftUI/Binding to one of the state object’s properties using the $ operator. Use a binding when you want to create a two-way connection to one of the object’s properties. For example, you can let a SwiftUI/Toggle control a Boolean value called isEnabled stored in the model:

    Toggle("Enabled", isOn: $model.isEnabled)
    

    This will write the modified isEnabled property to the model object’s Realm.

    See more

    Declaration

    Swift

    @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
    @propertyWrapper
    public struct StateRealmObject<T> : DynamicProperty where T : RealmSubscribable, T : ThreadConfined, T : Equatable

ObservedResults

  • A property wrapper type that retrieves results from a Realm.

    The results use the realm configuration provided by the environment value EnvironmentValues/realmConfiguration.

    See more

    Declaration

    Swift

    @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
    @propertyWrapper
    public struct ObservedResults<ResultType> : DynamicProperty, BoundCollection where ResultType : RealmSwiftObject, ResultType : ObjectKeyIdentifiable

ObservedRealmObject

  • A property wrapper type that subscribes to an observable Realm Object or List and invalidates a view whenever the observable object changes.

    See more

    Declaration

    Swift

    @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
    @propertyWrapper
    public struct ObservedRealmObject<ObjectType> : DynamicProperty where ObjectType : ObservableObject, ObjectType : RealmSubscribable, ObjectType : ThreadConfined, ObjectType : Equatable
  • A SyncConfiguration represents configuration parameters for Realms intended to sync with MongoDB Realm.

    See more

    Declaration

    Swift

    @frozen
    public struct SyncConfiguration
  • Structure providing an interface to call a MongoDB Realm function with the provided name and arguments.

    user.functions.sum([1, 2, 3, 4, 5]) { sum, error in
        guard case let .int64(value) = sum else {
            print(error?.localizedDescription)
        }
    
        assert(value == 15)
    }
    

    The dynamic member name (sum in the above example) is directly associated with the function name. The first argument is the BSONArray of arguments to be provided to the function. The second and final argument is the completion handler to call when the function call is complete. This handler is executed on a non-main global DispatchQueue.

    See more

    Declaration

    Swift

    @dynamicMemberLookup
    @frozen
    public struct Functions
  • An object intended to be passed between threads containing a thread-safe reference to its thread-confined object.

    To resolve a thread-safe reference on a target Realm on a different thread, pass to Realm.resolve(_:).

    Warning

    A ThreadSafeReference object must be resolved at most once. Failing to resolve a ThreadSafeReference will result in the source version of the Realm being pinned until the reference is deallocated.

    Note

    Prefer short-lived ThreadSafeReferences as the data for the version of the source Realm will be retained until all references have been resolved or deallocated.

    See more

    Declaration

    Swift

    @frozen
    public struct ThreadSafeReference<Confined> where Confined : ThreadConfined