Publisher
extension Publisher
extension Publisher where Output: ThreadConfined
-
Freezes all Realm objects and collections emitted by the upstream publisher
Freezing a Realm object makes it no longer live-update when writes are made to the Realm and makes it safe to pass freely between threads without using
.threadSafeReference()
.// Get a publisher for a Results let cancellable = myResults.publisher // Convert to frozen Results .freeze() // Unlike live objects, frozen objects can be sent to a concurrent queue .receive(on: DispatchQueue.global()) .sink { frozenResults in // Do something with the frozen Results }
Declaration
Swift
public func freeze<T>() -> Publishers.Map<Self, T> where T : ThreadConfined, T == Self.Output
Return Value
A publisher that publishes frozen copies of the objects which the upstream publisher publishes.
-
Freezes all Realm object changesets emitted by the upstream publisher.
Freezing a Realm object changeset makes the included object reference no longer live-update when writes are made to the Realm and makes it safe to pass freely between threads without using
.threadSafeReference()
. It also guarantees that the frozen object contained in the changset will always match the property changes, which is not always the case when using thread-safe references.// Get a changeset publisher for an object let cancellable = changesetPublisher(object) // Convert to frozen changesets .freeze() // Unlike live objects, frozen objects can be sent to a concurrent queue .receive(on: DispatchQueue.global()) .sink { changeset in // Do something with the frozen changeset }
Declaration
Swift
public func freeze<T>() -> Publishers.Map<Self, ObjectChange<T>> where T : Object, Self.Output == ObjectChange<T>
Return Value
A publisher that publishes frozen copies of the changesets which the upstream publisher publishes.
-
Freezes all Realm collection changesets from the upstream publisher.
Freezing a Realm collection changeset makes the included collection reference no longer live-update when writes are made to the Realm and makes it safe to pass freely between threads without using
.threadSafeReference()
. It also guarantees that the frozen collection contained in the changset will always match the change information, which is not always the case when using thread-safe references.// Get a changeset publisher for a collection let cancellable = myList.changesetPublisher // Convert to frozen changesets .freeze() // Unlike live objects, frozen objects can be sent to a concurrent queue .receive(on: DispatchQueue.global()) .sink { changeset in // Do something with the frozen changeset }
Declaration
Swift
public func freeze<T: RealmCollection>() -> Publishers.Map<Self, RealmCollectionChange<T>> where Output == RealmCollectionChange<T>
Return Value
A publisher that publishes frozen copies of the changesets which the upstream publisher publishes.
-
Enables passing object changesets to a different dispatch queue.
Each call to
receive(on:)
on a publisher which emits Realm thread-confined objects must be proceeded by a call to.threadSafeReference()
. The returned publisher handles the required logic to pass the thread-confined object to the new queue. Only serial dispatch queues are supported and using other schedulers will result in a fatal error.For example, to subscribe on a background thread, do some work there, then pass the object changeset to the main thread you can do:
let cancellable = changesetPublisher(myObject) .subscribe(on: DispatchQueue(label: "background queue") .print() .threadSafeReference() .receive(on: DispatchQueue.main) .sink { objectChange in // Do things with the object on the main thread }
Declaration
Swift
public func threadSafeReference<T: Object>() -> RealmPublishers.MakeThreadSafeObjectChangeset<Self, T> where Output == ObjectChange<T>
Return Value
A publisher that supports
receive(on:)
for thread-confined objects. -
Enables passing Realm collection changesets to a different dispatch queue.
Each call to
receive(on:)
on a publisher which emits Realm thread-confined objects must be proceeded by a call to.threadSafeReference()
. The returned publisher handles the required logic to pass the thread-confined object to the new queue. Only serial dispatch queues are supported and using other schedulers will result in a fatal error.For example, to subscribe on a background thread, do some work there, then pass the collection changeset to the main thread you can do:
let cancellable = myCollection.changesetPublisher .subscribe(on: DispatchQueue(label: "background queue") .print() .threadSafeReference() .receive(on: DispatchQueue.main) .sink { collectionChange in // Do things with the collection on the main thread }
Declaration
Swift
public func threadSafeReference<T: RealmCollection>() -> RealmPublishers.MakeThreadSafeCollectionChangeset<Self, T> where Output == RealmCollectionChange<T>
Return Value
A publisher that supports
receive(on:)
for thread-confined objects.
-
Enables passing thread-confined objects to a different dispatch queue.
Each call to
receive(on:)
on a publisher which emits Realm thread-confined objects must be proceeded by a call to.threadSafeReference()
.The returned publisher handles the required logic to pass the thread-confined object to the new queue. Only serial dispatch queues are supported and using other schedulers will result in a fatal error.For example, to subscribe on a background thread, do some work there, then pass the object to the main thread you can do:
let cancellable = publisher(myObject) .subscribe(on: DispatchQueue(label: "background queue") .print() .threadSafeReference() .receive(on: DispatchQueue.main) .sink { object in // Do things with the object on the main thread }
Calling this function on a publisher which emits frozen or unmanaged objects is unneccesary but is allowed.
Declaration
Swift
public func threadSafeReference() -> RealmPublishers.MakeThreadSafe<Self>
Return Value
A publisher that supports
receive(on:)
for thread-confined objects.