Delay While Retrieving Data From Firebase Database - java

It's a chat app. I am retrieving messages to show on the chat room recyclerview. For that I'm using FirebaseRecyclerAdapter. I'm retrieving latest 500 messages. And that chat room has over 20k messages.. When I load that activity it is taking 6 seconds to load all 500 messages.
I tracked the time.
The delay was between setAdapter and RecyclerAdapter class.
My Message Structure In Firebase Database
{
"-KlQc42ici0_PRCBX7-V" : {
"message" : "message 1",
"timestamp" : 1496185853436,
"type" : "action",
"username" : "berkleef2",
},
"-KlQmhcwK96j30FNoSs-" : {
"message" : "message 2",
"timestamp" : 1496188640000,
"type" : "action",
"username" : "atrickrodney16yahoocomat",
"zread" : true
},
"-KlQmjSoucJD7uaHriOh" : {
"message" : "message 3",
"timestamp" : 1496188647083,
"type" : "text",
"username" : "atrickrodney16yahoocomat",
"zread" : true
}
}
The Delay Started after setAdapter
mNewAdapter = new NewChatRecyclerAdapter(
ChatMessageItem.class,
R.layout.message_item,
RecyclerView.ViewHolder.class, ref.limitToLast(500), Fusername, booIsPrivateChat, chatd, strOpponent);
messageList.setAdapter(mNewAdapter);
I think there is no problem with the adapter class. So if the problem is with loading 500 messages. Then what should I do in this case to load chat messages...

Use Firebase Offline feature set persistence enabled on so that you can store your messages

You can fetch them in chunks of 20 or 50 messages one after another.

You can use keepsynced function given by firebase database on your database reference.

Related

REST Api - JSON - "Cannot deserialize instance of object out of START_ARRAY token"

The issue in question is as follows:-
I have a POJO with a list of Apple objects , say applesList.
This is stored in Cosmos DB.
When applesList is null, the get endpoint works fine. I get the Pojo as is.
When applesList is populated with apples, i get the error as given in the Title.
How do i fix this?
Json that works with get :-
{
"userId" : "123",
"applesList" : null
}
Json that gives stated error on get, applesList is populated by api :-
{
"userId" : "123",
"applesList" : [
{
"color" : "red",
"weight" : "150 g"
},
{
"color" : "green",
"weight" : "200 g"
}
]
}
Edit :- missed mentioning Cosmos DB SQL API
Change applesList to an array.

Can't call App Script function thru the REST api

I'm using Java API for calling the AppScript function on the spreadsheet
When I list deployments
List<Deployment> deployments = projects.deployments().list("XXX").execute().getDeployments();
I can see my deployments but the deployment.getFunctionSet() is NULL
So when I call
Operation op = service.scripts().run(
"XXX",
new ExecutionRequest().setDevMode(true).setFunction("exportJson")
).execute();
I get
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"message" : "Requested entity was not found.",
"reason" : "notFound"
} ],
"message" : "Requested entity was not found.",
"status" : "NOT_FOUND"
}
The question - how to call function from the google app script? )

Firebase .indexOn is not working as expected

I have a content node that I retrieve from a Firebase Realtime Database.
I utilize .orderByChild("time") to get a Data Snapshot of the first 5 blog objects ordered by their earliest timestamps. I
am currently attempting to utilize .indexOn : "time" instead so that I do not have to utilize .orderByChild("time") as I am expecting for the blog objects to already be ordered by their timestamp by back-end when they are retrieved. (I am going to be working with a very large number of blog objects, so I want to utilize .indexOn : "time" in back-end instead of orderByChild("time") in front-end to increase efficiency). Currently, .indexOn does not work and the data isn't ordered by their time fields when it is retrieved.
query without .indexOn
// this works fine
// startAt is utilized for saving last blog object retrieved to retrieve more data
query =
FirebaseDatabase.getInstance().getReference()
.child("content")
.orderByChild("time")
.startAt(nodeId)
.limitToFirst(5);
query with .indexOn
// this along with the Firebase rules below does not return the same result as above
// startAt is utilized for saving last blog object retrieved to retrieve more data
query =
FirebaseDatabase.getInstance().getReference()
.child("content")
.startAt(nodeId)
.limitToFirst(5);
Firebase rules:
{
"rules": {
".read": true,
".write": true,
"content" : {
".indexOn": "time"
}
}
}
JSON structure of data in Firebase :
"content" : {
"blog-0001" : {
"content_id" : "blog-0001",
"image" : "someimage",
"time" : 13,
"title" : "title1",
"type" : "blog"
},
"blog-0002" : {
"content_id" : "blog-0002",
"image" : "someimage",
"time" : 12,
"title" : "title2",
"type" : "blog"
},
"blog-0003" : {
"content_id" : "blog-0003",
"image" : "someimage",
"time" : 11,
"title" : "title3",
"type" : "blog"
},
"blog-0004" : {
"content_id" : "blog-0004",
"image" : "someimage",
"time" : 15,
"title" : "title4",
"type" : "blog"
}
...
}
You seem to be misunderstanding what .indexOn does, and how it related to orderByChild("time").
To retrieve child nodes from content ordered by (and possibly also filtered on) time, you will always need to call orderByChild("time").
If you define an index (using ".indexOn": "time") that ordering and filtering will be done on the server.
Without an index, all data under content will be downloaded to the client, and the ordering/filtering will be performed client side.
So .indexOn is not a replacement for orderByChild. Instead the two work hand in hand to perform efficient ordering and filtering of data.

sorting by date in firebase database [duplicate]

This question already has an answer here:
Firebase sort by points depending on date
(1 answer)
Closed 4 years ago.
I have a database that looks like this:
{
firstitem : {
"name" : "first_item_name",
"date" : "29/11/2018",
"user" : "random user",
"url" : "My Url",
"tag" : "firstitem"
},
seconditem : {
"name" : "second_item_name",
"date" : "20/11/2018"
"user" : "another user",
"url" : "a url",
"tag" : "seconditem"
},
thirditem : {
"name" : "third_item_name",
"date" : "20/11/2017"
"user" : "another user",
"url" : "a url",
"tag" : "thirditem"
}
ect....
}
I want to be able to have an option to sort by date, starting at the newest, and descending to the oldest. How can I achieve this?
This is my method for calling the firebase strings at the moment:
if (dataSnapshot.exists()) {
Log.i("Event", "Added");
Firebase_Strings dlStrings = dataSnapshot.getValue(Firebase_Strings.class);
p9p_strings.DOCUMENTARY_NAME.add(dlStrings.name);
p9p_strings.DOCUMENTARY_SYNOPSIS.add(dlStrings.about);
p9p_strings.DOCUMENTARY_URL.add(dlStrings.url);
p9p_strings.DOCUMENTARY_UPLOADED.add(dlStrings.date);
p9p_strings.DOCUMENTARY_TAG.add(dlStrings.tag);
There is no way in which you can sort your items by date if you are storing the date as String "29/11/2018" and not as a timestamp.
To solve this, you should change the type of your date property to number.
And here you can how to add and get back the timestamp.

Mongeez - Adding user to database

I'm testing Mongeez, but am having trouble adding a user to the DB system.users collection.
My code is:
MongeezTest.java
Mongeez mongeez = new Mongeez();
mongeez.setFile(new ClassPathResource("mongeez.xml"));
mongeez.setMongo(new Mongo("127.0.0.1", 27018));
mongeez.setDbName("MongeezTest");
mongeez.setAuth(new MongoAuth("admin", "admin"));
mongeez.process();
test.javascript
// changeset mlysaght:ChangeSet-1
db.organization.insert({
"Name" : "10Gen", "Location" : "NYC", DateFounded : {}
});
db.organization.insert({
"Name" : "SecondMarket", "Location" : "NYC", DateFounded : {"Year" : 2004, "Month" : 05, "day" :04}
});
When I run my code I get:
Exception in thread "main" com.mongodb.CommandResult$CommandFailure:
command failed [getlasterror]: { "serverUsed" : "/127.0.0.1:27018" ,
"errmsg" : "need to login" , "ok" : 0.0}
For further details:
I have a MongoDB instance with active authentication.
Using uMongo, after running my Mongeez application I see that the DB gets created. If I manually add a user to the MongeezTest database, and rerun my application, the JS code gets executed.
How do I solve this?
Thanks

Categories

Resources