Android Question CloudKVS: waiting for events from another module?

Dave O

Well-Known Member
Licensed User
Longtime User
Hi all, I'm using CloudKVS and trying to figure out a wait-for-data situation using events and resumable subs.

I define my cloudKVS instance in a common code module called "c" (which I try to use for all global variables).

I create the instance in my Starter module:
B4X:
c.ckvs.Initialize(Me, "ckvs", c.SYNC_URL, c.DB_FILENAME)

And I have my NewData event handler for RefreshUser in the Starter module too:
B4X:
Sub ckvs_NewData
    ...
End Sub

In another activity, I'd like to call RefreshUser and wait for the results before continuing, so I wrote this:
B4X:
c.ckvs.RefreshUser(c.userEmail)
wait for ckvs_NewData
'code continues here once the event happens

(For now, let's assume that there are always updates that fire the NewData event.)

The problem so far is that the original event handler in my Starter module fires, but the one in the other activity doesn't. I thought it would because its inline event handler should take precedence over the Starter one, wouldn't it?

Anyway, I'm relatively new to async/resumable subs and events, so happy to be corrected. Thanks! :)
 

Dave O

Well-Known Member
Licensed User
Longtime User
1. All of this will become trivial if you switch to B4XPages. No need to use the starter service and no need to delegate events.
There are a couple of reasons why I can't switch to B4XPages, so it's just pure B4A for the near future.

2. Don't use code modules as they don't support events and resumable subs. Use a class instead.
I find code modules handy for common code and variables. Would using a class for the KVS instance make a difference to this problem?

Thanks again!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I can't switch to B4XPages, so it's just pure B4A for the near future.
There nothing more or less "pure" in B4XPages.

I find code modules handy for common code and variables. Would using a class for the KVS instance make a difference to this problem?
If you want to call a sub with CallSub or CallSubDelayed then you need to use a class instance.
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
When the RefreshUser callback fires, it's coded (in CloudKVS) like this:
B4X:
CallSub(mCallback, mEventName & "_newdata")

...which means that it's wired up to the ckvs_NewData handler in my Starter module. That does fire. And I thought it could also fire in an activity module because I already use CallSub elsewhere to call from a service to an activity.

I hoped that my Wait For code above in a different activity would let me run RefreshUser and capture the result in that same code block. Is this possible and I'm just messing up the details, or it's not possible and I need to take a different approach?
 
Last edited:
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
I got the Wait For working when everything was in the Main module - CKVS variable declared and instantiated, and the NewData event handler added. The Wait For preempted the event handler as designed.

However, when I moved the CKVS variable declaration, instantiation, and event handler to the Starter module, then tried to call it with an inline Wait For in the Main module, it ignored the Wait For and fired the event handler in Starter.

Unless I'm doing something wrong, it seems that if I want to call RefreshUser in several different activities in my app, I'll need to use a single event handler (in Starter) that can dispatch back to the calling activity (probably via a global variable that I set just before the RefreshUser).

I'm happy to hear about better solutions that anyone can offer. Thanks!
 
Upvote 0
Top