Skip to main content

Use Data

 

Applies only to Mobile Apps
OutSystems

Read/Write Data One-to-Many

This data synchronization pattern is recommended for mobile apps with entities that follow a one-to-many relationship and where it is not likely for multiple end-users to change the same data while the apps are offline, as follows:

  • The server database holds the master data that can change over time;
  • The local storage database holds a subset of the master data and can be modified;
  • Synchronization sends the modified data from the local storage to the server database and vice-versa;
  • In the server, data is updated in a “last write wins” strategy while ensuring the relationship between the records of the master and detail entities. With this strategy, the latest data update overrides earlier updates.

The following is an overview of the Read/Write Data One-to-Many pattern:

  1.  Sends local storage data modified by the app back to the server;
  2.  Updates database data with the one sent from local storage, taking the necessary steps to maintain the relationship between the records of the master and detail entities;
  3.  Sends updated database data;
  4.  Deletes and recreates data in the local storage with the data received from the server.

Download the sample module for the Read/Write Data One-to-Many pattern, that uses companies that have multiple contacts as an example of data to synchronize. The following sections provide detailed descriptions of the data model and logic used in the sample module.

Data Model

This sample defines two related database entities Company and Contact and their local storage counterparts LocalCompany and LocalContact. Additionally, the LocalCompany and LocalContact entities define three metadata attributes to keep track of the synchronization status of the records.

Read-Write Data One-to-Many Data Model

The application logic must update the metadata attributes IsFromServer, IsModified and IsActive of the local entities according to the following:

  • IsFromServer: if True, the record exists on the server;
  • IsModified: if True, the record has been modified locally;
  • IsActive: if False, the record was deleted locally but may not yet have been removed from the server.

OfflineDataSync Logic

The following is a description of the logic of the OfflineDataSync client action:

Read-Write Data One-to-Many Pattern OfflineDataSync

  1. Obtains the list of locally added, updated, and deleted Company records;
  2. Obtains the list of locally added, updated, and deleted Contact records;
  3. Calls the ServerDataSync server action with the lists of locally added, updated, and deleted Company and Contact records as inputs. The server updates the data in the database and returns the list of updated Company and Contact records;
  4. Deletes all Company records in the local storage and recreates the Company records in the local storage using the list of records returned by the server;
  5. Deletes all Contact records in the local storage and recreates the Contact records in the local storage using the list of records returned by the server.

ServerDataSync Logic

The following is a description of the logic of the ServerDataSync server action:

Read-Write Data One-to-Many Pattern ServerDataSync

  1. Calls the SyncCompanies server action with the lists of locally added, updated, and deleted Company records as inputs. The action updates the data in the database and returns the list of updated Company records, as well as a dictionary containing the mappings between the local and database IDs of the Company records;
  2. Iterates the list of locally added Contact records and uses the mappings in the dictionary to update the attribute LocalContact.CompanyId to the ID of the corresponding Company record created on the database. This is to ensure that the one-to-many relationship of the records is maintained on the server database;
  3. Calls the SyncContacts server action with the lists of locally added, updated, and deleted Contact records as inputs. The action updates the data in the database and returns the list of updated Contact records;
  4. Assigns the lists of updated Company and Contact records to the output parameters of the action.

SyncCompanies Logic

The following is a description of the logic of the SyncCompanies server action:

Read-Write Data One-to-Many Pattern SyncCompanies

  1. Iterates the list of locally added Company records and creates the new records in the database;
  2. Iterates the list of locally added Contact records, creates the new records in the database, and creates a mapping between the ID of the local record and the ID of the newly created database record in a dictionary;
  3. Iterates the list of locally modified Company records and updates the records in the database;
  4. Iterates the list of locally deleted (inactive) Company records and deletes the records in the database;
  5. Retrieves all the Company records from the database;
  6. Assigns the dictionary containing the mappings between the local and database IDs of the Company records and the list of Company records to output parameters of the action, while setting appropriate values for the synchronization metadata attributes:

    IsFromServer = True
    IsModified = False
    IsActive = True
    

SyncContacts Logic

The following is a description of the logic of the SyncContacts server action:

Read-Write Data One-to-Many Pattern SyncContacts

  1. Iterates the list of locally added Contact records and creates the new records in the database;
  2. Iterates the list of locally modified Contact records and updates the records in the database;
  3. Iterates the list of locally deleted (inactive) Contact records and deletes the records in the database;
  4. Retrieves all the Contact records from the database;
  5. Assigns the list of Contact records to the output parameter of the action, while setting appropriate values for the synchronization metadata attributes:

    IsFromServer = True
    IsModified = False
    IsActive = True
    
  • Was this article helpful?