How should I proceed to build or update data based on Firebase user information ?
I'm building an app where people can posts messages in an open feed. On screen, these posts and replies contain the username and profile picture. I'm using Firebase to store these posts and the users.
The question is should I "build" and store a post with the profile pic (url) and username stored, "hardcoded", in that post or retrieve data stored in the corresponding user subtree in Firebase for each post?
If a user updates his profile picture, the first solution means searching through all posts and updating every corresponding post and replies but is quicker in loading the feed, the second seems more efficient for a large number of posts and users but more laggy on execution.
This is my first time with Firebase and with Android. What is the best practice ?
The common practice is to store the profile picture in Firebase Storage. When uploading the picture you need to get the coresponding url of that picture. Having that url, you can easily store it in the Firebase Database or in Cloud Firestore. Every time you need to display the picture, just simply use the reference of that picture. If a user will change the profie picture, there will be no need for doing something. Using that reference will display in every place the new picture.
Related
I'm a beginner developer and I'm currently developing a Quiz app.
Each Quiz contains 50 questions and I'm storing them using Room database.
I've got Coins, Stars, and the score and number of finished questions for each Quiz (And also user's information) that I want to save.
And I still don't know what is the best method to do that.
I was thinking about combining SharedPreferences and Firestore to save user's progress and information, SharedPreferences to save them locally, and Firestore so that when the user signs in on another device, all his progress and information will be retrieved (and saved in SharedPref).
(**Data will be saved in Firestore when the user only signs in with Google)
Now my Questions is :
Can I combine SharedPref and Firestore when saving data? and is it a good idea to do so?
if not, should I only use Firestore for that?
Can I combine SharedPref and Firestore when saving data?
Yes, you can but I cannot see any benefit at all. You say:
Firestore so that when the user signs in on another device.
To have the data available no matter what device the user is using means that each time the user closes the app, you should save the progress in Firestore. So I cannot see why would you use another data structure. Besides that, SharedPreferences do not persist along with app uninstalls. SharedPreferences data is always deleted.
It would have been an excellent idea only if you wanted to store the progress in SharedPreferences and commit the quiz to Firestore only when it's finished. In this way, you'll only be billed with a single write operation, which sounds perfect. However, this solution doesn't provide the feature to have the progress available, no matter what the device the user uses, as it's stored only on a single device.
Firestore has a feature where it will automatically store data in the device's cache, so when your user go offline, they can still view what they last had access to. Since firebase already takes care of when your users go offline, I suggest going with firestore only route.
You can read more about firestore's offline cache here(Watch the video, it's great):
https://firebase.google.com/docs/firestore/manage-data/enable-offline
I want to save the data on Firebase, that a user describes on an Intent. The saved data should be used to find one or more matching users with similar informations.
Thanks in forward.
Looks like you are building an app where you can clusters users with similar interests about particular thing, i would suggest you to use firebase firestore for storing data, and you can retrieve data that are similar using the firebase simple and compound query depending on your requirement.
I have a question in logic, to be honest I can't categorize it.
My question is
If I have a table in database with 2 columns let's say table called articles with article title and article content. and I develop mobile app which displays all the title in a list then when the user clicked on the title the will move to a new page "activity" and display its content.
I have 2 ways to develop that.
1- connect to the database once, select all the titles and all the content and store it locally and just display the content when its title is clicked
2- connect to the database twice. once to select all the titles, another one to select its content when clicked without storing anything locally
Please notice it is a mobile app so it doesn't have a huge space to store locally.
My main question. which one of this method will be faster and efficient?
Option 2 seems better, because :
you mentioned that you don’t have a lot of local space available
only the content that the user needs will be retrieved, which, depending on the total size of the content, could save a lot of space AND time
Option 2 seems a better option as already explained. To add, even while going with option 2, one of the way to further improve performance can be to use caching mechanisms for the retrieved title and content so that the database is not called every time for the same data for subsequent requests.
For example,
Request 1: title 1 is requested. Database is called and content 1 is returned from db. Content 1 is delivered to the user and cached.
Request n: title 1 is requested again, content 1 is retrieved from cache and returned to the user.
In the main activity of Android Studio, immediately after anonymously signing in into firebase, I have this line of code:
FirebaseDatabase.getInstance().getReference().child("Online Users").push().setValue(currentUser.getUid().toString());
So, the UID of current user will be saved into firebase under the child "Online Users". Good. Problem is, when I open the app for the second or third time, or if I navigate from another activity to the main activity, this line of code gets executed again, and now I have several identical entries in the firebase. I mean, one UID has been pushed into firebase several times. But I don't want that. I want only one copy of each UID there. How can I achieve that?
Whenever you want a specific value to be unique in the Firebase Database, model it as the key of a list.
In your case that means that instead of calling push() you actually use the UID as the key:
FirebaseDatabase.getInstance().getReference().child("Online Users").child(currentUser.getUid().toString()).setValue(true);
Now whenever the user comes online again, they're just writing true to the same location again.
Note that the same user can come online from multiple devices. If you are trying to build a presence system, tracking what users are online, I recommend studying the sample presence system in the Firebase documentation which handles the edge cases correctly.
I was trying to develop an mobile app which have some similar idea just like Uber, which is real time update driver & customer location. So here I am seeking for suggestion for what I was thinking for the app structure.
For what I research, in order to provide fast real time update location, I may need to make use of real time database such as Firebase for the backend. So, I was thinking to combine 2 different type of database to achieve what I was thinking...
Firebase - For real time fast update user location
MySQL - For backend api business logic
However, I have no experience with firebase, so I hope you all can give some advise. I plan to only store the user location coordinate information in firebase database, then retrieve it from mobile app to update realtime.
My problem is I not sure should I forever persist those driver coordinate data in firebase database? Since the coordinate data keep changing update in firebase, so should I delete those coordinate data from firebase as soon as the driver have reach the destination. (No need to keep those data persist, only real time data keep change on firebase)
Thanks for reading such long question, I will also happy that if you all can remind to me any other concern if I use 2 different database for my application.
You'll typically keep a list of drivers and their locations in Firebase:
driverlocations
driver1id: location
driver2id: location
This means that you're not adding new data, but updating existing data. So you don't have to worry about the list constantly growing.
To ensure you don't have stale data for drivers that closed the app/stopped driving, you can use Firebase's onDisconnect() handlers to remove the data when they disconnect.
Now querying this data for nearby drivers is still tricky. You'll want to look at GeoFire for that. I recently explained why that is needed and how it works here: Sort array by distance near user location from firebase