How do I make sure I fetch data from api ONLY ONCE - java

I have an a few cards inflated, I fetch data from a website's api and insert it in the database and get back the first 3 rows with a seeAll option button to add fragmentB with the whole rows inflated from the databse. inside fragmentB I fetch the same from the same website just to make sure it was fetched and inserted in the database. is there a way to check before trying to fetch the api again?
better explanation:
This is the main layout, there are few other card below this one
this is the fragment opened when I press See all
so once the application opens i fetch the api and inflate the main layout, when I add the new fragment I want to check if the link was fetched instead of just fetching it once its created

Without seeing any of your code... :
Save the data within an object you defined such as
private apiData recievedData = null;
Whenever you attempt to recieve data, you'll first check if the object is null.
if(recievedData == null)
{
recievedData = getDataFromAPI();
}
return recievedData; //will return previously retrieved data if not null

It may be better to first check the database and if the record's exists then do not fetch it from the server. Also, is there any reason of again fetching the data in FragmentB when you have already fetched it in FragmentA. Anyways, try to first check the database for the record and if the record doesn't exist then fetch it from API.

Related

Java to postgresSQL. I am using SERIAL for my id column , can I make it replace missing value when I remove row and add a new one?

I am doing Library db , sometimes I need to remove books and add a new one and I dont like how it starts missing lower ids and it just goes one . Because Serial just increade everytime . Is there a way how to do that ? For example different type of column instead of SERIAL
I don't know if that will solve your problem but I suggest that you do updating for all serial ids again.
So If you want it to be (1,2,3,4) so you can use this query :
ALTER SEQUENCE seq RESTART WITH 1;
UPDATE t SET idcolumn=nextval('seq');
source: How to reset sequence in postgres and fill id column with new data?
You can create a service that executes this query inside deleting books service, so after deleting books you will call this service to rearange ids
public void UpdateIds() {
// make a connection with your database and send query above to update ids
}
and in your service where you delete books, I suppose you have a service looks like that
public void deleteBooks(//some books ids to be deleted
){
//some code for deleting books by ids
// call the method above to update ids again
UpdateIds()
}

Save button deletes previous data in firebase

I am new to android studio and firebase. I am trying to save a list of people to firebase like this. Idea is that the logged in user should be able to save information about some people.
String userId = user.getCurrentUser().getUid();
databaseReference.child("users").child(userId).child("savedPersons").child("name").setValue(nameTxt);
databaseReference.child("users").child(userId).child("savedPersons").child("surname").setValue(surnameTxt);
databaseReference.child("users").child(userId).child("savedPersons").child("gender").setValue(genderTxt);
databaseReference.child("users").child(userId).child("savedPersons").child("ageTxt").setValue(ageTxt);
It does not surprise me that it deletes the previous saved person when i save another one but i don't know how to save all of them. I have this in my firebase but i need multiple saved users. How do i do it ?
Firebase screenshot
If you want to save multiple people in a list in the database, you'll want to call push:
String userId = user.getCurrentUser().getUid();
DatabaseReference newRef = databaseReference.child("users").child(userId).child("savedPersons").push(); // 👈
newRef.child("name").setValue(nameTxt);
newRef.child("surname").setValue(surnameTxt);
newRef.child("gender").setValue(genderTxt);
newRef.child("ageTxt").setValue(ageTxt);
This will create a new child node under savedPersons each time you call push(). To learn more on this, see the Firebase documentation on appending data to a list.
Note that calling setValue for each property is wasteful, and may lead to unexpected behavior down the line. I recommend putting all values in a map, and then adding them all with one call to setValue:
Map<String, Object> values = new Map<>();
values.put("name", nameTxt);
values.put("surname", surnameTxt);
values.put("gender", genderTxt);
values.put("ageTxt", ageTxt);
newRef.setValue(values);

How to retrieve values from a node in firebase and display in a recycler view?

- Orders
|-1234567898(phoneno)
|-march13,202117:33:52PM(orderid)
|-name
|-address
|-phoneno
|-orderid
|-1231231231(phoneno)
|-march10,202117:33:52PM(orderid)
|-name
|-address
|-phoneno
|-orderid
|-1212121212(phoneno)
|-march9,20211:33:52PM(orderid)
|-name
|-address
|-phoneno
|-orderid
I need to display all orders in a recycler view with values name, phoneno.
How can i get the phoneno s?
Several issues in one question . It is quite impossible to have a complete answer in one go .I would advise you to create custom class like Order for example with all data mandatory .Then if you just need to display all orders , i don t think the database structure is the best . You could have Orders:order (with id inside the order class) , or orderBy customerID if you need to handle researches

How to implement a Delete Observer in Object Box

I am using ObjectBox as storage in my app which data are coming through socket connection and stored directly. An observer with a paginated query is used in fragment and applies data changes to recyclerview. The problem is how to know if an entry has been deleted from ObjectBox, get notified in the fragment, and remove it from recyclerview.
The basic approach is outlined in the data observer docs: you create a Query on which you can observe to react to data changes:
Query<Task> query = taskBox.query().equal(Task_.complete, false).build();
query.subscribe(subscriptions)
.on(AndroidScheduler.mainThread())
.observer(data -> updateUi(data));

Parse server : Adding Search filter to a Parse ListView in Android

I'm trying to add a search ListView with Parse api. But I could not add can anyone tell me how to add it ? (It is a typical ListView just fetching some text from parse).
Try the parse query adapter. It's an easy adapter for getting parse queries into a list.
If you want to filter a query you have already pulled from the server then you could save all the results to the local datastore and then query that to save the amount of downloads required.
https://github.com/ParsePlatform/ParseUI-Android/wiki/ParseQueryAdapter

Categories

Resources