Sync Rules v1 (deprecated)

Sync rules give developers the ability to determine programmatically what data should be sync to which devices. Sync rules are important in production applications, where devices should only store data that's needed for the app to work for security and performance reasons.

Developers can specify sync rules for their app by opening sync_rules.xml in OXIDE.

Basic Sync Rules Types

A developer can specify four types of sync rules:

  1. Rules for the user object

  2. Rules for globally-synced objects, called object

  3. Rules for has_many relationships

  4. Rules for belongs_to relationships

It is possible to use a condition in user, object and in has_many relationships to filter the objects that should be synced per device. This is particularly useful for apps where you'd like to sync objects with certain statuses, or are applying an archiving strategy for older objects.

Constraints

Please take note of the following constraints in sync rules:

  1. Apart from globally synced objects, you can only sync objects directly or indirectly to the user.

  2. You can sync any two nested relationships from the user. The single exception is userhas_manybelongs_to, which is not allowed. This means that many-to-many relationships with the user are not currently supported in sync rules.

  3. You cannot nest any relationships from a global object

  4. condition can be == (equal), != (not equal), > (greater than), < (less than), &gte; (greater than or equal), &lte; (less than or equal). Conditions can be used within has_many and object.

Examples: The basics

Sync everything (the default):

The following example, on the other hand, synchronizes nothing except the user itself. As soon as the user type is referenced in the sync rules, all objects have to be explicitly specified.

Here is a more complicated example:

Conditions on user role:

Examples: Real-life applications

In this section we will show you a few real-life examples of how sync rules can be used.

Simple Field Service App

Here's a sample data model for a simple field service app.

This is an example of a "sync rule compatible" data model, with nested has_many relationships from the Technician (user) object. In this case, all of the Job Cards belonging to the Technician and all of their Line Items and Photos can be synced to the device.

In addition, it would be possible to sync all Products globally.

Here's an example of corresponding sync rules for the data model above:

Time Study App

Here's a sample data model for a simple time study app.

This is another example of a "sync rule compatible" data model for a time-tracking app where the User belongs-to an Operation, which has multiple Observations. In this case all the operations from the User's current operation can be synced.

Here are the corresponding sync rules:

Syncing everything (default)

The default for every app is to sync all data to all devices:

Not specifying sync rules means that all users will get all data. This is often inadequate in production where data volumes will grow linearly over time, scaled by the number of active devices. Having more data synced to an app impacts the performance of queries, and also adds general memory overhead to the app.

Sync only the user object

The following example, on the other hand, synchronizes nothing except the user itself. This is not useful in practice but illustrates how sync rules work. As soon as the user type is referenced in the sync rules, all objects have to be explicitly specified.

A complicated example:

Here is a more complicated example that shows all the constraints of sync rules:

User roles

It is possible to have multiple user sync rules blocks with different conditions to account for user roles:

Developers Notes

Here are a few final notes on sync rules for developers:

  • Once sync rules have been deployed for an app, it will become critically important to update the rules when new models are added to the data model, or if model or relationship names change. Failing to do so will result in queries not showing the necessary data in the view, or logic in the JS failing.

  • Sync rules affect what data can be accessed in the app via DB. OnlineDB is unaffected by sync rules.

  • Sync rules constraints have a big impact on data model design. It is often useful to create "sync relationships" which are only used for sync rules, but have no other meaning in the data model.

Guidance around using sync rules

A general rule is to sync only the data that is applicable to a specific app. The fewer data synced, the more performant the app will be in general:

  • Fewer than 10k objects on a device is ideal.

  • Up to 50k objects synced to a device should work but needs to be tested properly. At these data volumes, the app becomes sensitive to poorly-written and unindexed queries. Be sure to monitor the DB size for increases (in the app diagnostics) to prevent unforeseen performance degradations.

  • Up to 500k objects synced to a device can work, but even with performant queries the sync performance will become slow. This will be exacerbated if there are a large number of users syncing the same data or if the data changes often. Strongly consider using aggressive archiving strategies, or OnlineDB.

Last updated