How to check existing data when using push ID using Firebase? - firebase

I have some problem on checking existing data on firebase. i want to check data date , timeIn and timeOut before inserting data into firebase. The code that i make doesnt work because its check data by push id.
private void addCourtBooking(){
CourtBookingDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if ((dataSnapshot.child(dateinput.getText().toString()).exists() && dataSnapshot.child(timeStartSpinner.getSelectedItem().toString()).exists()) && dataSnapshot.child(timeEndSpinner.getSelectedItem().toString()).exists()) {
Toast.makeText(getApplicationContext(), "Exist", Toast.LENGTH_LONG).show();
}else {
UCourtType = txtCourtType.getText().toString();
UPurpose = sp.getSelectedItem().toString();
UDate = dateinput.getText().toString();
UTimeStart = timeStartSpinner.getSelectedItem().toString();
UTimeEnd = timeEndSpinner.getSelectedItem().toString();
Map CourtBookinginformation = new HashMap();
CourtBookinginformation.put("courtType",UCourtType);
CourtBookinginformation.put("purpose",UPurpose);
CourtBookinginformation.put("date",UDate);
CourtBookinginformation.put("timeIn",UTimeStart);
CourtBookinginformation.put("timeOut",UTimeEnd);
CourtBookingDatabase.setValue(CourtBookinginformation);
Toast.makeText(getApplicationContext(),"Booking Successfull", Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}

If I'm not wrong I understand that you wanna update that field just if the data didn't exist, and if exists, do nothing.
In that case you should use an update call to update the database, in case that your data exists and equals the same data that you wanna push, the database will do nothing:
In case that you wanna avoid the writing in any case if the data already exist,, you should use a transaction. From the documentation:
When working with data that could be corrupted by concurrent modifications, such as incremental counters, you can use a transaction operation. You give this operation two arguments: an update function and an optional completion callback. The update function takes the current state of the data as an argument and returns the new desired state you would like to write. If another client writes to the location before your new value is successfully written, your update function is called again with the new current value, and the write is retried.
Using this, you will be able to read the data, check if it exists and do whatever you need depending if it exists or not.
You can read more about read and write data in Firebase in the next link from the official documentation:
https://firebase.google.com/docs/database/android/read-and-write

Related

Getting firebase data in a stream

I built an application, which gets data from the firebase (realtime db). I did it whith this code, but I want, that I always get the new data. In the internet I found something like in a stream, but I didn't find a manual for that.
Does somebody know how this works?
This is my code:
void readData() {
FirebaseDatabase.instance.reference().child('CHECK').once().then(
(DataSnapshot dataSnapShot) {
print(dataSnapShot.value);
},
);
}
I want to get the data for example every 0.5 seconds
That's not really how Firebase works. But if you want to get the data from the database once right away, and then whenever it is updated, you can use onValue for that.
That'd look something like:
FirebaseDatabase.instance.reference().child('CHECK').onValue.listen((event) {
print(event.snapshot.value);
});
Give it a try: just set up the listener with this code, run the app, and then make a change to the database in the Firebase console. You'll see the data be printed once as soon as you run the app, and then again whenever you make a change.
From what I've read in your comments, you want the function to be executed repeatedly every 0.5 seconds.
A stream is not appropriate for that. However, you can use Timer
#override
void initState() {
super.initState();
timer = Timer.periodic(Duration(seconds: 15), (Timer t) => readData());
}
#override
void dispose() {
timer?.cancel();
super.dispose();
}
Your build() function will be called more than once once Timer.periodic is created.

How to retrieve limited documents in firestore depending on the time they were added(For a chat app)?

Basically I want to align the messages as they are supposed to be in the usual chat app.
UPDATE: Now messages are aligned properly in the recyclerview. But whenever I send the new message it puts them on the top of the other messages. And whenever I go back and come again to that activity messages are arranged properly(even the top ones).
I just want the message which I send to show up at the bottom. Any help will be appreciated.
mLinearLayout.setReverseLayout(true);
Then:
private void loadmessage() {
mFirestore.collection("Users").orderBy("Timestamp", Query.Direction.DESCENDING).limit(10).addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot snapshots,
#Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w("TAG", "listen:error", e);
return;
}
for (DocumentChange dc : snapshots.getDocumentChanges()) {
switch (dc.getType()) {
case ADDED:
Message message = dc.getDocument().toObject(Message.class);
messageList.add(message);
mAdapter.notifyDataSetChanged();
mMessagesList.scrollToPosition(messageList.size()-10);
break;
}
}
}
});
}
You should continue retrieving the information as you are currently doing, and then invert the order on client side, it isn't an expensive task, and trying to do what you want in Firestore is not implemented yet.
After retrieving the list, you can do something like:
Collections.reverse(messageList.clone());
Here is the same case scenario and it has the answer too. So in case anyone looking for the answer.
And This is the github repository for the features of the firestore chat app.

rxJava Observer.onNext not called second time

I am using rxJava to fetch data from the database and show it in a recyclerview. The relevant code is shown below
function updateUI(){
ContactsLab contactsLab = ContactsLab.get(getActivity());
Subscription sub = contactsLab.getContactList().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.toList()
.subscribe(onContactsReceived());
mCompositeSubscription.add(sub);
}
ContactsLab is a singleton that returns an Observable of Contact objects.
onContactsReceived function is shown below
private Observer<List<Contact>> onContactsReceived(){
return new Observer<List<Contact>>() {
#Override
public void onCompleted() {}
#Override
public void onError(Throwable e) {}
#Override
public void onNext(List<Contact> contacts) {
if(mContactsAdapter == null) {
mContactsAdapter = new ContactsAdapter(contacts);
mRecyclerView.setAdapter(mContactsAdapter);
} else{
mContactsAdapter.setContactList(contacts);
mContactsAdapter.notifyDataSetChanged();
}
}
};
}
The updateUI function is called in my fragment onResume but the view is updated only the first time. If I come back to this fragment from any other fragment (having added more items to db), onResume is called, updateUI runs and onContactsReceived also runs but returns immediately without calling onNext or onComplete.
I think this has something to do with the way rxJava handles observables but no idea how to fix it (read about defer but couldn't understand much). Can somebody please help?
Edit:
The getContactList function look like this :
public rx.Observable<Contact> getContactList() {
List<Contact> contacts = new ArrayList<>();
ContactCursorWrapper cursorWrapper = queryContacts(null, null);
try{
cursorWrapper.moveToFirst();
while (!cursorWrapper.isAfterLast()){
contacts.add(cursorWrapper.getContact());
cursorWrapper.moveToNext();
}
} finally {
cursorWrapper.close();
}
return rx.Observable.from(contacts);
}
Basically it queries the database and maps the returned Cursor into my Contact class(which is a POJO). I added the rx.Observable.from to get an observable that was later collated using toList and updated into the adapter.
I used this approach avoid having to call notifyDataSetChanged after getting each item (and call it only once after getting all that).
What's the right approach to minimize the number of notifyDataSetChanged calls and also, refresh each time onResume is called?
Your observable contactsLab.getContactList().toList() has terminated.toList() collects all emissions from a source observable to a list and emits the entire list once the source Observable terminates (see the documentation). You aren't going to observe any more emissions from it.

Gathering data from Firebase asynchronously: when is the data-set complete?

in a Firebase Android app that I'm currently developing I would like to provide an export feature. This feature should allow the user to export a set of data that is stored in Firebase.
My plan is to gather all required data into a intermediate object (datastructure) that can be (re-)used for multiple export types.
I am running into the issue that because of the flat Firebase data structure that I am using (as explained in https://www.firebase.com/docs/android/guide/structuring-data.html), it's difficult to know when all the data required for the export has been collected.
Example: when retrieving all objects that are referenced using 'indices' (name: key, value true), for each of these I set an addListenerForSingleValueEvent listener, but because this returns asynchronous, it's impossible to determine when all the indices are retrieved. This way it's not possible to determine the correct moment to start the export.
Who has best practices for coping with this?
Posting this to show a worked out example of the comment of #FrankvanPuffelen that seems to do the job quite well:
#Override
public void onDataChange(DataSnapshot indexListDataSnapshot) {
final long participantsRequired = indexListDataSnapshot.getChildrenCount();
for (DataSnapshot ds : indexListDataSnapshot.getChildren()) {
DataUtil.getParticipantByKey( mEventKey, ds.getKey() ).addListenerForSingleValueEvent( new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Participant p = dataSnapshot.getValue(Participant.class);
mParticipants.add( p );
if (participantsRequired == mParticipants.size()){
executeExport();
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
mListener.onDataLoadFailure( firebaseError.toException() );
}
});
}
}

How to prevent static variable became null after application holds for long time

I have developed and released one application in market long ago. Now some some users pointed crashes when holding application for long time. Now I identified the reason for the crash, that is I am using a class with static variable and methods to store data (getters and setters). Now I want to replace the static way with any other ways.From my study I got the following suggestions:
shared preferences: I have to store more than 40 variable (strings, int and json arrays and objects), So I think using shared preferences is not a good idea.
SQLite: more than 40 fields are there and I don't need to keep more than one value at a time.I am getting values for fields from different activities. I mean name from one activity , age from another activity, etc So using SQLite also not a good Idea I think.
Application classes: Now I am thinking about using application classes to store these data. Will it loss the data like static variable after hold the app for long time?
Now I replace the static variable with application class . Please let me know that application data also became null after long time?
It may useful to somebody.
Even though I didn't get a solution for my problem, I got the reason for why shouldn't we use application objects to hold the data. Please check the below link
Don't use application object to store data
Normally if you have to keep something in case your Activity gets destroyed you save all these things in onSaveInstanceState and restore them in onCreate or in onRestoreInstanceState
public class MyActivity extends Activity {
int myVariable;
final String ARG_MY_VAR="myvar";
public void onCreate(Bundle savedState) {
if(savedState != null {
myVariable = savedState.getInt(ARG_MY_VAR);
} else {
myVariable = someDefaultValue;
}
public void onSaveInstanceState(Bundle outState) {
outState.putInt(ARG_MY_VAR, myVariable);
super.onSaveInstanceState(outState);
}
}
Here if Android OS destroys your Activity onSaveInstanceState will be called and your important variable will be saved. Then when the user returns to your app again Android OS restores the activity and your variable will be correctly initialized
This does not happen when you call finish() yourself though, it happens only when Android destroys your activity for some reasons (which is quite likely to happen anytime while your app is in background).
First you should overwrite the onSaveInstanceState and onRestoreInstanceState methods in you activity:
#Override
protected void onSaveInstanceState (Bundle outState){
outState.putString("myVariable", myVariable);
// Store all you data inside the bundle
}
#Override
protected void onRestoreInstanceState (Bundle savedInstanceState){
if(savedInstanceState != null){
myVariable = savedInstanceState.getString("myVariable");
// Restore all the variables
}
}
May be try use static variable inside Application space?
public class YourApplication extends Application
{
private static ApplicationBubblick singleton;
public String str;
public static YourApplication getInstance()
{
return singleton;
}
}
And use variable via:
YourApplication.getInstance().str = ...; // set variable
... = YourApplication.getInstance().str; // get variable
This variable will be live until your app will start and stop all services or activities of your app. This is not work when your app crash.

Resources