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") }}
NSBundle *testBundle = [NSBundle bundleForClass:self.class];NSURL *attachmentTestImage = [testBundle URLForResource:@"attachment_test" withExtension:@"png"];NSData *attachmentData = [NSData dataWithContentsOfURL:attachmentTestImage];
NSDictionary<NSString *, NSString *> *metadata = @{@"name": @"my_image.png"};DITAttachment *attachment = [collection newAttachment:attachmentTestImage.path metadata:metadata];
DITDocumentID *docID = [collection upsert:@{@"some": @"string", @"my_attachment": attachment} error:nil];DITDocument *doc = [[collection findByID:docID] exec];DITAttachmentToken *attachmentToken = doc[@"my_attachment"].attachmentToken;
DITAttachmentFetcher *fetcher = [collection fetchAttachment:attachmentToken onFetchEvent:^(DITAttachmentFetchEvent *event) { switch (event.type) { case DITAttachmentFetchEventTypeCompleted: { DITAttachmentFetchEventCompleted *completed = [event asCompleted]; DITAttachment *fetchedAttachment = completed.attachment; NSData *fetchedAttachmentData = [fetchedAttachment getData:nil]; [attachmentRoundtripExpectation fulfill]; break; } case DITAttachmentFetchEventTypeProgress: break; default: break; }}];