I know this question a lot here, But I don`t know about this. This is about chat app.
When message get stored into the firebase database. It would become bigger and bigger as time goes by, I want to delete it. and when should I delete this? I just want to left just only last 10 data. It means if I out the app and again go in, It appears only last 10 sentence and I know about the function limitToFirst and limitToLast (but this is not the delete thing.)
If I pay firebase server, you know, if database's data is large It will be more expensive. but I just want to leave just last 10 sentences at least.
when they did come back then they can see the last 10sentence and want to delete except for this. how do I do this?
I saw the answer using the date, but I don`t want that. Is that only answer? If I must do that when I get to delete them? When do I invoke the delete function?
I know how to do that almost, but when? If in the app, there are so many friends and I open the chat screen to chat my friend. That time should I delete them using remove function? How am I saving this for server payment?
I don`t want cost a lot. and I want them to be clear, not dirty in my Firebase database console. so want to delete them. Which is the I have to do? Which time is the best time that I should delete them? When open it ? or when close it ? or when users stop the my app.
You can achieve this in a very simple way. You can use the getChildrenCount() method on the node in which you hold the messages to see the exact number of messages. If you are using as an identifier the random key generated by the push() method, it's very easy to delete the extra messages. Because the records are by default order by date, you can easily query your database using limitToLast(limit) method and than delete the messages like this:
yourRef.child("messages").child(messageId).removeValue();
Another way to achieve this is to use Cloud Functions for Firebase.
Hope it helps.
you can use firebase functions to delete old data when new ones added to the database that is the best time, you can keep the last 100 messages or less.
Related
I have a collection in my Firestore Database with posts. Each post has some comments as a document in the collection comments in the main post document (screenshot)
In my Android app when I click on the post item I go to PostDetail screen and there I download comments.
I want to increment the value of timesViewed for each comment which has been downloaded and displayed.
Is it possible to do that? I want to do that in the most efficient way. I don't want to increment every single comment manually by sending a request timesViewed++.
I want to increment the value of timesViewed for each comment which has been downloaded and displayed.
If you're looking for something that does that automatically, please note that there is nothing built-in.
Is it possible to do that?
Yes, but you should implement your own mechanism.
I don't want to increment every single comment manually by sending a request timesViewed++.
Unfortunately, there is no other way. However, it is very easy to implement. You can use FieldValue.increment(1).
Unfortunately, such a feature does not exist in the RealtimeDatabase firebase and firestore to automatically increase a field.
But you can create a counter and retrieve the counter every time onResponse () or onComplete and increase it by 1, which is not recommended for 2 reasons.
Generates extra traffic, especially if the number of users is large
It is not reliable because someone can intentionally increase it
But if you do not mind a lot of traffic and you insist on doing so, do not forget to set rules in the firebase console to control the incremental frequency.
Working with Firebase for the first time and looking for advice of setting up the right structure for my project which is basically an "offers/coupon" type starter project.
The scenario is this:
I have a node containing a list of all offers available to users
This list of offers is displayed to users after successful Firebase authentication
When a user redeems an offer, I want to be able to count/record that activity in their child node under user and hide that offer so that they cannot see it again once used.
My question is what would be the best way to do this given that offers may be added, may expire, or may change at some point in the future. So, in effect, the user should receive the list of most updated offers, minus the ones he/she have used in the past.
a) would it be more effective to have a master list of offers, and then run a cloud/server function to clone this list for each new user an track that way
Firebase Structure 1
or
b) Keep a master list of offers in one node, then track user specific offer usage
Firebase Structure 2
Appreciate your guidance
The second solution is better because you'll save bandwith. This practice is called denormalization and is a common practice when it comes to Firebase. The first solution is not good becase every time you want to display the users you donwload unnecessary data. If you want to read more details about how you can structure a Firebase database in a efficient way, please read this post, Structuring your Firebase Data correctly for a Complex App. Also, you can take a look a this tutorial, Denormalization is normal with the Firebase Database, for a better understanding.
Second solution is much good. Because in first one we are having redundancy of data in our database. And second one obviously removing that cause.
But instead of using true or false because it is only showing you, "it's available or not", so you can use a string type parameter as "expired", "going to expire" and "updated" or whatever sooo. So it. Will be able to trace all you information related to offer for particular user. I think this is your requirement also.
Happy coding.
I have been looking for this quite some time now and i haven't found any answer related to my problem, so before you tag my question duplicate, at least read it first.
First i have just started firebase and only know basic things about it, i am trying to make a simple single user (1 to 1) chat app.
I want to get the number of users logged in in the database.
I have a child to the root of my database called users which have the list of the users who have logged in.
I know about the datashot.getchildrencount() but that works when some update/event happens, but i want it to give me the number of users whenever i want, not only on some event (For example in messenger number of current active users are shown continuously,i dont want active user tho, i just want total logged in users).
I thought of the another way to make a child called NumberofUsers so i store number of users there but then firebase dont allow to getvalue of the child,only set value (it does allow to get value but only on some event). Any idea what should i do?
UPDATE
i thought of another way to do this, though its not working at the moment but i think solving it is easier than thinking of another method to solve my original problem.
so I made one more child of NumberofUsers with key "02" and random value. now everytime i want data of "01" i change the value "02" after enabling the addChildEventListener for "NumberofUsers". Code that does this. Error its giving me. .new database.
Apparently I cant access child data using datasnapshot, any solution?
Any idea how to solve either of this?
Thank you.
Unfortunately, there is no way in which you can read data from a Firebase Database without attaching a listener. Regarding Firebase, everything it's about listeners. In your case, i think you want a callback to be called once and then immediately removed. If so, i recomand you using addListenerForSingleValueEvent() method to simplify this scenario. It triggers once and then does not trigger again.
This is useful for data that only needs to be loaded once and isn't expected to change frequently or require active listening.
Hope it helps.
I am trying to figure out how I execute my database operations efficiently and effectively. I have it so that when a user logs in, I use the Facebook API and grab details regarding the user's friends and place them into my database. This is done in a method called createFriend. However, if I login a second time, I get an error because every friend has a unique user id. Therefore, if I log in the second time, my code calls the createFriend method yet again and tries to insert the same friend that was already inserted the first time I logged in, and I get a unique column violation.
As a result, I don't know when I should call the createFriend method. I thought of 2 solutions to fixing my problem:
I looked at this link to see if I could check each row and if that row exists, only then I do the insert. I found: SQLiteDatabase: Insert only if the value does not exist (not via raw SQL command). It just doesn't seem very efficient to check every single time I want to insert a friend, to see if the row exists, and if not, I don't insert, otherwise I do. I have to do this select statement for every friend the user has.
The other method I was thinking of was this: I can call the Facebook API and check if the number of friends returned from the Facebook API is equal to the number of rows in my local database. If so, I skip calling createFriend all together. If not, I can just remove the entire database and reinsert all the friends again. However, this doesn't seem to work well because there is the chance that the Facebook API can keep returning the same number of friends as are in my database, BUT those friends returned could be different people than what the database has. Therefore, the database won't update because the number of friends is still the same, but I would want it to update because I would like to insert those potential new friends into the database.
Can anyone please point me in the right direction on where I can solve this problem? Thank you!
You can update on duplicate key.
Have a look at this for reference,
SQLite UPSERT - ON DUPLICATE KEY UPDATE
It just doesn't seem very efficient to check every single time I want to insert a friend, to see if the row exists
You are jumping to conclusions.
Guesses are likely to be wrong; you do not know how fast a query is until you have actually measured it.
And when you have a UNIQUE constraint, the database searches for a matching row anyway, so there will not be any additional I/O.
In any case, make your algorithm correct before thinking about optimizing it.
And if you want to ignore friends that already exist in the database, just use INSERT OR IGNORE.
i am trying to develop a simple application. i want to create a simple message and want to save it. now, i want to select message from list of messages which i have created and stored.
can any one please suggest me or give an idea for developing the same.
Thanks in Advance
If you substitute the word "message" for "note", you are describing something really similar to the notepad example.
It runs you step by step trough the code you need to make an application that has notes (messages) you can add, and open.
If you are new to Android development you should go trough all the excersises, because it's a really good help, but if you're not you can just download the sollution and use that.
I feel you should go like this:
Have your application store Contacts in a database (I guess you're already doing this)
Have your application store messages in another database (I guess you're already doing this too)
In the UI, display all the messages inside a ListView, by querying them from your message database.
When user clicks on any message, have another screen that loads this message in full, and lets the user select the recipients
Send the text message to the selected recipients now
I just wanted to point out the flow of the app, since the storage and retrieval process os pretty straight forward. I am not sure if this was what you were looking for.
Do let me know.
If you will store little amount of data you can use sharedpreferences
http://developer.android.com/reference/android/content/SharedPreferences.html
If you want a real solution that is sqlite database that comes with android.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html