These B4X modules will handle the synchronization of key/value databases between multiple devices and between platforms!
So one application runs on a server widely available on the internet such as a low cost VPS that can run java applications.
Then each application have to add simple code to get synchronized variables.
ckvs.Put("User1", "Key1", 100)
Log(ckvs.Get("User1", "Key1")) '100
Log(ckvs.GetDefault("User2", "Key1", 0)) '0 because User2/Key1 is different than User1/Key1 |
ckvs.Put("User1", "Key1", 100)
Log(ckvs.Get("User1", "Key1")) '100
Log(ckvs.GetDefault("User2", "Key1", 0)) '0 because User2/Key1 is different than User1/Key1
The SetAutoRefresh method sets the user(s) that will be fetched from the remote store.
For example if we want to auto-synchronize the data of “User1”:
ckvs.SetAutoRefresh(Array("User1"), 5) |
ckvs.SetAutoRefresh(Array("User1"), 5)
Sample Code:
'Code module
#Region Project Attributes
#ApplicationLabel: B4i Example
#Version: 1.0.0
'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
#iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
#iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
#Target: iPhone, iPad
#MinVersion: 7
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public App As Application
Public NavControl As NavigationController
Private Page1 As Page
Type Item (UserField As String, KeyField As String, ValueField() As Byte, IdField As Long, TimeField As Long)
Type Task (TaskName As String, TaskItem As Item)
Private ckvs As ClientKVS
Private TextField1 As TextField
Private User As String = "u1"
End Sub
Private Sub Application_Start (Nav As NavigationController)
NavControl = Nav
Page1.Initialize("Page1")
Page1.RootPanel.LoadLayout("1")
NavControl.ShowPage(Page1)
ckvs.Initialize(Me, "ckvs", "http://xxx.nopapers.org:12345")
ckvs.SetAutoRefresh(Array(User), 0.1) 'auto refresh every 0.1 minutes
TextField1.Text = ckvs.GetDefaultAndPut(User, "number", 0)
End Sub
Private Sub ckvs_NewData
TextField1.Text = ckvs.Get(User, "number")
End Sub
Sub btnSet_Click
Dim number As Int = TextField1.Text
ckvs.Put(User, "number", number)
Page1.ResignFocus
End Sub
Private Sub Page1_Resize(Width As Int, Height As Int)
End Sub
Private Sub Application_Background
End Sub |
'Code module
#Region Project Attributes
#ApplicationLabel: B4i Example
#Version: 1.0.0
'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
#iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
#iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
#Target: iPhone, iPad
#MinVersion: 7
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public App As Application
Public NavControl As NavigationController
Private Page1 As Page
Type Item (UserField As String, KeyField As String, ValueField() As Byte, IdField As Long, TimeField As Long)
Type Task (TaskName As String, TaskItem As Item)
Private ckvs As ClientKVS
Private TextField1 As TextField
Private User As String = "u1"
End Sub
Private Sub Application_Start (Nav As NavigationController)
NavControl = Nav
Page1.Initialize("Page1")
Page1.RootPanel.LoadLayout("1")
NavControl.ShowPage(Page1)
ckvs.Initialize(Me, "ckvs", "http://xxx.nopapers.org:12345")
ckvs.SetAutoRefresh(Array(User), 0.1) 'auto refresh every 0.1 minutes
TextField1.Text = ckvs.GetDefaultAndPut(User, "number", 0)
End Sub
Private Sub ckvs_NewData
TextField1.Text = ckvs.Get(User, "number")
End Sub
Sub btnSet_Click
Dim number As Int = TextField1.Text
ckvs.Put(User, "number", number)
Page1.ResignFocus
End Sub
Private Sub Page1_Resize(Width As Int, Height As Int)
End Sub
Private Sub Application_Background
End Sub
B4A_ClientKVS
B4i_ClientKVS
B4J_ClientKVS
B4J_ServerKVS
jBuilderUtils
Continue reading “Synchronize Key/Value Database between devices”