I creat a query that hold some info from firebase - android

I creat a query that hold some info from firebase And I got this error
this is the error
And this is my query query
thi is the body
When i run my code the first if statement appears in the screen, which is the error I had
I tried to delete the where condition for the status but i had the same error

Okay, this is very simple, from your images I couldn't see anything but I think I know what is going on.
how firebase work is that when you use where() to query a collection it works fine but if you use 2 or more where() the firebase will take longer so to improve that they allow indexing and the error you got it should have a long URL in it click it and it will take you to your firebase project and create the needed indexes for you.
wait for like 5 min for them to build and retry using that query from your app
PS: some time the indexing is required even with just 1 where()

Related

How to count how many times document was downloaded in Firebase?

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.

remove old data in Firebase

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.

Run query to get Event count from big query

I am asking how to run query to get event count from my android application.My app using Firebase realtime database and we linked firebase analytics to Big query.How to get my custom event count by run query from my android application.or Tell me how to get that count from my android app,i have one field in my application to show that count.
Only the data gathered by Analytics will be exported to Big Query once the linkage has been established.
Raw data for event count should be available in the event_dim as per the Big Query Export Schema.
Refer to the sample queries for Firebase-Big Query for more details in this official Google docs.

How to know when to insert into my database?

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.

java.lang.ArrayIndexOutOfBoundsException: asked for back ref X but there are only 0 back refs

I'm doing some database work on Android, and get the following error when trying to insert a bunch of records using applyBatch.
java.lang.ArrayIndexOutOfBoundsException: asked for back ref 26 but there are only 0 back refs
What's causing this?
For me, it turns out that I was trying to insert a group of records using applyBatch, but one of the later records depended on an earlier record in the group being inserted correctly, which it wasn't (it failed).
I identified the error by doing "applyBatch" with each of the ContentOperations, one at a time. Then i could easily see why it was failing.
Note that this post was helpful in figuring out the problem.

Categories

Resources