DittoPendingCursorOperation
public class DittoPendingCursorOperation
These objects are returned when using find
-like functionality on DittoCollection
s.
They allow chaining of further query-related functions to do things like add a limit to the
number of documents you want returned or specify how you want the documents to be sorted and
ordered. You can either call exec
on the object to get an array of DittoDocument
s as an
immediate return value, or you can establish either a live query or a subscription, which both
work over time.
A live query, established by calling observeLocal
, will notify you every time there’s an
update to a document that matches the query you provided in the preceding find
-like call.
A subscription, established by calling subscribe
, will act as a signal to other peers that
the device connects to that you would like to receive updates from them about documents
that match the query you provided in the preceding find
-like call.
Typically, an app would set up a subscribe
in some part of the application which is long-lived
to ensure the device receives updates from the mesh. These updates will be automatically
received and written into the local store. Elsewhere, where you need to use this data, an
observeLocal
can be used to notify you of the data, and all subsequent changes to the data.
Update and remove functionality is also exposed through this object.
-
Limit the number of documents that get returned when querying a collection for matching documents.
Declaration
Swift
public func limit(_ limit: Int32) -> Self
Parameters
limit
The maximum number of documents that will be returned.
Return Value
A
DittoPendingCursorOperation
that you can chain further function calls and then either get the matching documents immediately or get updates about them over time. -
Sort the documents that match the query provided in the preceding
find
-like function call.Declaration
Swift
public func sort(_ query: String, direction: DittoSortDirection) -> Self
Parameters
query
The query specifies the logic to be used when sorting the matching documents.
direction
Specify whether you want the sorting order to be ascending or descending.
Return Value
A
DittoPendingCursorOperation
that you can chain further function calls and then either get the matching documents immediately or get updates about them over time. -
Offset the resulting set of matching documents. This is useful if you aren’t interested in the first N matching documents for one reason or another. For example, you might already have queried the collection and obtained the first 20 matching documents and so you might want to run the same query as you did previously but ignore the first 20 matching documents, and that is where you would use
offset
.Declaration
Swift
public func offset(_ offset: UInt32) -> Self
Parameters
offset
The number of matching documents that you want the eventual resulting set of matching documents to be offset by (and thus not include).
Return Value
A
DittoPendingCursorOperation
that you can chain further function calls and then either get the matching documents immediately or get updates about them over time. -
Enables you to subscribe to changes that occur on a collection. Having a subscription acts as a signal to others that you are interested in receiving updates when local or remote changes are made to documents that match the query generated by the chain of operations that precedes the call to
subscribe
. The returnedDittoSubscription
object must be kept in scope for as long as you want to keep receiving updates.Declaration
Swift
public func subscribe() -> DittoSubscription
Return Value
A
DittoSubscription
object that must be kept in scope for as long as you want to keep receiving updates for documents that match the query specified in the preceding chain. -
Remove all documents that match the query generated by the preceding function chaining.
Declaration
Swift
@discardableResult public func remove() -> [DittoDocumentID]
Return Value
A list containing the IDs of the documents that were removed.
-
Evict all documents that match the query generated by the preceding function chaining.
Declaration
Swift
@discardableResult public func evict() -> [DittoDocumentID]
Return Value
A list containing the IDs of the documents that were evicted.
-
Execute the query generated by the preceding function chaining and return the list of matching documents.
Declaration
Swift
public func exec() -> [DittoDocument]
Return Value
A list of
DittoDocument
s matching the query generated by the preceding function chaining. -
Enables you to listen for changes that occur on a collection. The
eventHandler
closure will be called when local or remote changes are made to documents that match the query generated by the chain of operations that precedes the call toobserve
. The returnedDittoLiveQuery
object must be kept in scope for as long as you want the providedeventHandler
to be called when an update occurs.Declaration
Swift
@available(*, deprecated, message: "Use observeLocal(deliverOn:eventHandler:﹚ and a separate call to subscribe(﹚ instead. See https://docs.ditto.live/common/concepts/syncing-data#subscribe") public func observe( deliverOn queue: DispatchQueue = .main, eventHandler: @escaping ([DittoDocument], DittoLiveQueryEvent) -> Void ) -> DittoLiveQuery
Parameters
queue
The
DispatchQueue
that will be used to deliver live query updates. Defaults toDispatchQueue.main
.eventHandler
A closure that will be called every time there is a transaction committed to the store that involves modifications to documents matching the query in the collection that
observe
was called on.Return Value
A
DittoLiveQuery
object that must be kept in scope for as long as you want to keep receiving updates. -
Enables you to listen for changes that occur on a collection, and to signal when you are ready for the live query to deliver the next event. The
eventHandler
closure will be called when local or remote changes are made to documents that match the query generated by the chain of operations that precedes the call toobserve
. The returnedDittoLiveQuery
object must be kept in scope for as long as you want the providedeventHandler
to be called when an update occurs.Declaration
Swift
@available(*, deprecated, message: "Use observeLocalWithNextSignal(deliverOn:eventHandler:﹚ and a separate call to subscribe(﹚ instead. See https://docs.ditto.live/common/concepts/syncing-data#subscribe") public func observeWithNextSignal( deliverOn queue: DispatchQueue = .main, eventHandler: @escaping ([DittoDocument], DittoLiveQueryEvent, @escaping DittoSignalNext) -> Void ) -> DittoLiveQuery
Parameters
queue
The
DispatchQueue
that will be used to deliver live query updates. Defaults toDispatchQueue.main
.eventHandler
A closure that will be called every time there is a transaction committed to the store that involves modifications to documents matching the query in the collection that
observe
was called on.Return Value
A
DittoLiveQuery
object that must be kept in scope for as long as you want to keep receiving updates. -
Enables you to listen for changes that occur on a collection. This won’t subscribe to receive changes made remotely by others and so it will only fire updates when a local change is made. If you want to receive remotely performed updates as well then also call
subscribe
with the relevant query. The returnedDittoLiveQuery
object must be kept in scope for as long as you want the providedeventHandler
to be called when an update occurs.Declaration
Swift
public func observeLocal( deliverOn queue: DispatchQueue = .main, eventHandler: @escaping ([DittoDocument], DittoLiveQueryEvent) -> Void ) -> DittoLiveQuery
Parameters
queue
The
DispatchQueue
that will be used to deliver live query updates. Defaults toDispatchQueue.main
.eventHandler
A closure that will be called every time there is a transaction committed to the store that involves modifications to documents matching the query in the collection that
observeLocal
was called on.Return Value
A
DittoLiveQuery
object that must be kept in scope for as long as you want to keep receiving updates. -
Enables you to listen for changes that occur on a collection, and to signal when you are ready for the live query to deliver the next event.. This won’t subscribe to receive changes made remotely by others and so it will only fire updates when a local change is made. If you want to receive remotely performed updates as well then also call
subscribe
with the relevant query. The returnedDittoLiveQuery
object must be kept in scope for as long as you want the providedeventHandler
to be called when an update occurs.Declaration
Swift
public func observeLocalWithNextSignal( deliverOn queue: DispatchQueue = .main, eventHandler: @escaping ([DittoDocument], DittoLiveQueryEvent, @escaping DittoSignalNext) -> Void ) -> DittoLiveQuery
Parameters
queue
The
DispatchQueue
that will be used to deliver live query updates. Defaults toDispatchQueue.main
.eventHandler
A closure that will be called every time there is a transaction committed to the store that involves modifications to documents matching the query in the collection that
observeLocal
was called on.Return Value
A
DittoLiveQuery
object that must be kept in scope for as long as you want to keep receiving updates. -
Update documents that match the query generated by the preceding function chaining.
Declaration
Swift
@discardableResult public func update( _ closure: @escaping ([DittoMutableDocument]) -> Void ) -> [DittoDocumentID: [DittoUpdateResult]]
Parameters
closure
A closure that gets called with all of the documents matching the query. The documents are
DittoMutableDocument
s so you can call update-related functions on them.Return Value
A dictionary of document IDs to lists of
DittoUpdateResult
that describes the updates that were performed for each document. -
Undocumented
Declaration
Swift
typealias Snapshot = (documents: [DittoDocument], event: DittoLiveQueryEvent)
-
A Combine publisher that produces documents matching the requested query.
See moreDeclaration
Swift
struct LiveQueryPublisher : Publisher
-
A Combine publisher that produces documents matching the requested query.
Declaration
Swift
func liveQueryPublisher() -> LiveQueryPublisher
Return Value
A
LiveQueryPublisher
which has an output of([DittoDocument], DittoLiveQueryEvent)