Android Tutorial [B4X] CloudKVS - synchronized key / value store

miquelr

Member
Licensed User
Longtime User

Hi Erel

In the Activity_Pause of the Main module, it had code to update a remote SQL database when the program was closed:

B4X:
Activity_Pause sub (UserClosed As Boolean)
   Dim ct As String ="UPDATE usuaris set idLastDisp ='" & deviceId & "', idCurrentDisp = 'ninguno1' WHERE GuidId = '" & idUser &"'"
   job5.Initialize("Job5", Me)
  job5.Download2(sqlSvr, Array As String("query",".....

It is obvious that the code is not correct in Activity_Pause.
Removing this code solves the problem.

Thanks for you fast support and congratulations for the outstanding work of your team, and the contribution of the most of members of the forum.

( I beg your pardon for my googlenglish )
 

luke2012

Well-Known Member
Licensed User
Longtime User
Hi to all. What is the path of local store ?
 
Last edited:

luke2012

Well-Known Member
Licensed User
Longtime User
Can I use a PI 3 as CloudKVS Server ?
I can run the compiled Server jar as it is on the PI 3 ?

Thanks in advance for the reply.
 

jareal

Member
Licensed User
Longtime User
Hi,

I have App1 that uses CloudKVS and has an average of 4 PUTs per hour.
Then developed App2, totally independent of App1, that also uses CloudKVS and has about 10 PUTs per hour.
Both apps communicate with the same Server.

Both apps use the original unchanged ClientKVS module. So, both use 'db.db' as local store.

When I installed the two apps on the same device, both continued to work without a problem.

But I am wondering if my approach is correct... or I will have problems when the update rate of the apps increase...
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The local store name is not important as it is stored in the app's internal folder.

You can use the same CloudKVS server to support multiple applications. However you need to make sure that the <user, key> tuple is unique or data will be synchronized between the apps.

CloudKVS can support much higher rates (many puts per second).
 

Dey

Active Member
Licensed User
Longtime User
Hello to all,
They can asynchronous CloudKVS with a lot of data
Thank you
 

miquelr

Member
Licensed User
Longtime User
Hi,
On a computer in the office I tried the example of CloudKVS smoothly. Now I'm trying on another computer in my house and I can not do it.
During compilation I get this message:
***
B4A version: 6.30
Parsing code. (0.01s)
Compiling code. (1.32s)
Compiling code layouts. (0.00s)
Organizing libraries. (0.00s)
Generating R file. (0.11s)
Compiling code debugger engine. (1.21s)
Compiling Java code generated. Error
javac 1.8.0_111
src \ b4a \ example \ starter.java: 84: error: can not find symbol
super.onTaskRemoved (rootIntent);
^
symbol: method onTaskRemoved (Intent)
Note: src \ b4a \ example \ clientkvs.java use unchecked or unsafe operations.
Note: Recompile With -Xlint: unchecked for details.
1 error
*****
First I tried with jdk1.7.0_45 and then width jdk1.8.0_111 getting the same error.
What I can do?
 

luke2012

Well-Known Member
Licensed User
Longtime User
Hi to all,
My problem is that each time the app is started, I check for a specific item and I create it if not found (in local store).

But when a user remove the app all the related local data is removed (allitems = 0).
So the item is present on the server store but not in the local store and my program can't find it and create a duplicated item (thinking at my app logic).

See the details...

B4X:
Sub Service_Create

       'This code run just after the app is removed

       ckvs.Initialize(Me, "ckvs", Main.ServerUrl)
       Private allitems As Map = ckvs.GetAll ("user1")

       'here I check for the item but the local store contains nothing (it is on the server store in this moment)...
end sub

So in this case the local store isn't in sync with the server store and my program cannot find this item (that already exist within the cloud) and it create a new item (but it doesn't) within the local store and than it is replicated to server (in my app logic it is an item duplication).
So the final result is that I have two items with the same data within the cloud.

So the point is that I don't known how to force a local store sync (just after ckvs.Initialize) within the Service_Create (Starter) to avoid this problem.
Note that this problem exist only when a user remove and reinstall the app (cKVS client).

Thanks in advance for your reply.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is exactly the purpose of GetDefaultAndPut.

It gets the current value. If the value doesn't exist then it will return the default value. The default value will be synced with the server. The server will first check whether there is an existing item. If there is one then it will not update the value.
 

luke2012

Well-Known Member
Licensed User
Longtime User

So you are suggesting me to use the GetDefaultAndPut instead of Put to avoid this situation.
 

luke2012

Well-Known Member
Licensed User
Longtime User
Not exactly. GetDefaultAndPut should only be used for default values.

Your example of GetDefaultAndPut:
"If there is a User1/Score value in the local store then it will be returned. Otherwise it will return 0 and also put 0 in the store."

In my case the item (value) is a type and I check (within the Starter/Service_Create) it using a field within the type (user gmail).
So if there is an item with the current user GMAIL it doesn't create the item. If this item isn't found (item with a specific gmail within the type) the item will be created.

Each user item (with current user GMAIL) is generated using large number key like bbb80701-bebe-4caa-a0d2-5678220939b4 (see your post #31).
So the problem is that (in the scenario described above) I don't have this data in the local store (only on the server) when the local store is purged (app removed).

So the point is that within the local store (cloudKVS client) I don't have the big number key and all related data (I known only the user gmail from the AccountManager).
So the final result is that my program within the sarter don't find the user item with the current user gmail (because in that moment is only on the server store) and create a new (duplicated item with the same data) with a differrent large number ID (key) like bbb80701-bebe-4caa-a0d2-5678220939b5.

So I have data redundancy and two different unique key related to the same user.
 
Last edited:

luke2012

Well-Known Member
Licensed User
Longtime User
You can use GetDefaultAndPut with the new GUID (large number key).
If it returns the new GUID then you need to call RefreshUser and wait for NewData event. Get the GUID again in NewData event. It will be the correct one.

Please Erel can you show me a example how to check it within the NewData event ?
 

luke2012

Well-Known Member
Licensed User
Longtime User

thanks Erel. Do you confirm that each time I call the RefreshUser the ckvs_NewData event will be raised ?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…