Given the date model provided below, after a user logs in and retrieves their data, they can also get a list of their friends.
However, in order to now attain details of those two friends by ids fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de and a96da7b1-7c4e-44bc-b82e-fc75bed52bcd,
at present, the most efficiently operation appears to simply be to loop through the urls, in such a way as:
for singleFriendID in allFriendIds
firebase.com/[my_db_name]/users/{singleFriendID}
end
but even that has its major limitations, if say you want a list of usernames of your 40 friends, you now need 40 individual, specific calls, to the server...
How can I efficiently lookup data within Firebase instead of dumping all the friend details to their phone?
Firebase schema:
{
"users":{
"99e4989b-a046-4c5f-9478-5ebd8bdc3ded":{
"email":"person#gmail.com",
"friends":{
"fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de":{
"confirmed":true,
"selfSendRequest":false,
"timeInvited":"2016-02-21 08:49:31"
},
"a96da7b1-7c4e-44bc-b82e-fc75bed52bcd":{
"confirmed":true,
"seldSendRequest":false,
"timeInvited":"2016-02-21 08:49:31"
}
},
"password":"aPassword",
"phoneNumber":"16144444444",
"username":"person2"
}
"a96da7b1-7c4e-44bc-b82e-fc75bed52bcd": {
...
...
}
}
Loolooii's approach will save on the number of calls and is definitely a valid approach.
But you're also making the assumption that 40 calls to the Firebase database is a lot. If you take a moment to measure this, you'll probably be surprised.
In traditional databases, you do a roundtrip to the database for each call.
client server
--- give me item 1 --->
loading
item 1
<-- here is item 1 ----
--- give me item 2 --->
loading
item 2
<-- here is item 2 ----
--- give me item 3 --->
loading
item 3
<-- here is item 3 ----
--- give me item 4 --->
loading
item 4
<-- here is item 4 ----
Firebase pipelines all the requests. So essentially the clients immediately send the requests for all items in quick succession:
client server
--- give me item 1 --->
--- give me item 2 --->
--- give me item 3 --->
--- give me item 4 --->
loading
item 1
loading
item 2
loading
item 3
loading
item 4
<-- here is item 1 ----
<-- here is item 2 ----
<-- here is item 3 ----
<-- here is item 4 ----
You'll find that this is much faster, since you're only waiting for 1 roundtrip, plus the time to load the items. If that last bit is concerning, take Loolooii's approach.
There is no need to do 40 calls to the db. You simply keep a list of friends per user (with each object containing every user info that you might need):
/users/:id:/friends
So when you want a list of your friends:
/users/:myId:/friends
Which will give you a list (with one call!), which you can loop through. So there is no need to do a separate call for every specific user endpoint.
There is no problem with having duplicate data in your db to improve performance!
Update (to answer the comment):
To show friends of a user or for example a list of people who have liked a photo, it usually suffices to only store IDs and names. To show more information, the profile of that particular user could be shown on click event.
Related
I am working in a app
App conatins Alot of data in it more than 4000
Images, name ,phone,Image size is more than 250kb in hd quality
When data is load in a app
App isn't responding Close and wait dialog box appears
How i can manage it ?
4000 data is in Arraylist
To logically handle the issue of improving the performance of your application, you first need a local database (sqlite,room,realm,active android , ...) where you can manage the data (for better control, of course!)
And to use photos with that quality, if your program can connect to the Internet, read the photo addresses from there and save the photo address along with that text in a line that you can use it easily (because usually In any type of database, they do not store the image directly in the table.
First, they put it in a separate server or repository and only display the address of the saved path in the table.)
In terms of display in Recycler View, it is better that the photos you use have a smaller volume (both in terms of size and size) in addition to their quality, and you can use the same images, but in the size of a thumb strap. The quality of the first view is not important, you can add more details to the items to say that after clicking on this item, now that photo will be displayed in high quality!)
If the number of your records is too much for the show and ... the recycler makes the view itself because it makes the view every time (that is, for example, it brings 10 records in each show), it can handle this issue, but! For better efficiency of your app, it is better to use the infinite paging or scrolling feature.
You can use limit and offset with your query:
For example:
We have a table with 5 records of id column, values are 1 | 2 | 3 | 4 | 5 in respective 5 rows.
IF perform select * from TABLE_NAME: it returned all records from a table in order 1 | 2 | 3 | 4 | 5 .
If we need a total(e.g. limit) 1 record and it should start from offset index 2, then use
select * from TABLE_NAME limit 1 offset 2
This will return record index | 3 | as request limit = 1 & offset index = 2.
The same can be achieved using select * from company limit 2, 1;
Note, here 2 is offset & 1 is limit(order reverse).
Since Android SDK does not provide a separate attribute to set offset, in that case last 'limit' attribute of SQLiteDatabase.query(..., limit) can be used as limit 2, 1.
I'm using firebase database to store data. I know how to use firebase database.
In my project I've an activity which displays a list of notices from faculties to students and by clicking on list item it will open another activity which is a chat room. In this activity student can ask question related to given notice.
Now, In first list activity I've also added on text view which displays the total number of chat messages for that particular Notice. It works well when i load data from database initially. But i want to change number of chat messages dynamically.
So, i need to listnen at chat_cnt node to update value of message_cnt text view
and problem is it is a big list so i can not use child_added for each and every notice item and if i use value_event listener than it will load all data unnecessarily at start.
The code is a little bit big so i cannot post it here. But I've described the problem.
Any answer would be appreciated, thanks. Sorry for my english :)
I am working on a relatively simple android application: there's an sqlite database, and the user is presented with a list of the titles, and upon clicking on an item, the entire details of that record are displayed. But the problem is that the data is not uniform; some rows have only a couple of lines of text, some rows have a line of text and then an audio file, some rows have two or more audio files plus some text. How should I present this data, and more importantly, how do I store this data?
P.S. I want the audio to play at their respective places.
ii've solved the issue. the data is stored using two tables. example:
table 1:
id title
--- -----
1 blah
2 blahblah2
3 blah3
table 2 [details]:
id text img aud
--- ---- --- ---
1 jsgsg img11 aud11
1 segs img12 aud12
1 jbcj img13 aud13
2 nvnh img21 aud21
2 kjbj img22 aud22
3 jhbb img31 aud31
and the data is presented in inflatable listview containing one textview, one imageview and one audio button, the visibility of each depending on the value in the database(view invisible if value is null). one list each for all items with the same id.
I want to add a Highscore Screen to my Quiz App. I've already created a Highscore Screen with a start value, which contains the score the user has reached in the last round, the category-name and the difficulty.
I split this start value into 2 variables:
The first contains only the score and the other one the category and the difficulty. Now all in all I have 3 categories and 2 difficultys for each of them. Now I want to keep the top 10 Highscores of each category and difficulty. Like this:
Category 1 Difficulty 1
Category 1 Difficulty 2
Category 2 Difficulty 1
Category 2 Difficulty 2
Category 3 Difficulty 1
Category 3 Difficulty 2
As you can see, I will have 6 different Highscore lists.
Now my question:
How can I save all of the 6 lists in my TinyDB and reload the data again?
for each of the lists use its own tag for TinyDB
to save one of the lists, use the TinyDB.StoreValue block, to get it again in Screen.Initialize use the TinyDB.GetValue block, see also the docu and remember: on first run TinyDB is empty, see an example here how to handle that.
and: do the tutorials to get familiar with the basic concepts of App Inventor.
I have a listview in which a user adds his comments on a particular topic, these are then stored on server,
as multiple users may add comment i wish the listview to update periodically (say 10sec),
and add only the latest data and not the complete data as it will slow down the application.
How do i do this.?
Fix some Buffer Comments something like 50. If the number of comments is more than 50 then display the last 50 comments only. Implement pagination concept from the server. Keep some 'Load More' button or Auto Load functionality(just like in twitter or facebook) for viewing more rows in the List.