DittoCollection

public class DittoCollection

A reference to a collection in a DittoStore.

  • The name of the collection.

    Declaration

    Swift

    public var name: String { get }
  • Inserts a new document into the collection and returns its assigned ID.

    Throws

    DittoSwiftError.

    Precondition

    The document content to insert must conform to Codable.

    Declaration

    Swift

    @discardableResult
    public func insert<T: Codable>(
        _ content: T,
        id: String? = nil,
        isDefault: Bool = false
    ) throws -> String

    Parameters

    content

    The new document to insert.

    id

    The ID to use for the document. If null then Ditto will automatically assign an ID.

    isDefault

    Represents whether or not the data being inserted should be treated as default data or not. Set this to true if you want to set a default value that you expect to be overwritten by other devices in the network. The default value is false.

    Return Value

    The ID of the inserted document.

  • Inserts a new document into the collection and returns its assigned ID.

    Throws

    DittoSwiftError.

    Declaration

    Swift

    @discardableResult
    public func insert(
        _ content: [String: Any?],
        id: String? = nil,
        isDefault: Bool = false
    ) throws -> String

    Parameters

    content

    The new document to insert.

    id

    The ID to use for the document. If null then Ditto will automatically assign an ID.

    isDefault

    Represents whether or not the data being inserted should be treated as default data or not. Set this to true if you want to set a default value that you expect to be overwritten by other devices in the network. The default value is false.

    Return Value

    The ID of the inserted document.

  • Generates a DittoPendingIDSpecificOperation with the provided document ID that can be used to find the document at a point in time or you can chain a call to observe, observeLocal, or subscribe if you want to get updates about the document over time. It can also be used to update, remove or evict the document.

    Declaration

    Swift

    public func findByID(_ id: String) -> DittoPendingIDSpecificOperation

    Parameters

    id

    The ID of the document.

    Return Value

    A DittoPendingIDSpecificOperation that you can chain function calls to either get the document immediately or get updates about it over time.

  • Generates a DittoPendingCursorOperation with the provided query that can be used to find the documents matching the query at a point in time or you can chain a call to observe, observeLocal, or subscribe if you want to get updates about documents matching the query as they occur. It can also be used to update, remove, or evict documents.

    Declaration

    Swift

    public func find(_ query: String) -> DittoPendingCursorOperation

    Parameters

    query

    The query to run against the collection.

    Return Value

    A DittoPendingCursorOperation that you can use to chain further query-related function calls.

  • Generates a DittoPendingCursorOperation that can be used to find all documents in the collection at a point in time or you can chain a call to observe, observeLocal, or subscribe if you want to get updates about documents in the collection over time. It can also be used to update, remove or evict documents.

    Declaration

    Swift

    public func findAll() -> DittoPendingCursorOperation

    Return Value

    A DittoPendingCursorOperation that you can use to chain further query-related function calls.

  • Creates a new attachment, which can then be inserted into a document.

    The file residing at the provided path will be copied into the Ditto’s store. The DITAttachment object that is returned is what you can then use to insert an attachment into a document.

    You can provide metadata about the attachment, which will be replicated to other peers alongside the file attachment.

    Below is a snippet to how you can use the newAttachment functionality to insert an attachment into a document.

    if let attachment = collection.insert.newAttachment("/path/to/my/file.pdf") {
        let docID = try! collection.insert(["attachment": attachment, "other": "string"])
    }
    

    Declaration

    Swift

    public func newAttachment(path: String, metadata: [String : String] = [:]) -> DittoAttachment?

    Parameters

    path

    Path to the file on disk that will be used as the attachment.

    metadata

    Optional metadata relating to the attachment.

    Return Value

    A DittoAttachment object, which can be used to insert the attachment into a document.

  • Fetch the attachment corresponding to the provided attachment token.

    Declaration

    Swift

    public func fetchAttachment(
        token: DittoAttachmentToken,
        onFetchEvent: @escaping (DittoAttachmentFetchEvent) -> Void
    ) -> DittoAttachmentFetcher?

    Parameters

    token

    The token for the attachment that you want to fetch.

    onFetchEvent

    A closure that will be called when there is an update relating to the attachment fetch attempt. If the attachment is already available then this will be called almost immediately with a completed fetch event value.

    Return Value

    A DittoAttachmentFetcher object, which must be kept alive for the fetch request to proceed and for you to be notified about the attachment fetch attempt’s events. If the attachment fetcher could not be created then nil will be returned. This can happen if, for example, an invalid attachment token was provided.