Java Question Create java.util.Map<String, Object> - how to do it from b4a

DonManfred

Expert
Licensed User
Longtime User
After i found out how to get the right jar to import i started to do a wrap for the upcoming Firebase Firestore Database.

The DB-Structure is based on Documents which represents a
B4X:
java.util.Map<String, Object>
or
B4X:
java.util.Map<String, Long>

What would be the best way to handle this kind of data?

I mean; should i create a helper method to go over a b4a map (i mean in java) which creates a
B4X:
java.util.Map<String, Object>
from the b4a Map given?
is there a easy/simple way to do it in java?

Based on my experience with the "old" realtimedatabase i am thinking that it would be easier if we have a javaclass which represents a database-entry (or in Firestore a document)...



This is the list of allowed Fieldtypes which are all referenced by a name in the java Map

https://firebase.google.com/docs/firestore/manage-data/data-types

What would be the best solution to support these types in a single Object.
I mean what would be the best way to work with the data?

I´m not sure that i fully understand how i would handle this...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use these two java methods to convert between B4A map and Java map:
B4X:
@SuppressWarnings("unchecked")
     java.util.Map<String, Object> B4AMapToJMap(anywheresoftware.b4a.objects.collections.Map map) {
       return (java.util.Map)map.getObject();
     }
     anywheresoftware.b4a.objects.collections.Map JMapToB4AMap(java.util.Map<String, Object> map) {
       anywheresoftware.b4a.objects.collections.Map m = new anywheresoftware.b4a.objects.collections.Map();
       m.Initialize();
       m.getObject().putAll(map);
       return m;
     }

Based on my experience with the "old" realtimedatabase i am thinking that it would be easier if we have a javaclass which represents a database-entry (or in Firestore a document)...
I'm not sure that it is a good idea to add another class. Maps are very useful, especially with the CreateMap keyword.
 

DonManfred

Expert
Licensed User
Longtime User
You can use these two java methods to convert between B4A map and Java map
Cool. Much easier than i would have done it :)

Thank you!

I'm not sure that it is a good idea to add another class. Maps are very useful, especially with the CreateMap keyword.
I was thinking about the use in B4A later (for the developer). So the user could use it in B4A like using a custom Type....

User.FirstName, User.SurName, User.Picture or whatever.

But based on the two methods you provided i think i should go this way. Using maps to store all Infos needed and convert the java map back to b4a Map when reading the value from the Database...

Thank you for you comments! I really appreciate
 
Top