Skip to main content

Attachment

If you have a large amount of binary data, or perhaps just a large file, that you want to sync between devices then instead of inserting this into a document as bytes you should make use of the attachments feature.

Attachments do not get synced between devices by default, even if they are part of a document that is being synced between devices. This is because they could be very large files that a given device doesn't need. Instead an attachment must be explicitly fetched using an attachment token that will be present in the document that the attachment is linked to.

let collection = ditto.store["foo"]
let myImageURL = bundle.url(forResource: "image", withExtension: "png")!
let metadata = ["name": "my_image.png"]let attachment = collection.newAttachment(    path: myImageURL.path,    metadata: metadata)!
guard let docID = try? collection.upsert(["some": "string", "my_attachment": attachment]) else {    // handle error    return}
// Later, find the document and the fetch the attachment
let doc = collection.findByID(docID).exec()let attachmentToken = doc!["my_attachment"].attachmentToken!
let fetcher = collection.fetchAttachment(token: attachmentToken) { status in    switch status {    case .completed(let fetchedAttachment):        // Do something with attachment    default:        print("Unable to fetch attachment")    }}