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);
Related
I am using FirebaseRecyclerOptions in calling the database, however, I cannot get all of the data in the database. Here is the structure of the database: database structure the yellow underline is the user id (UID) and below is another node that contains the data that I want to retrieve in the RecyclerView.
Here is a snippet of the code
FirebaseRecyclerOptions<RegisterParking> options =
new FirebaseRecyclerOptions.Builder<RegisterParking>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("RegParkingArea"), RegisterParking.class)
.build();
voPListAdapter = new VoPListAdapter(options);
recyclerView.setAdapter(voPListAdapter);
When you're passing the following two arguments to the setQuery() method:
.setQuery(FirebaseDatabase.getInstance().getReference().child("RegParkingArea"), RegisterParking.class)
It means that the adapter expects to render on the screen RegisterParking objects. If you take a closer look at your database schema, under the RegParkingArea node, you can find UIDs and not RegisterParking objects. The objects that you want to display in the RecycerView exist under each UID. So when reading the data from the database, the Firebase-UI library tries to map each child under the above reference into an object of type RegisterParking, which is actually not possible since the UIDs are strings.
So if you're allowed to change the database schema, then you should either denormalize the data, basically copying the data under another node, or change the actual data into:
db-root
|
--- RegParkingArea
|
--- $pushedId
|
--- uid: "4u9h...XrP2"
|
--- //other fields.
What I have basically done, I have removed a level from the database tree. If you'll use this schema, then you can leave the code as it is and everything will work perfectly fine.
One more thing to note is that if you need to get all parking of a particular user, then you can use the following query:
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
Query queryByUid = db.child("RegParkingArea").orderByChild("uid").equalTo(uid);
How can I change multiple documents? I have 2 collections firestore. One (users) is for user account (email, name, etc).
To second collections (dashboard) users can add some message to a board. In this second collection is name, category, time etc.
What I want to do is when someone change his name or faculty in account, it will also change in second collection for each his comment so it will show up to date information
DocumentReference documentReference = fStore.collection("users").document(user.getUid());
Map<String, Object> edited = new HashMap<>();
edited.put("email",email);
edited.put("smallName",StringUtils.unaccent(profileName.getText().toString()).toLowerCase());
edited.put("fullName",profileName.getText().toString());
edited.put("fakulta",mySpinner.getSelectedItem().toString());
documentReference.update(edited).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(EditProfile.this, "Profile data are changed", Toast.LENGTH_SHORT).show();
//startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
Firestore
Is there any way to do it without changing the whole structure of my app?
To update the user names in the comment docs, you'll need to:
Query the collection to find the comments by the user who updated their name. If you stored the UID for each user in the comment document, this would look something like fStore.collection("dashboard").where("uid", "==", "theUidOfTheUserWhoUpdatedTheirName"). If you didn't store their UID, you'll have to query on the old value of their name instead, but the code will be similar.
Loop over the query results and update each document in turn. If you have a lot of these, you may want to read about the fastest way to do this here: What is the fastest way to write a lot of documents to Firestore?
I am having app where users can post ideas and update their profile picture and status message which is connected to firebase
firebase database screenshot here
for instance, if I update status message as mentioned in above image marked in blue, The data which is marked in red duplicates as with new key
firebase database locations
users details are saved in firebase root > users
user post(ideas) are saved in firebase root > ideas and user-ideas
I'm facing a bug when...
Open app
post new Idea
after posting new idea (without closing app) if I go to profile settings and update the status or profile picture the above idea (or very recent one idea is duplicating itself.
here is the code
1) new idea activity class to post by users
'https://pastebin.com/Pzq9mjkD'
2) user profile activity class to change profile picture
'https://pastebin.com/ZmmCkKzP'
push is used to add a new element to the database.
update is used to update an existing element.
String key = mDatabase.child("ideas").push().getKey();
Idea idea = new Idea(date, userId, username, script_name, action, entryPrice, period, target,
stop_loss, script_views, thumbImgSrc);
Map<String, Object> ideaValues = idea.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/ideas/" + key, ideaValues);
childUpdates.put("/user-ideas/" + userId + "/" + key, ideaValues);
mDatabase.updateChildren(childUpdates);
Your code is always generating a new key mDatabase.child("ideas").push().getKey(); then saving that new record.
Instead of generating a new key each time you should update your existing records, why not try dropping the key and just writing to the ideas and user-ideas with the userId key?
childUpdates.put("/ideas/" + userId, ideaValues);
childUpdates.put("/user-ideas/" + userId, ideaValues);
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.
On my User class I have a field that is a list of strings:
#Persistent
private List<String> openIds;
When I create a new user I do this:
User user = new User();
user.openIds.add(openid);
pm.makePersistent(user);
When I break after that last line and look, the openIds contains the openid I put in there.
But, when I later call User user = pm.getObjectById(User.class, id); with the correct id, the openIds field is an empty list.
Anyone know what could cause that?
EDIT: BTW I'm running on the Google App Engine
UPDATE: Looking at the datastore viewer, I can see the openid was correctly stored in the database. So its just not getting it out correctly...
UPDATE 2: Its working fine now. I'm pretty sure I didn't change anything. I think what must have happened is that there was an old version of the user object being pulled from the database. A user object that was put in before I had the code that saves the openid. Once I wiped the database things worked fine.
Not putting that field in the fetch plan ?
Accessing persistent fields directly, rather than going via setters ?