I have a list of comment objects inside of an object. If I want to add a comment without touching the other comments how would I do that? I have used transactions in the past for editing an integer efficiently but I don't know if I can do that with a list. The end goal is for multiple users to be a ble to add comments at the same time without cutting eachother off (overriding their edits).
Here is an example of my database:
Solved it! Super simple. Just had to add push() to the end of my DatabaseRefercence like so. It gives every comment a key.
commentRef = pollRef.child("comments").push();
commentRef.setValue(comment);
Related
I'm a beginner in developing android
I create a news feed where you can comment on posts, to do this I use firebase as a database and I use FirebaseRecyclerAdapter and FirebaseRecyclerOptions
I want the user to click on the "Comment" button of the first RecyclerView
The second RecyclerView displays the comments according to the post clicked.
I have no idea how to do this, knowing that I tried by capturing the position of the post.
Thank you. Thank you.
The first thing you'll need to do is record what pot the user clicked on. If you're using the Realtime Database, I recommend doing that by recording the key of the item that they clicked on. For some hints on how to do that, see:
Retrieving the Firebase key from onItemClick in FirebaseListAdapter
How to get obj key from FirebaseListAdapter on Item Click. FirebaseUI
Next up, you need to pass that key to the second recycler view somehow.
If the two recycler views are in the same activity, you can use a member field to store the comment that was clicked.
If the two recycler views are in separate activities, you can store the key in Android's shared preferences. For more on this data transfer mechanism, see:
SharedPreferences in the Android reference documentation
How to use SharedPreferences in Android to store, fetch and edit values
Finally, you'll need to use the key to load the comments for that specific post. This depends on your exact data structure, but I recommend starting with the documentation on working with lists of data or FirebaseUI, similar to what you likely already have.
Suppose we would like to retrieve 15 random children from questions node having this database structured as below:
1. The first (intuitive and discussed) way of retrieving random children from Firebase is to retrieve the whole required parent node (questions as dataSnapshot) and then select some random children on the client-side. This method has been pointed out in many posts, like in this one here .
Obviously, this method has its downsides; for example when querying through a large sized parent node (e.g. over 10.000 children) retrieving such an amount every time would result in a huge bandwidth usage as well as a client side burden. (when we actually require only a small amount of children)
2. Moving on: another approach, as described here which uses an iterator somehow bypasses the whole client side burden, yet the huge bandwidth usage could still occur as we download the whole parent node every time.
3. An interesting approach is described in Tom's answer in this firebase discussion which proposes:
A hacky way of doing this would be to generate a random key and do a query with startAt().limit(1). I have a feeling this could hurt the performance of your firebase though, so this should not be an operation you perform often. We don't have a real random sample function.
This solution actually sounds pretty good, yet I am not sure how it would indeed impact my Firebase.
4. Another silly solution could actually be naming the question ids manually, so to speak, from 0 to N, therefore handling the random group of ids on the client side and retrieving the questions spot-on by knowing the actual name of nodes.
5. And lastly, I have come up with the following solution to which I ask if is more or less viable than the ones presented above: creating another parent containing the question ids only and when needed, one should retrieve this parent which is much "lighter" than questions parent . From there, I would have the specific random ids and I would only need to snipe for those children. To better understand my meaning, please check the below picture:
Now, from this method arises the following issue: is assigning (let's say) 15 eventListeners good practice? Could this actually slow up things? (Note: this applies to methods 3 and 4 as well)
And ultimately, which method is actually the optimal one when querying from a large database for some random children?
You can use the classic solution as I explained in this answer but if you are afraid of getting huge amount of data then use instead 15 listeners. There is nothing wrong in using listeners as long as you remove them according to the life-cycle of your activity. So, IMHO go ahead with 15 listeners.
We have 2 case here
case 1
If you want to grab all details of the random ids at once, then I suggest 1 listener to the parent node (get value of datasnapshot using pojo class).
case 2
If you want to get the details independently upon request then you will have to attach a listener to each (random id) that you want.
Concerning performance
Try to use only Listener For Single Value Events as they listen one time and then stop (better for performance).
Dont use Value Event Listener (because these listeners keep checking for changes and therefore bad performance as listeners increase).
EDIT
lets say you listened to (questions_ids) node, now you have access to the random id keys, store them in a String variable, and then inside the same listener add another listener to (questions) pointing to the id that you want to grab details
//first listen to question ids ref (the one with 15 ids)
question_ids_ref.addListenerForSingleValueEvent(...{
//grab the key of each (random id) and store in variable
String random_01=......;
//run another listener this time to questions ref
questions_ref.child(random_01).addListenerForSingleValueEvent(..{
//get details of random_01 and so on....
});
});
I have a parseobject which consists of many objects, notably an array of consisting of ParseUser pointers.
When an individual clicks a button, the array should remove a certain User.
I don't get how to do this,
I have tried:
mRideEdit.removeAll("Participant", (Collection) childuser);
Where mRideEdit is my ParseClass, Participant is the array consisting of ParseUsers, and childuser is the user I want to remove
Please help,
I've recently faced the same problem.
It originates from how the ParseObjects (including ParseUser) are saved on the server.
If you look at the console, you'll see it is actually an array of strings created from the JSONObject toString method.
The simple answer is - You're using it wrong.
There is no sense in saving objects in such a way (though it's very intuitive).
As you probably noticed - you don't receive them as objects with the getJSONArray method either and have to get them from their objectIds.
The best way to do this is using relations.
If you're set on avoiding relations, what I'd suggest is saving them as strings from the get go (objectId). That way add, addUnique and removeAll will work just fine.
Hope this helps.
I have a Fragment, and once the user presses OK, an Item is added to my database and its ID is added to the ArrayAdapter. Immediately after, the adapter tries to draw the view, but the first time it tries to get its attributes, it returns a null HashMap (it gets drawn properly the following times).
Is there a way to make sure the item is in the table before trying to get its attributes?
Even putting the attribute retrieval into a while loop until it returns a not-null HashMap doesn't work so it doesn't look to be an issue of time.
You need to do Select or GetAttributes with ConsistentRead=true as Amazon SimpleDB supports two read consistency options: eventually consistent read and consistent read. Eventually consistent read is default. For more detail please refer doc. link
Try using AsynTask.
Add item to database in doInBackground.
Read it in postExecute.
You are done.
I have problem, as you can see I want to delete all objects in corona. I'm looking for one function that will remove all of them.
I can't find it, I'm not sure if it's even possible.
I'd like it to work like this:
Display all objects
Wait for event
When you touch, then remove all objects from 1 and go back to it.
Possible or not?
I'm not aware of one function that does that. But what you can do is every time you create an object you put it in a table. Then when you want to remove them all you iterate over the table and call removeSelf on each. After the loop, you reset the table. See my answer to remove all spawned coins.