Background Synchronisation Process

Contents Hide


Background synchronisation allows mobile applications to send data to the server in the background while working or processing another task. This eliminates the requirement for users to explicitly initiate the synchronisation process. The background synchronisation can be triggered by several events such as user click events, closing or opening of a form, changing record states (e.g. job status changes) or on data pull from BrightServer™.

This will improve the usability of the mobile applications because the users does not need to care about data synchronisation issues.

Enabling Background Synchronisation

With your Bright Software mobile application, you simply have to set the Enable Background Synchroniser and optionally the Enable Push Listener project properties to true, if the application is expected to receive unsolicited push messages from back-end.

As the name implies, the Enable Background Synchroniser property allows your application to send and receive data to and from server in the background, without the awareness of the field users. Whereas the Enable Push Listener property lets BrightServer™ to send data to the remote client devices.

If the application's background synchroniser is enabled, on startup, BrightForms™ starts a background worker process to handle background synchronisation. Otherwise the background synchroniser will not be started and the background synchronisation will not occur.

The Enable Push Listener property is only required if BrightServer™ is sending unsolicited data from back-end systems to the remote devices in field with a known (static) IP addresses. For more information on how to use the push listener in scripts, please refer to the Script Methods Reference > Examples > Push Examples and BrightServer > Scripts sections of this document.

Configuring Sync Rules for Background Synchronisation

Sync rules are generally used by the mobile applications in foreground to send or receive data to and from server. This can be initiated by a user event or an application event such as form timer driven expressions. Sync rules can also be marked so that they are used by the background synchroniser. For the standard foreground synchronisation engine to send and receive data, at least one or more sync rules must be enabled.

Similarly for the background synchroniser to execute background synchronisation :

 

Enabling sync rules are achieved by using the Synchroniser.EnableSyncRule method. To mark a sync rule as a background sync rule, you must also use the Synchroniser.MakeBackgroundSyncRule method.

As with standard sync rules, any active sync rules will be set to disabled on successful synchronisation. Else, they will continue to be active until they are executed via a background or foreground sync.

Sync rules which have been marked as a background sync rule may still be called and executed as a regular sync rule. While records may have transferred correctly, please be aware of this if the business logic of the project is dependant on a background sync rather than a foreground one.

How to Run Background Synchronisation

Enabling a sync rule and making it as a background sync rule is itself not sufficient. You need to instruct the background synchroniser to execute enabled background sync rules. This is done by using Synchroniser.SynchroniseInBackground() method.

A sample code to execute a background sync would look like as follows.

..

Synchroniser.EnableSyncRule("SyncRuleSendData", true);

Synchroniser.MakeBackgroundSyncRule("SyncRuleSendData", true);

Synchroniser.SynchroniseInBackground()

..

As mentioned previously, any active sync rules and/or flag to synchronise in the background will remain active until they are run successfully, or until BrightForms is closed. Therefore, sync rules may be activated immediately after data has been finalised on the device, queued such that the captured data may reach the server at the quickest possible time.

Any records to be synchronised will be done so when the background sync is called - potentially causing an issue if the database object is used when automatic deletion for the table is activated. As the user is not in control when they want to sync, it is important to make sure that only completed records are being sent when synchronising in the background.

How Does The Background Synchroniser Work?

Usually, your mobile application will only have one process thread. A thread is a single sequential flow of control within a program, it has a beginning, an execution sequence and an end. At any given time during the runtime of the thread, there is a single point of execution. The following figure shows this relationship.

In BrightForms™, the Background Synchronisation process is a separate thread running at the same time as that of the main thread which is your mobile application business logic. This is illustrated in the next figure, it runs within the context of a full-blown mobile application and takes advantage of the resources allocated for that application and the applications environment.

The diagram below describes the background synchronisation process:

Idle Time : This is the time that background sync process sleeps and wait to let other processes use the CPU.

Retry Time : This is the time that the background sync waits before retrying to connect to the server again after an error. Any pending calls to synchronise in the background, and any active background sync rules will be executed after the retry.

In simple terms BrightForms™ will check if there is a background sync request. If there is a request, then it checks if there are any sync rules that are enabled and marked as a background sync rule. If there are enabled background sync rules, BrightForms™ will execute them. Any form that is currently open in the application will register automatically with the background synchroniser for the background sync notification events. After executing the sync rules, the background synchroniser will inform the registered forms that a background execution took place. If the result is successful, then it will sleep (be inactive) for the idle time. If there is an error however, it will sleep for the retry time. Retry and idle times can be controlled separately via Synchroniser object methods.

If there is no request and or no enable background sync rules in the system, BrightForms™ will then simply will sleep for the idle time.

It is important to note that on a successful background synchronisation, the request counter will be set to 0. That means no matter what value it had, it will be reset. This mean the background synchronisation will not occur again until a new background sync request is made via the Synchroniser.SynchroniseInBackground() method.

Background Sync Notification Events

As of BrightForms™ 6.1, forms with actions defined for background sync (and also for push) events will receive automatic notification calls from the background synchroniser. In these call back expressions, the application programmer can check the result of the execution, and reactivate the background sync as required by the business and application rules.

This expression will be called each time the background synchronisation thread wakes, regardless of if whether server have been touched or data was actually synchronised or not.

Furthermore, if Synchroniser.SynchroniseInBackground() is called and a synchronisation successfully occurs, query based controls for the affected table such as listviews, combo boxes or any which are on forms with databinding defined will automatically be refreshed by BrightForms.

Synchroniser Object

Expression Synchroniser object comes with a set of methods to initiate, execute and check the background sync execution results.

Refer to the Objects Reference manual for examples of Background synchronisation code.