Querying firebase, without duplication - android

I am new to coding and I am just playing around to build an app where everyone can post new events. So I have already created notes like "user" and "events". The problem I face off is that I dont know how to avoid duplication like for example if two users are posting the excat same event.
I don't want to show them twice in my RecyclerView.
So I have to compare the date, the location and the description, due to this many queries, is the Firebase Realtimedatabase the right "tool"? Or should I use another software.
------/Events
---------/LbwXYVICCl9xd5m..
--------------datum: 10.04.2019
--------------userid: OncDIQis...
--------------location:New York
--------------description:Coldplay

You seem to define two events as being the same based on some of their properties having the same value. In a scenario like that, I'd use those combined properties as the key of the event.
So if you say that two events are the same if they have the same date, location, and description, then you can create the unique key of that event as ${date}_${location}_${description}. For example:
20190410_New York_Coldplay
I've put the date in an easier to search for format, which also filters the .s (since those are not allowed in keys. Now you can store the event under that key:
Events
20190410_New York_Coldplay
datum: 10.04.2019
userid: OncDIQis...
location:New York
description:Coldplay
With the above structure, if another user tries to create an equivalent event, it will get the same key.

Firebase Real-time database is the right tool for you as it updates the database in real time.
At the time of fetching the data from the firebase, you can check whether it is already present in your local database(where you are collecting data fetched from the firebase) or not.
I hope this will help you out.

Related

How to give data access in cloud firestore to another added users in android?

I wanna ask about the concept and logically ways to give another user the privilege to access other's users' data. What I want to do exactly is like this :
Basically, collection 1 contains several Users ID (UID) from authentication, then the user will have their own data collected in collection 2 which contain the data ID.
So, it's like giving access to another user to collaborate with the data like Google Docs Apps where we can add another user to edit our documents. I've been thinking of how to do this, but still, I got stuck.
My question is, how can I possibly do this? cause from what I've read, cloud firestore don't use such a foreign key like MySQL. Thank You
haven't tried something like this but i think this approch overcomes your problem.
modify your structure according to above image. userID collection will contain userIds which are allowed to edit their parent collection.and create firestore rules according to your use to check weather the userId is allowed to edit the Collection or not.
in your case when 'user 2' will have reference to 'collection 2', he/she will try to change data. firebase rule will check if auth.userId is inside the 'collection2.UserIDs' or not and will allow according that.

Firebase database structure - storing user activity in order to customize views and permission

Working with Firebase for the first time and looking for advice of setting up the right structure for my project which is basically an "offers/coupon" type starter project.
The scenario is this:
I have a node containing a list of all offers available to users
This list of offers is displayed to users after successful Firebase authentication
When a user redeems an offer, I want to be able to count/record that activity in their child node under user and hide that offer so that they cannot see it again once used.
My question is what would be the best way to do this given that offers may be added, may expire, or may change at some point in the future. So, in effect, the user should receive the list of most updated offers, minus the ones he/she have used in the past.
a) would it be more effective to have a master list of offers, and then run a cloud/server function to clone this list for each new user an track that way
Firebase Structure 1
or
b) Keep a master list of offers in one node, then track user specific offer usage
Firebase Structure 2
Appreciate your guidance
The second solution is better because you'll save bandwith. This practice is called denormalization and is a common practice when it comes to Firebase. The first solution is not good becase every time you want to display the users you donwload unnecessary data. If you want to read more details about how you can structure a Firebase database in a efficient way, please read this post, Structuring your Firebase Data correctly for a Complex App. Also, you can take a look a this tutorial, Denormalization is normal with the Firebase Database, for a better understanding.
Second solution is much good. Because in first one we are having redundancy of data in our database. And second one obviously removing that cause.
But instead of using true or false because it is only showing you, "it's available or not", so you can use a string type parameter as "expired", "going to expire" and "updated" or whatever sooo. So it. Will be able to trace all you information related to offer for particular user. I think this is your requirement also.
Happy coding.

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.

How to paging query from Firebase using Android FirebaseUI

I have a chat project using Firebase Android SDK for the server. After 3 months, my application has many users and they chat with each other a lot. It takes a long time to load all of any chat list from Firebase. So now, I wonder that how to paging query from Firebase using Android SDK.
I researched this link to find a query command for this problem but fail. I only query a number of the row from first or last. I could not query from random locate in Firebase. Example using skip and take key to query.
If you have any document about that. Please tell me.
Thanks for advance.
Concepts like skip() or take() don't map well to the realtime world of the Firebase Database. That's why pagination will also be challenging to implement. Technically it is possible, but for a good user experience it's important you keep in mind that the data on a page may change while the user sees it.
Back to how you can implement pagination: the startAt() and endAt() methods take a optional second parameter, which is documented as (emphasis mine):
public Query startAt (String value, String key)
Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.
So if you for example have a list of items sorted by date, you can pass in the date and key of the last item on the previous page to get the next page. You'll have to request one extra item (often called the anchor item), but the overhead should be small.
I tried this demo Firebase Paginator and it work for me. Although, it have some issues.

Implement CreatedAt and UpdatedAt timestamp for DynamoDB Android SDK

Need help with DynamoDB.
I am switching back end from Parse.com (because they are retiring parse) to AWS mobile hub.
I want to capture and save the date and time for which a row or item of data is written into my dynamodb table. In parse it is done automatically but not so in dynamodb.
I have searched around on the internet for clues but no solid explanation or example at the moment.
Can someone please point me in the right direction or pls show an example code here on how to implement CreatedAt and UpdatedAt into dynamodb.
Do I get my system time and save it to dynamodb or get server time?
If I need to get server timestamp which AWS server time do I get and how can I implement it?
Thanks a lot.
AWS enables one to Use AWS Lambda with Amazon DynamoDB
This enables you to trigger server-side code based on DynamoDB events. It's way more involved than Parse but would enable you to avoid using app-side dates/times/code to maintain CreatedAt and UpdatedAt values.
Such datetime values need to be Ints or Strings as Datetime is not a JSON type and DynamoDB doesn't go beyond basic JSON field types.
This is tricky for a number of reasons. First if you require strict order of events, then using time can cause a bunch of errors and you might want to look into more advanced distributed systems algorithms like https://en.wikipedia.org/wiki/Lamport_timestamps or https://en.wikipedia.org/wiki/Vector_clock.
If just 'pretty close' is fine for your use case then the main thing you need to keep in mind is that mobile device system time is often incorrect. User's can change the clock, move between time zones, ect... There are a few things you could do.
A) You could have your own server that keeps a central time that you ping
B) Dynamo also returns a date header when you make calls which you could look, but it's returned in the response. However you could at least use it to see if the date on your device is accurate. The SDK some something similar if you see https://github.com/aws/aws-sdk-android/blob/78cdf680115a891a6e1355c56068e2f56e3c5056/aws-android-sdk-core/src/main/java/com/amazonaws/http/AmazonHttpClient.java when Dynamo returns an error if the date in the signature from the request doesn't match the time Dynamo is expecting. This probably isn't the best solution, but should at least give you ideas of possible avenues.
I have decided to capture System.currentTimeMillis() at the time a user clicks the post/save button on my app and save the Epoch time in milliseconds into my DB CreatedAT attribute.
I read here : "One of the most important things to realize is that 2 persons calling System.currentTimeMillis() at the same time should get the same result no matter where they are one the planet" So I intend to generate a random UUID Primary Key (Hash Key) and then save the user's CreatedAT time as the Sort Key.
I am open to any other practical options. I will use this in the mean time for testing and observe the behaviour.

Categories

Resources