How to get value from inside closure to set to a local variable

I have been trying fetch data from firebase and storing it in a dataModel using completion handler like this static var dataModels = [DataModel]() static func fetchData(completion: @escaping () -> Void) { let ref = Database.database().reference().child("data") ref.observe( .value, with: { snapshot in guard let value = snapshot.value as? [[String: Any]] else { return } for… Read More How to get value from inside closure to set to a local variable

How to add an .index in Realtime Database when the folders are not constant?

Whenever I am trying to query some data in order to delete a folder in Realtime Database, I get the following warning: Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "id" at /comments/evChargers/D6A3D94D-1F2F-467B-A6C5-F6D9BF7D1C02/AKsxbJAifSbsIhFONMSFT6UTarS2 to your security rules for better performance However, the values D6A3D94D-1F2F-467B-A6C5-F6D9BF7D1C02 and AKsxbJAifSbsIhFONMSFT6UTarS2 are not constant and… Read More How to add an .index in Realtime Database when the folders are not constant?

Iterating over children of a DataSnapshot throws exception

When I try to iterate over the children in a DataSnapshot it throws a RangeError. This is the code that I have: Map<String, Object> mapOfMaps(DataSnapshot snapshot) { Map<String, Object> map = <String, Object>{}; for (var child in snapshot.children) { if (child.children.isNotEmpty) { map[child.key!] = mapOfMaps(child); } else { map[child.key!] = child.value!; } } return map;… Read More Iterating over children of a DataSnapshot throws exception

How can I query Firebase Realtime Database by value when it is only a list of key:values?

I have a few collections in a Firebase Realtime Database that are structured like this:: SomeCollectionName { randomKey1: userId1, randomKey2: userId2, randomKey3: userId3, randomKey4: userId1, randomKey5: userId1 } I want to query SomeCollectionName where the values are equal to userId1, but am having a hard time figuring out how to get those values without knowing… Read More How can I query Firebase Realtime Database by value when it is only a list of key:values?

how to stop slash from splitting the firebase database child

I am working on an App that allows the student to register using their registration number. the registration number is like 17/csc/001 till infinity. The student registration number will saved as a child of that firebase reference but the issue I am having is that firebase splits the registration number into three places due to… Read More how to stop slash from splitting the firebase database child

Failed to convert value of type java.lang.Long to String – Android Studio – Firebase Realtime Database

I’m coding a Tic Tac Toe, and while executing it crashes when I tried to read a value from the database. I’m getting the following error: com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String The line where it crashes is the following : canPlay = Long.parseLong(snapshot.getValue(String.class)); The function is the following: refPeutJouer.addValueEventListener(new ValueEventListener()… Read More Failed to convert value of type java.lang.Long to String – Android Studio – Firebase Realtime Database

All my firebase field get automatically an underscore on front

My PoIs class: public class PoIs { private Integer location_id; private String location_name; private String location_address; public PoIs() {} public PoIs(Integer location_id, String location_name, String location_address) { this(); this.location_id = location_id; this.category_id = category_id; this.location_name = location_name; this.location_address = location_address; } public Integer get_location_id() { return location_id; } public void set_location_id(Integer location_id) { this.location_id =… Read More All my firebase field get automatically an underscore on front

Get value from list of objects Firebase Android Studio

I have the following code: public void signOn(String id, String password){ usersReference.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { boolean valid = false; for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){ String key = singleSnapshot.getKey(); if (key.equals(id)) { singleSnapshot.getValue()//THIS LINE THIS LINE Intent myIntent = new Intent(WelcomePage.this, MainActivity.class); myIntent.putExtra("key", key); WelcomePage.this.startActivity(myIntent); } } } @Override public void onCancelled(DatabaseError… Read More Get value from list of objects Firebase Android Studio