Using Firebase Database - android

I am using firebase as backend for storage and authentication for the android app. I have created the authentication part. So, far it works well. The problem comes in database part.
My question here is:
I want to store list of names and address of client in the firebase database. How can it be done (Considering I have just a text file with 2 columns)? Can this be automated or should it be done manually? i.e. By creating a Location child and in that writing Name with name of client and Value as address. As the list is high is number how can this be automated?
Once the list is stored on database, I want to distribute specific number of address to specific users only. How can this be done through firebase? Is there any way to distribute say "first 10 names and address to user1" and next 10 to user2" and so on?
Fetch the address and names from database to the users as list and "feed it to Geolocation for validation" and "obtaining latitude and longitude" and "further sending it to Maps" for navigation.
That's it. This is the part I am struggling with.
Can you suggest an alternative to implement this?

Here are the answers.
(i) If you can convert your text file into a JSON formatted file, then you can upload it very simple using your Firebase console -> Database -> Import JSON. The structure must be as a pair, of key and value.
(ii) Yes you can do it. You need to create a Map for names and a Map for address like this:
Map<String, Object> map = new HashMap<>();
map.put("name1", name1);
map.put("name2", name2);
........................
map.put("name10", name10);
yourRef.child(userId).child("names").updateChildren(map);
"name1", "name2" ... "name10" can be also changed with ids. You can use a method named push() which generates unique keys. Also userId is the id of the specific user for which you want to assign the names.
(iii) Yes, you can achieve that too. You can take a look at this post.
Hope it helps.

Related

How to give data access in cloud firestore to another added users in android?

I wanna ask about the concept and logically ways to give another user the privilege to access other's users' data. What I want to do exactly is like this :
Basically, collection 1 contains several Users ID (UID) from authentication, then the user will have their own data collected in collection 2 which contain the data ID.
So, it's like giving access to another user to collaborate with the data like Google Docs Apps where we can add another user to edit our documents. I've been thinking of how to do this, but still, I got stuck.
My question is, how can I possibly do this? cause from what I've read, cloud firestore don't use such a foreign key like MySQL. Thank You
haven't tried something like this but i think this approch overcomes your problem.
modify your structure according to above image. userID collection will contain userIds which are allowed to edit their parent collection.and create firestore rules according to your use to check weather the userId is allowed to edit the Collection or not.
in your case when 'user 2' will have reference to 'collection 2', he/she will try to change data. firebase rule will check if auth.userId is inside the 'collection2.UserIDs' or not and will allow according that.

Android - Firebase replaces data

I am developing an android application where older kids can pick up younger kids and walk to school. With the application, the authenticated (email and password) younger kid can choose between three addresses to get picked up. I have a user node and an address node.
The problem is that every time a user picks an address, the username child is overridden. Therefore there will only be one username per address.
My database structure looks like this:
database.child("Addresses").child(m1.getStreet()).child("username").setValue(name);
database.child("Addresses").child(m2.getStreet()).child("time").setValue(time);
database.child("Addresses").child(m3.getStreet()).child("address").setValue(address);
I thought about using the push function, but I don't think it can be used in this context. Any ideas?
setValue() will override everything in the existing map. Use updateChildren() if you want to add/update values and keep the old data.
If you have unique usernames or if you can get the keys in User, you can directly use that as a key:
database.child("Addresses").child(m1.getStreet()).child("users").child(name).child("username").setValue(name);
database.child("Addresses").child(m2.getStreet()).child("users").child(name).child("time").setValue(time);
database.child("Addresses").child(m3.getStreet()).child("users").child(name).child("address").setValue(address);

How do i differentiate between different level of users in firebaseAuth

I have two different types of users Teachers and Students . I use Firebase Auth with Email and password to Authenticate them and store them in the Firebase Real time database . My question is is there a way to create custom accesor methods such as getCurrentUser().getEmail, getDisplayname etc . I need to display different UI for different user type (Teacher/Student) from the current user-type
You got 2 type of users. So first of all, you can make only one firebase structure for both user and just add a boolean variable TEACHER that indicates if a user is a teacher or a student.
But what I usually do is to seperate the users in two different firebase structures meaning that you should create a firebase path teacher/user_id and an another student/user_id which will give you flexibility with retreiving data and display the data in different UI.

Is there any disadvantage of assigning numbers as keys to Firebase database?

Recently I've been working on a News App where I store information of news artciles in Firebase database. Following is an image of the structure of my Firebase database.
I read somewhere that it is not advisable to keep numbers as keys for Firebase database. However, I've not been able to figure out any explanation for this. Is there any norm that Firebase keys should not be numbers ?
If so, can I have key names as National_01, National_02, National_03... and so on for each news category?
TL;DR It is okay to use keys like "National_01" for static data and mini projects; Use Push Keys for dynamic data and big projects.
Here are some benefits of using Firebase Push Keys
Push Keys are automatically generated
If you continue to use manually created keys whenever you will have to update the data you have to do create a key like this:
String category = "National;"
count = count + 1;
String key = category + count.toString();
// Not say keep track of the "count" too
Instead, if you decide to use Firebase Auto-Generated Push Keys:
String key = mDatabase.child(category).push().getKey();
Nearly Impossible to Duplicate Push Keys
If you use self-generated keys there might me an error in creating keys hence creating a duplicate key. In that case, you might loose data since you overwrite data under the original key you duplicated.
Whereas, push ids are nearly impossible to duplicate and take no extra code to implement.
The Conclusion
So continue to use self-generated keys only and only if your data is static (which I don't think it should be coz it's a news app) and you don't wish to scale this static data (which again is unrealistic to imagine for a news app).
But remember it is PROGAMATICALLY EASY to work with Push Keys and is FUTURE PROOF in case you intend to Scale your app in future. :)
Edit
Moreover, the Push Keys generated are generated based on timestamp hence all the keys will be sorted chronologically by default!!
Using key names in your Firebase database is not a bad practice but according to your app complexity you can choose whenever to use those keys or the random generated keys.
Using the keys generated by the push() method, guarantees you the uniqueness, because the algorithm is based on a timestamp. Because of this, all your items will automatically be ordered chronologically.
If you want the users to post news in your app, because Firebase generates a unique key for each new news, no write conflicts will occur if multiple users add a post at the same time. This cannot be achieved with your key names in a very simple way.
Hope it helps.
For the record: I've read in the Firebase Best Practices documentation, that you shouldn't use custom keys for large projects, because that will disable the sharding method used in the background.
You can check further details at:
https://firebase.google.com/docs/firestore/best-practices

is it possible query data that are not equal to the specified condition?

Am fetching list of users for my listview (android) i used firebaselistadapter to backing up my listview , I wanna show users data except mine .
Like an sql query Select something from usertable where id!=userid;
i wanna fetch all other users data,
Firebase currently only offers a way to include nodes based on the presence of a certain value. You cannot exclude nodes based on the presence of a value.
Update (20160828): I wrote a related answer today that shows how to detect the absence of a property.
I have the same issue, using angularfire and geofire I am searching for business prospects within a radius and trying to create a lead only for new prospects, that means to exclude any existing account already with an existing relation. To do so I need to open a firebaseObject for each key returned by geofire and after $loaded() , check for _.isNull(loadedFirebaseObject.$value). The lodash function _.isNull() returns null if the firebaseObject path does not exists (a prospect) or undefined if it exists (not a prospect but an existing account).
Is there a better way of doing this?

Categories

Resources