Querying Firebase is becoming painful day by day Android - android

Working with Firebase from past 3 months made my data "fan-out" and managing redundant data day by day I am feeling as if I am over coding now.
I am typically making logging app. Logging Hours minutes. Here what it does.
Every user signup trough Google authentication.Data is saved in "/users/[users]"
Then they further create some Helper or workers who's time is been logged.
Workers are saved as "/workers/userID/[workers]
Logs : A worker is selected and his logs are added in two places
/logs/userId/[logs] (this is for calculating monthly stats)
/workder_logs/workerId/[logs] (this is for calculating per worker based stats)
This works fine for me..
But now I am taking this app from productivity app to collaborative app.
Where users can connect with each other and request or approve logs
Logs added by one users is reviewed and approved/rejected by other user.
This is what happens when a log in this new logic
User select a worker again which is now linked to a userid (typically linkedUser field in Worker class)
Log is added to /logs/userid/
Log is added to /worker_logs/workderid/
Log is added to /logs/otherUserId/
And once he approve it is added to /worker_log/otherUserWorker/
But before that I need a notification screen. This is where I am getting frustrated by firebase after so many days of work(I am also working on some other project with Firebase).
I want to query this for a single recyclerview.
Select logs from /logs/myUserid/ where logs.date is between 1dec16 to
31dec16 and { (logs.createdby="not me" and logs.status=unapproved) or
(logs.createdby="me" and logs.status=rejected) }
Point is I do not want to maintain further logs redundancies(putting in different buckets namely /log/approved etc and manage them). I am not getting any breakthrough when it comes to querying data in Firebase.
I also feel documentation is not so much helpful.

Related

Get data entry count using firebase analytics

I am new to Firebase Analytics and I went through some online tutorials to learn about it. I have a simple 'Notes' app (Android) in market, which allow users to create and save notes offline on their device.
It would be great, if I could get a total number of notes that have been created till date with my app. I know, I can add an event which will notify Firebase when user creates a new note, but what about the existing notes that are present in the database?
Is there any way to get such statistics through Firebase Analytics?
UPDATE:
I have included a user property as notes_count, but I am not able to find a way to see the total. How to do it?
You could set a user property with the number of notes the user has created instead of or in addition to logging the event. You are limited to 25 different user properties total though, so use them with care.

Firebase user A transfer child content to user B. Possible?

Users gets to sign up/log in via email/password authentification on firebase. Done. I am also able to get the current user and also have him write personal info in his child also, game score points are updated to firebase as he progresses. Done.
Now, say I want user A to transfer points to user B. Knowing that user A is the current user. And perhaps user A knows user B's email or userID. How can I make him access/write in user B score points.
I have tried a few methods none worked:
I tried FirebaseDatabase.getInstance().get reference().child("profile/").child(X.getText().toString()).addListenerForSingleValueEvent blah blah blah.
Where X is B's user ID. Well this returned null.
I read about firebase-admin userRecord ..and after trying to put it in my Gradle and running into many errors I discovered it was for Server side. So not applicable.
I read about making a list of all users but dat didn't seem cool since what happens if I have a million users. It shows all when A only needs B's.
Oh. I'm a mechanical engineer. So please don't refer me to read firebase docs and etc . Most of the terms just confuses me and don't tag this as already asked question believe me I have read them all. Also, I'd very much love it if you could be kind enough to either refer me to a tutorial or explain the codes you have written using A and B as case study.
Here's an answer at kind of a high level:
I would suggest adding a node that will notify other users of pending transfers. Something like
transfers
transfer_0
fromUid: "uid_a"
toUid: "uid_b"
amt: "100"
transfer_1
fromUid: "uid_c"
toUid: "uid_d"
amt: "10"
All users should add an observer query to that node to watch for any newly added nodes where toUid equals their Uid. In this case childAdded for uid_b.
When user a transfers 100 to user b, it will be written to that node and user b will be notified the transfer node was added and can update their own node, then remove transfer_0.
Likewise, if they are offline and then log in, the childAdded will pull in any new transfers so then their node can be updated and the transfers removed when complete.

firebase how many current logged in user in my app

So i have app with Firebase email Auth
I want to keep track of how many users are actively logged in right now
A number in firebase database that can keep changing or list of active users right now!
They are removed moment user logs out
We cant exactly do addition in firebase database so how to do that.. any help?
PS: I dont want to use Analytics, i want a database solution
You need to use firebase StreamView : RealTime Analytics
Below are the details
https://support.google.com/firebase/answer/7229836?hl=en
http://firebase.googleblog.com/2017/03/realtime-analytics-for-everyone.html
For real hardcore database solution you can do the following:
You can add a last_login field in your database users collection. Update that field whenever a user resumes your app. Then you will be able to query once or Live Stream about how many users are logged in the last several minutes.
If you are looking for something much more accurate you can update the user last_login every several seconds from your application and Live Stream the last results.
Note that this may cost you a lot of money since firestore is a pay per action database, Which means that you pay for each update and each read from your database.
You can also checkout #Frank van Puffelen comment bellow your answer.
Enjoy!

Firebase takes too much time to save data

I am having a simple JSON structure in Firebase console. I have a kind of chat app that uses this data to chat with other users. Everything works fine. But just sometimes, even after adding the value to Firebase with setValue, I cannot see the value in the console for a very long time(like around 3-5 mins).
For example:
For the above structure, I am adding values with below code snippet:
mFirebaseTyping.child("68_118").child("118").setValue(typingObject)
Now, it takes too long to update value to firebase. Sometimes around 3-5 minutes.
Does anyone have any work around this lagging issue? Any help would be appreciated.
P.S.: I tried with CompletionListener too, but the response only takes that time(i.e.: 3-5 mins). So does that mean this Firebase Realtime Database is not recommended for chatting apps?

Android Google Play / Drive Api

Hi I'm using the Google Drive Api to store a database using the AppDataFolder facility. I have a test app running successfully on one device. I am able to upload/update/delete/download the database file and reintegrate it into the program with no problems.
The issue I'm having is when I want to share this database with another device running the same app, then things don't work as expected. Example device A I upload the database, device B - I want to download the database but no file is found (this delay can vary greatly from seconds to hours). Reason for this - when using the Api it decides when it wants to 'sync' data, as it is queued rather than being instantaneously uploaded. So when used on one device this is not a problem because it takes either the 'synced' file from the cloud storage, or file waiting to be synced.
I have tried various things like trying to list all AppDataFolder files or retrieving metadata through a query with searchable filters before making a request to update/delete etc.. However I can't get it to work as desired fundamentally it chooses when to sync.
So my actual question is: How can I force Google Drive to sync my file when I want it to i.e. every time a request is made, so that synchronisation is achieved across multiple devices using the same app. There must be an answer as I would think this is quite a fundamental reason why you would use the AppDataFolder is the first place.
Thanks in advance
EDIT/UPDATE:
I have been able to find an option in the Api to 'sync' the drive content using this code:
// try to sync
Drive.DriveApi.requestSync(mGoogleApiClient).setResultCallback(new ResultCallback<com.google.android.gms.common.api.Status>() {
#Override
public void onResult(com.google.android.gms.common.api.Status status) {
if (!status.getStatus().isSuccess()) {
Log.e("SYNCING", "ERROR" + status.getStatusMessage());
} else {
Log.e("SYNCING", "SUCCESS");
// execute async task to list AppFolderContents
new AppFolderContentsAsyncTask(getActivity()).execute();
}
}
});
This works well for 3/4 attempts in quick succession, however I reach a syncing limit and status message:
ERRORSync request rate limit exceeded.
Is there any way to increase the request rate as this is not really desirable if I have to have a app that prompts the user 'please try again later to sync - not sure how long you'll have to wait until you can though!'
SOLUTION - OF SORTS, AND MY THOUGHTS (for what its worth)
The solution that I am going for (after emailing a app dev whose published a drive app that synchronizes without problems) is to use the Drive REST Api, rather than the newer (and Google preferred) Drive API. I tried limiting the 'requestSync' to only when the user navigated to a fragment with the Drive options (rather than every file transaction). However this would probably solve the requestSync rate limit for the most part, but it still could hit that limit. Also if multiple devices are running the app, and linked to the same Drive account were both syncing/uploading/deleting files at the same time in the app then there is a possibility of losing synchronization - maybe a rare scenario, but still possible. I don't feel that making a user wait to sync files is a viable option in terms of user experience or app design.
Curious though - The actual Drive app lets you refresh (requestSync?) as many times as you like. I created 20 folders on the web interface in quick succession, after each folder was created I refreshed the Drive app on my phone and it synced all 20 times. It seems Google understands the importance of synchronization, but chooses to make this quite difficult ensure this in their new Drive API. As already stated uploading files to the cloud storage happens usually instantly (it is queued, however if you have connectivity it happens almost straight away). I would have thought that this is the costly transaction in terms of moving data/updating drive contents, rather than just querying for synchronization of files. However you can keep adding files to your your drive app one at a time, and it uploads them one at a time almost instantly - where as you request sync more than 4 times in 30 seconds and it then fails and you have to 'cool off' for a while.
I'm very new to programming/Android in general - as soon as I learn something new, I realize how little I actually know, so if their is a better solution out there using the Drive API (rather than REST API) I'd very much welcome it.
DriveApi#requestSync will let you request a "sync down" from the server to the device. This way, on the second device you should be able to see the content uploaded from the first device. However, the calls to request sync are rate limited (per-device) to avoid abuse and guarantee a reasonable amount of data usage by the Drive API. You should ideally call request sync only when you need to. There's no way to increase the rate limit.
Regarding upload completion, after committing an upload, you shouldn't use request sync since that won't help you (it only syncs down but not up). The actual upload will happen as soon as possible (based on the device's connectivity and the preferences specified by your app through DrivePreferencesApi, which by default are unrestricted).
If you want to know when the actual upload happened, you can use CompletionEvents. This way you can test your app with more insights on what's actually going on.

Categories

Resources