FirebaseUI with Firestore Recycler Options and custom adapter - java

I'm using the FirebaseUI and a custom adapter to show data coming from Firestore database and storage, inside a list of Cards (on Samsung Galaxy S6 and S8 devices).
I was first using the method described here (from the firebase.google webiste) but I then switched to this one (from firebase github) as it is more suitable for my needs.
When I tried to see if data were coming from the server or the cache with the first method, the code below worked as expected:
String source = snapshot != null && snapshot.getMetadata().hasPendingWrites()
? "Local" : "Server";
However, when using the second method, when I get my list of Snapshots, inside the adapter, from the query, and try to see if the data are coming from the server or the cache, I always get:
mSnapshots.getSnapshot(position).getMetadata().hasPendingWrites()
equal to false
and
mSnapshots.getSnapshot(position).getMetadata().isFromCache()
equal to true
independently of my internet connection.
Why? And how can I see if the data are coming from the server or the cache using the second method?
Thanks

Related

Can't write on Realtime Database [duplicate]

I have created a Firebase, and I can write data into Authentication.
But, I could not write data into realtime database.
I have connected to Firebase
I have add Realtime Database
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:28.4.1')
// Declare the dependency for the Realtime Database library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-database:20.0.2'
I have set rules of read and write to ture
{
"rules": {
".read": true,
".write": true
}
}
I tried this to write data into Firebase but I get noting
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
// Write a message to the database
FirebaseDatabase.getInstance().getReference().child("aaa").child("bbb").setValue("ccc");
FirebaseDatabase.getInstance().getReference().child("aaa").child("bbb").setValue("ccc");
The code for writing looks fine, so I think the problem may be elsewhere.
If you created the Firebase console after you downloaded the google-services.json file, that file will not contain the correct URL, and the SDK may not be able to find your database on the server.
To solve that problem, you can either:
Download an updated google-services.json and use that in your app, or
Provide the database URL in your code with FirebaseDatabase database = FirebaseDatabase.getInstance("database URL here");
My code works this morning, but I did nothing except go to bed. As shown in the picture, the second step has changed. I clicked that Add the Realtime Database SDK to your app button thousands of times, and it was still a button. Surprisingly, it has changed to a checked state.
I did the google-services.json part as #FrankvanPuffelen said yesterday, but it didn't work.
I have restarted the Android Studio a lot of times and didn't work.
Maybe I should try close the computer lid next time.
But now I know that one should have the second step checked rather than keep it as a unchecked button.

Multiple Firebase Projects in one app with different persistence setup

I want to setup 2 Firebase in 1 App, but with difference persistence.
One is enable, and the second is disable.
Please inform me how to set it up.
I take code from link below,
Multiple Firebase projects in one app
but it didn't explain,
how to setup persistence for the second Firebase.
I want to enable persistence for 1st Firebase.
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
And disable persistence for 2nd Firebase.
FirebaseDatabase.getInstance().setPersistenceEnabled(false);
As we see, that getInstance is static method,
how do we know that,
the returned FirebaseDatabase instance,
will belong to 1st or 2nd Firebase.
private void initSecondFirebaseAcct()
{
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("<your application id>")
.setApiKey("<your api key>")
.setDatabaseUrl("<your DB url that ends in 'firebaseio.com/' ")
.build();
try
{
FirebaseApp.initializeApp(this, options, "<database tag>");
}catch (Exception e){
Log.d("Firebase error", "App already exists");
}
mMySecondApp = FirebaseApp.getInstance("<database tag>");
mSecondDBRef = FirebaseDatabase.getInstance(mMySecondApp).getReference();
}
The way to make sure you are referencing the first or second Firebase app is to provide the specific FirebaseApp instance as a parameter to:
FireDatabase.getInstance(<app instance here>).setPersistenceEnabled().
If you use FirebaseDatabase.getInstance() you will receive the default instance, the first database app. So just provide the second instance as a parameter and you'll be good to go.

Play Framework - How to build a web page with multiple MySQL queries

I've created my first Play Framework Website with Java using the official documentation. It has a single page where I display a list of items that can be filtered or modified.
I have a Controller class with a method:
public CompletionStage<Result> feedpostslist(String domain, String date, String state, int page, int resnum, String search) {
return feedRepository.getArticleList(domain, date, state, page, resnum, search).thenApplyAsync(articles -> {
FeedArticle[] list = new FeedArticle[articles.size()];
articles.toArray(list);
return ok(views.html.feedpostslist.render(list));
}, ec.current());
}
This method does a query to the DB (through feedRepository) and then display the result using the view feedpostslist.
Everything is fine but now I need to get other data from the DB to be used in the same web page (so multiple queries). How do I do this in Play Framework? I don't understand what is the best way to do that.
Should I do multiple DB request inside the method showed before (through feedRepository) and then pass all these informations to my view? I don't want to do a mess or even something too heavy to handle.
If the second query doesn't depend on the first one you can run them in parallel using combineAsync. This is a good example on how to do that:
https://github.com/playframework/play-samples/blob/2.8.x/play-java-ebean-example/app/controllers/HomeController.java#L85
If the second query depends on results on the first then there's nothing you can do but to wait for the first one to complete and run the second one.

How to get the modified content from google sheets?

I am trying to get the modified content after the given time from google sheets. Nowhere I can found the api to get the data. What i can see is getting modified date alone from the drive Api. How can I get the data using Drive or Sheets Api? Give me the suggestions if Possible
Google Drive keeps a track of revision history of files that are contained on it. There is however, no way to obtain the revisions from a request alone.
Google allows for you to receive email notifications whenever a user makes an edit to your sheet, which you can set up by completing the following steps:
In the Spreadsheet's web view, click Tools -> Notification rules...
Under Notify me at myemail#address.ext when... select Any changes are made
Under Notify me with... select Email - right away
Click Save.
You should also be aware that you will not get a notification for edits made to the sheet by you - notifications are only received when another user edits the sheet. Whenever you get an email notification, you will receive a link to view the changes to the spreadsheet in the form of a read-only web view link.
You can work around this programatically, though there isn't one right way and it can be quite complicated. You can use the Revisions: list method of the Drive REST API to get the information about the user that made an edit, as well as a list of links which you can use to export that revision of the sheet to another MIME Type, as shown below in the request response.
Requesting:
GET https://www.googleapis.com/drive/v3/files/SPREADSHEET_ID/revisions
with revisions/exportLinks,revisions/lastModifyingUser/emailAddress as the fields field and replacing SPREADSHEET_ID with the ID of the spreadsheet will give you a 200 response:
{
"revisions": [
{
"lastModifyingUser": {
"emailAddress": "username#domain.ext"
},
"exportLinks": {
"application/x-vnd.oasis.opendocument.spreadsheet": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=revisionNumber&exportFormat=ods",
"text/tab-separated-values": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=revisionNumber&exportFormat=tsv",
"application/pdf": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=revisionNumber&exportFormat=pdf",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=revisionNumber&exportFormat=xlsx",
"text/csv": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=revisionNumber&exportFormat=csv",
"application/zip": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=revisionNumber&exportFormat=zip",
"application/vnd.oasis.opendocument.spreadsheet": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=1&exportFormat=ods"
}
}
]
}
With the links to individual changes, you can fetch and compare the different versions of the Sheet using Apps Script, and output A1 notation of the cells that have different values between versions. This, with the email address from the original Revisions: list request, is enough to compile a file or a log containing.
You can put this into a simple onEdit() trigger bound to the sheet will allow you to automatically get the changes each time a user edits the sheet.

View.getEntryCount() returns 1500, but can't get any document by key?

I'm writing a simple standalone Java class that uses Lotus Domino's NCSO JAR for remote-connecting to a Domino server.
I'm establishing a session, getting access to a database and then to a view:
Session session = NotesFactory.createSession("host", "user", "password");
Database db = session.getDatabase(null, "MyDB.nsf");
View view = db.getView("MyView");
Now, I'm printing the number of entries in the view:
int count = view.getEntryCount();
I get a nonzero number (let's say 1500).
However, I can't seem to load any document by key. For example, for any letter in the alphabet, I'm getting an empty document collection with this call:
System.err.println(view.getAllDocumentsByKey(letter, false));
When I try to load a document by key, when I know that the key exists in the view, I get null.
Document document = view.getDocumentByKey("DocKey"); // Equals null even though
// I know that 'DocKey' is
// the key of an existing
// document within the view.
The very same code is said to be working (although I didn't check it) when using local Notes calls (using Notes.jar).
What am I missing?
EDIT
I just noticed that session.getNotesVersion() returns version 8.5.2. The NCSO.jar file that I'm currently using doesn't appear to have a few methods that were added with Notes 8. Therefore, there is a possibility that the NCSO.jar file I use belongs to an earlier version of Notes than the one I'm trying to communicate with. Could that be the reason?
If the same code is working locally, then that should rule out the possibility that the first column of the view isn't sorted. Assuming that, then the most likely issue is that the documents are protected by ReaderNames fields and the identity that you are using for authenticating your session does not have access to the documents.
Assuming I understand you right, you want to get all documents where the first (lookup) column of the view contains anything that starts with a specific letter?
E.g. you send "A" to veiw.getAllDocumentsByKey() and expect a collection that contains "Apple", "Alpha", "Amoeba" and "Apricot" to be returned?
I would modify the column in the view to only hold the first letter:
#Left(MyField;1)
Then it would be easy to perform the lookup and see if you get the correct result.

Categories