B4J Question [BANano] [SOLVED] Is this the right way to use BANanoFetch inside a loop? Subscribing to multiple firebase topics...

Mashiane

Expert
Licensed User
Longtime User
Ola

I'm attempting to subscribe to a list of topics in firebase. This should happen when the user logs in before the dashboard is shown.

B4X:
'we need to subscribe to each of the topics
    Dim mytopics As List = Array As String("banano", sdeviceid, stel1, stel2, stopicarea)
    '
    For Each strtopic As String In mytopics
        If strtopic = "" Then Continue
        'subscribe using fetch
        Dim response As BANanoFetchResponse
        Dim error As BANanoObject
        'subscribe to each topic
        Dim fetch As BANanoFetch = firebase.messaging.subscribe(strtopic)
        banano.Await(fetch)
        fetch.Then(response)
        Dim Done As Boolean = firebase.messaging.IsSubscribed(response)
        banano.Console.Log($"Subscribe to topic: ${strtopic}, ${Done}"$)
        fetch.Else(error)
        banano.Console.Log($"Could not subscribe to topic: ${strtopic}"$)
        fetch.End
        Sleep(1000)
    Next

This works however I'm not sure whether calling banano.await(fetch) is proper in this case. Sleep(1000) of course seems to be necessary.
Yes, I want to ensure that the subscriptions happen "before" the login screen is hidden.

Thanks,
 

alwaysbusy

Expert
Licensed User
Longtime User
This looks like a perfect case for PromiseAll. As I have no idea what messaging.subscribe() does, I do it here with normal fetches.

B4X:
    Dim myTopics As List
    myTopics.Initialize
    myTopics.Add("./assets/favicon-16x16.png")
    myTopics.Add("./assets/doesnotexist.file") ' to force a fail for this example
    myTopics.Add("./assets/favicon-32x32.png")
   
    Dim Fetches As List
    Fetches.Initialize
   
    ' add all the fetches
    For Each strtopic As String In myTopics
        Dim Fetch As BANanoFetch
        Fetch.Initialize(strtopic, Null)
        Fetches.Add(Fetch)       
    Next
   
    Dim ResultsAll() As BANanoFetchResponse
   
    ' do all the fetches and wait until they are all done
    ResultsAll = BANano.Await(BANano.PromiseAll(Fetches))
    ' expected output: ok, not ok, ok
    For i = 0 To ResultsAll.Length - 1
        If ResultsAll(i).OK Then
            Log($"Subscribed to topic: ${myTopics.Get(i)}"$)
        Else
            Log($"Could not subscribe to topic: ${myTopics.Get(i)}"$)
        End If
    Next
   
    Log("And Now This...")

Output:
B4X:
GET http://127.0.0.1:8887/assets/doesnotexist.file 404 (Not Found)
Subscribed to topic: ./assets/favicon-16x16.png
Could not subscribe to topic: ./assets/doesnotexist.file
Subscribed to topic: ./assets/favicon-32x32.png
And Now This...

More info on these methods:
https://www.b4x.com/android/forum/t...-abstract-designer-support.101551/post-691184 (*)

(*) This topic is a good resource to find such examples, but maybe I will have to find another way to bundle them so they are better searchable.

Alwaysbusy
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Great stuff, this is very easy to implement than my loop and its faster also.

Anyway, in the result below

firebase is my library class instance, this references a child class named messaging so that all messaging subs runs from their separate concern.

This is defined as

B4X:
Public messaging As FBMessaging

and I initialize it from a sub called Connect with

B4X:
messaging.Initialize(firebase, vapidKey, ServerKey)

So in my messaging class, I have added this sub

B4X:
Sub subscribeToTopicsWait(topics As List) As Boolean
    Dim fetches As List
    fetches.Initialize
    For Each strtopic As String In topics
        Dim fetch As BANanoFetch = subscribe(strtopic)
        fetches.Add(fetch)
    Next
    Dim ResultsAll() As BANanoFetchResponse        'ignore
    ResultsAll = BANAno.Await(BANAno.PromiseAll(fetches))
    Return True
End Sub

When I run

B4X:
Dim Done As Boolean = banano.Await(firebase.messaging.subscribeToTopicsWait(mytopics))
    Log(Done)

This results in this error

B4X:
// [446]  Dim mytopics As List = Array As String( {242} , sdeviceid, stel1, stel2, stopicarea)
_mytopics=["topic",_sdeviceid,_stel1,_stel2,_stopicarea];
// [451]  Dim Done As Boolean = banano.Await(firebase.messaging.subscribeToTopicsWait(mytopics))
_done=await _B._firebase.await _messaging.subscribetotopicswait(_mytopics);
// [452]  Log(Done)
console.log(_done);

If however I take the subscribeToTopicsWait sub and put it on the root of my library class it works perfectly. This is when I call it with

B4X:
Dim Done As Boolean = banano.Await(firebase.subscribeToTopicsWait(mytopics))
    Log(Done)

I also update it for the root class to reference the messaging class like this.

B4X:
Sub subscribeToTopicsWait(topics As List) As Boolean
    Dim fetches As List
    fetches.Initialize
    For Each strtopic As String In topics
        Dim fetch As BANanoFetch = messaging.subscribe(strtopic)
        fetches.Add(fetch)
    Next
    Dim ResultsAll() As BANanoFetchResponse        'ignore
    ResultsAll = BANano.Await(BANano.PromiseAll(fetches))
    Return True
End Sub

Is there a way to fix this as I would not want to bloat the root library class? or perhaps call it differently so that it does not break and result in this?

B4X:
_done=await _B._firebase.await _messaging.subscribetotopicswait(_mytopics);

Thanks
 
Last edited:
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Make a small project
I have to ensure that I always do this, I'm sorry.

I have created two projects, one is the library and the other is a test project. Both dont refer to any css or js and both dont do much except just demonstrate this.

Basically, its the second part that does not work and the one that i'd rather prefer to work

B4X:
'This code works
    Dim mytopics As List = Array As String("topic1", "topic2", "topic3")
    Dim Done1 As Boolean = BANano.Await(firebase.subscribeToTopicsWait(mytopics))
    Log(Done1)

    'This code does not work
    Dim Done As Boolean = BANano.Await(firebase.messaging.subscribeToTopicsWait(mytopics))
    Log(Done)

Thanks for your time, appreciated.
 

Attachments

  • BANanoFireBase.zip
    1.9 KB · Views: 179
  • TestProject.zip
    1.6 KB · Views: 166
Upvote 0
Top