cancel
Showing results for 
Search instead for 
Did you mean: 

Offline MDK app: automatic synchronization

mariondupin
Explorer
0 Kudos

Hello,

I follow this good tutorial: https://developers.sap.com/mission.mobile-dev-kit-level-up.html

I have done my first offline app, with Business Application Studio and the MDK, connected to my SAP system, everything works fine.

I have a button to launch the synchronization: I refresh my app and upload changes to the back-end when I click on the sync Button.

I would like to do this synchronization automatically every 10 minutes. Could you explain how I could do that?

Thanks for your help.

Best regards,
Marion

Accepted Solutions (1)

Accepted Solutions (1)

FranciscoSAP
Advisor
Advisor

Hi mariondupin,

Intuitively I'd say that having automatic synchronizations would defeat the purpose of having an offline app, but if there's a specific requirement in your project for which you absolutely need to do it, I'd take a look at how tasks are scheduled in javaScript and write a rule to do so.

A simple way to do it would be using the setInterval() method to write a function that triggers your MDK Sync Action every 10 minutes. Although I'm not sure about how this would behave in runtime and if the function would still be triggered if the user navigates through different pages in your app, you could quickly test it by writing a simple rule using setInterval() and setting it to the OnLoaded event of your main page.

mariondupin
Explorer
0 Kudos

Hi Fransisco,

Thanks for your help.

I do an offline app because for some outside users for my customer, the network is not good. So the aim is to automatically sync if there is network, and if not, use offline capabilities. It's not really user friendly to have to sync the app manually.

It's the same logic explained here I would like to use: https://help.sap.com/viewer/42dc90f1e1ed45d9aafad60c80646d10/3.0.16/en-US/59ae11dc4df345bc8073f9da45...

But in the MDK, I don't understand how to trigger a javaScript rule for running in background: where do you think I could call my javaScript rule?

Thanks,
Marion

FranciscoSAP
Advisor
Advisor
0 Kudos

Sorry mariondupin, I edited my original comment to clarify that part. You could either set your setInterval() rule to run on the "onSuccess" event of your initializeOffline action, or you could assign it to the "OnLoaded" event of your Main page. But, like I said, I'm not entirely sure about how this would behave during runtime in a MDK app, you could quickly test it by writing said rule and navigating normally through your app like any user would do and see what happens.

FranciscoSAP
Advisor
Advisor
0 Kudos

If this doesn't work you could have a look at this: https://nativescript.org/blog/using-workers-in-nativescript-to-execute-javascript-on-background-thre...

I have never used it myself because I'm also fairly new to native App development, but it seems like it might do the trick for what you are trying to achieve. There are also several npm modules that I have used in the past to schedule tasks but I suspect that getting them to run in a nativeScript environment might be difficult.

mariondupin
Explorer

Hello Francisco,

I add a rule called in the "OnLoaded" event of your Main page, with:

setInterval(function() { clientAPI.executeAction('/MyApp/Actions/Service/UploadOffline.action'); }, 600000);

It works!

Thanks for your help.

Best regards,
Marion

Answers (1)

Answers (1)

fjcarrasco
Active Participant
0 Kudos

Hi. In case it's not possible do it automatically, I would create a js rule to run the sync action and I would set this rule on success of each CRUD option in my app. In this rule you could check if the last sync was longer than 10 minutes, saving the time when you trigger the syncronization on context page.