Photo sync and sync-rules

I would like to know how Journey handles the syncing of photos.

  1. Do all photos sync across all devices, or are they only available on command?
  2. If they are only available on command, is there any reason to apply sync rules for photos?
  3. And if photos are only synced on command, is there a way to enable storage for offline use?

Hey @alex

This is an excellent question.

In General
Photos (all attachments really, e.g. signatures, files) are not stored in the standard DB but instead stored separately from the “raw data” in an AttachmentStore. What is stored in the DB is a reference (an Attachment ID) to the entry in the AttachmentStore. A bi-product of the separate storage is that the attachments are not synched automatically with the rest of the data, this is done to limit unnecessary network traffic / data usage.

By default attachments are synched on demand and cached locally on the device. The demand is triggered whenever the app/device tries to access the attachment programmatically, either using a view component (like a <display-photo/>) or by trying to access it from JS/TS (for example using journey.files.viewFile(someAttachment)). At that point the device will try to access the attachment from local storage and, if not available, download the attachment from the Cloud AttachmentStore and cache it locally to use in subsequent requests. If no internet connection is available then the attachment will not be downloaded and there will be no attachment to interact with.

Attachments that are created on the device will automatically be cached on the device. Finally all cached attachments eventually get uncached / unsynced on a first-in first-out basis.

Sync Rules
You cannot setup sync rules for attachments, only for the objects that hold the references to the attachments. So if you have a model called photo that has a field for a photo attachment and a text field for a name, then you can setup sync rules for your photo objects but not for the actual photo attachments.

For example
Data Model

<model name="photo" label="Photo">
    <field name="name" label="Name" type="text:name" />
    <field name="photo" label="Photo" type="photo" />

    <belongs-to model="user" name="user" />
    <display>{name}</display>
</model>

Sync Rules

<bucket via="self">
    <has-many name="photos" />
</bucket>

This would sync the photo DB objects, but not the actual photo attachment files. Those would only get “synced” once you try to use them in the app.

Syncing Attachments Automatically [ENTERPRISE ONLY]
Our Enterprise Customers do have another option though. You cannot use sync rules to make attachments available offline automatically, but you can set a XML attribute on the Data Model that will make the attachment automatically download to all the devices that the objects are being synced to. The attribute is called auto-download="" and can be specified on any attachment field, e.g. photo, signature and attachment

For example
Data Model

<model name="photo" label="Photo">
    <field name="name" label="Name" type="text:name" />
    <field name="photo" label="Photo" type="photo" auto-download="true" />
        
    <belongs-to model="user" />
    <display>{name}</display>
</model>

And so, assuming the same sync rules as before, this will now mean that for every photo DB object that is synced to a device, if that object has a photo attachment available, that will also be downloaded to the device. Please note attachments are downloaded separately from and after the raw data, and again this feature is only available on our ENTERPRISE TIER.

I hope this helps

Thank you @tielman for the detailed response. This is certainly very helpful.

One follow up question: In the case where users would only like to store a small amount of photos for offline use, do you think there would be a way to cache only this pre-selected subset of photos (for let’s say 24 h)?

@alex You would manage this through your sync rules. You cannot programmatically decide when the attachments get synced / unsynced, only when the objects they are attached to get synched (and then assuming you have auto-download="true" the attachments themselves will sync along).

So, in your case, once the user is done with his workflow (or whatever logical point you decide), simply flag the objects in such a way as to make them no longer sync according to the sync rules.

All that said, way would you care if the photos are cached for longer?

@tielman Thank you, this makes a lot of sense.