ObservedResults

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

A property wrapper type that represents the results of a query on a realm.

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

Unlike non-SwiftUI results collections, the ObservedResults is mutable. Writes to an ObservedResults collection implicitly perform a write transaction. If you add an object to the ObservedResults that the associated query would filter out, the object is added to the realm but not included in the ObservedResults.

Given @ObservedResults var v in SwiftUI, $v refers to a BoundCollection.

  • Declaration

    Swift

    public typealias Element = ResultType
  • Stores an NSPredicate used for filtering the Results. This is mutually exclusive to the where parameter.

    Declaration

    Swift

    @State
    public var filter: NSPredicate? { get nonmutating set }
  • Stores a type safe query used for filtering the Results. This is mutually exclusive to the filter parameter.

    Declaration

    Swift

    @State
    public var `where`: ((Query<ResultType>) -> Query<Bool>)? { get nonmutating set }
  • Initialize a ObservedResults struct for a given Projection type.

    Declaration

    Swift

    public init<ObjectType: ObjectBase>(_ type: ResultType.Type,
                                        configuration: Realm.Configuration? = nil,
                                        filter: NSPredicate? = nil,
                                        keyPaths: [String]? = nil,
                                        sortDescriptor: SortDescriptor? = nil) where ResultType: Projection<ObjectType>, ObjectType: ThreadConfined

    Parameters

    type

    Observed type

    configuration

    The Realm.Configuration used when creating the Realm, user’s sync configuration for the given partition value will be set as the syncConfiguration, if empty the configuration is set to the defaultConfiguration

    filter

    Observations will be made only for passing objects. If no filter given - all objects will be observed

    keyPaths

    Only properties contained in the key paths array will be observed. If nil, notifications will be delivered for any property change on the object. String key paths which do not correspond to a valid a property will throw an exception.

    sortDescriptor

    A sequence of SortDescriptors to sort by

  • Initialize a ObservedResults struct for a given Object or EmbeddedObject type.

    Declaration

    Swift

    public init(_ type: ResultType.Type,
                configuration: Realm.Configuration? = nil,
                filter: NSPredicate? = nil,
                keyPaths: [String]? = nil,
                sortDescriptor: SortDescriptor? = nil) where ResultType: Object

    Parameters

    type

    Observed type

    configuration

    The Realm.Configuration used when creating the Realm, user’s sync configuration for the given partition value will be set as the syncConfiguration, if empty the configuration is set to the defaultConfiguration

    filter

    Observations will be made only for passing objects. If no filter given - all objects will be observed

    keyPaths

    Only properties contained in the key paths array will be observed. If nil, notifications will be delivered for any property change on the object. String key paths which do not correspond to a valid a property will throw an exception.

    sortDescriptor

    A sequence of SortDescriptors to sort by

  • Initialize a ObservedResults struct for a given Object or EmbeddedObject type.

    Declaration

    Swift

    public init(_ type: ResultType.Type,
                configuration: Realm.Configuration? = nil,
                where: ((Query<ResultType>) -> Query<Bool>)? = nil,
                keyPaths: [String]? = nil,
                sortDescriptor: SortDescriptor? = nil) where ResultType: Object

    Parameters

    type

    Observed type

    configuration

    The Realm.Configuration used when creating the Realm, user’s sync configuration for the given partition value will be set as the syncConfiguration, if empty the configuration is set to the defaultConfiguration

    where

    Observations will be made only for passing objects. If no type safe query is given - all objects will be observed

    keyPaths

    Only properties contained in the key paths array will be observed. If nil, notifications will be delivered for any property change on the object. String key paths which do not correspond to a valid a property will throw an exception.

    sortDescriptor

    A sequence of SortDescriptors to sort by

  • Declaration

    Swift

    nonisolated public func update()