Fetch Relational data from parse - android

I m working on parse. There are 2 table:
1. ScoreNode: User, score.
2. User: id, name, image(ParseFile)
Now need a query to fetch all the these info in one go.
ParseQuery<ScoreNode> query = new ParseQuery<ScoreNode>();
query.include("user");
How i can include image file?
query.include("user.image");
And query should be on scorenode.

regarding "scorenode.user"
if that is type pointer -> User object
and User has the image field
Then you should be able to get everything but the image in one query by tagging on '?include=User' to the query on scorenode.
In Rest api its just a parm '?include= $pointer'
dont know syntax in android SDK.
A 2nd call will be needed to get the bitmap image itself. I think you can get the image URL from the first query and then just use that url getting a bitmap.

Related

Display images from Firebase Storage order by date added in Android

I am trying to display images from Firebase's storage in my Android app inside a GridView.
I have done that, however, the images are displaying in an unknown order, and I have noticed that there is a 'Last modified' column inside Firebase's storage when I upload the images to it.
My question is: is there a way that I could sort the images inside the GridView in order to display them according to that date? (for example, the last one added, would have the latest 'Last modified' date and would be viewed first and such...)
This is my code:
val listRef : StorageReference = FirebaseStorage.getInstance().reference.child("images/posts/$userName")
val fileNameList: ArrayList<String> = ArrayList<String>()
listRef.listAll()
.addOnSuccessListener { it ->
it.items.forEach{
fileNameList.add(it.name)
}
gridView?.adapter = ImageRecyclerAdapter(activity, fileNameList,userName)
}
Note: the code is in Kotlin
I have looked everywhere and couldn't find anything that helps.
Any help is appreciated, thank you :)
According to the official documentation regarding StorageReference's listAll() method:
List all items (files) and prefixes (folders) under this StorageReference.
You might not be interested in listing folders within the reference you are pointing to.
That been said, the best option that you have is to store the URLs in a database. Such a database can be either Cloud Firestore or Firebase Realtime Database. This means that each object should have at least two fields, one for the actual URL and one for a timestamp. Please see in my answer from the following post how you can add a timestamp to Firestore:
ServerTimestamp is always null on Firebase Firestore
Is in Java, but you can simply convert it to Kotlin. Once you have all URLs in place, you can create a query and order the URLs according to the date. In Firebase Realtime Database the default order is ASCENDING, but below is how you can reverse the order:
How to arrange firebase database data in ascending or descending order?
While in Firestore, you can simply pass the desired direction to Query's orderBy(String field, Query.Direction direction) method.

Fetching Object value in mongodb through query

I am making a chat app through socket.io in Android through Node.js . I want to store history of message and username which I have already stored successfully in mongodb but I want to fetch message:Object values here.. can someone please help me in writing query.
This query results in the output which you can see in the image.
messagesCollection.find().toArray().then(function (docs){
console.log(JSON.stringify(docs))
I want to retrieve "message" & "senderNickname" value through query. Replies are highly appreciated.
Pass projection operators in a query,
messagesCollection.find({},{_id: 0, "message.message":1,"message.senderNickname":1})
.toArray().then(function (docs){console.log(JSON.stringify(docs))});
make _id: 1 if you want object id as well for each record.

How to retrieve data using single value in Firebase

this is my firebase database details:
Everywhere I have found, they get all details to list view. But here I want to get all details using single value. For example: I want to get user's city, address, and dob based on their first name and print it in plain text. So not all person details at once, only for a single person.
If you want to get the single value then, you should use the row key like this,
val query = FirebaseDatabase.getInstance().reference?.child("Users")?.child("{that row key}")
Or if you want to get the first item in that list you can use limitToFirst api like this,
val query = FirebaseDatabase.getInstance().reference?.child("Users")?.limitToFirst(1)

Android parse.com I can't get second user from data base

I have two tables in data base. One is user ad the second is transaction that have pointer to the first user, pointer to the second user and int. When I try make and display list I have error. I want to display all rows that CurrentUser is in first column:
ParseUser cu = ParseUser.getCurrentUser();
query.whereEqualTo("first", cu);
and list it with firstUser, secondUser and int:
from Adapter:
ParseObject taskObject = mTask.get(position);
ParseUser first = taskObject.getParseUser("first");
holder.firsthp.setText(first.getUsername());
ParseUser second = taskObject.getParseUser("second");
holder.secondhp.setText(second.getUsername()); //this line make error
int ile = taskObject.getInt("ile");
holder.taskhp.setText(Integer.toString(ile));
return convertView;
This is how transakcja table looks: http://i.stack.imgur.com/yh83p.png
When I saving transaction (when transaction table is clear, don't have any records) and immediately read it works but when I logout and login it crashes.
And here is entire code for toDoActivity Task Adapter and hplayout:
I had problems with pasting code here so I pasted it on pastebin
http://pastebin.com/2wtQLJXE
I think I know the problem. When you are calling your query on the transaction table, the two User's are Pointers to ParseObjects, or ParseUsers in this case. You need to ensure that these objects are FETCHED as well by the ParseQuery in order to properly access their data. They are ParseObjects with data from another table, Parse does not automatically retrieve them so you must tell Parse to do so when you need that data.
Looking at ParseQuery documentation for Android we find the include method.
public ParseQuery include(String key)
Include nested ParseObjects for the provided key.
You can use dot notation to specify which fields in the included object that are also fetched.
You want to use this to include columns names to Pointers of ParseObjects so the query fetches them at the same time as fetching the rest of the data from the table, in this case your transaction table.
Add this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("transakcja");
query.whereEqualTo("first", cu);
query.addDescendingOrder("createdAt");
query.include("first");
query.include("second");
The reason first is having no null issues, is it is the Pointer to the current ParseUser logged in, which doesn't need to be fetched as it's data is accessible. The second one is not fetched, therefore adding the include to the query SHOULD fix this :). Make sure to also include the column "first" because I'm sure your future ParseQuery's will not always be between the current user and non-current second user

Android Parse query for data from more than two tables

I am using Parse for one of my android application and struggling for a query to get output.
Application Parse DB has three table which is as described below-
User: user related information with unique object Id
Post: Contains Post related information with unique object Id and UserId(Pointer to user table) who create the post
Like: UserId(Pointer to user table) represent who is Liking the post, PostId(Pointer to post table) which post is liking and unique
object Id
Now I want to make a single compound query for All post for a specific user along-with total number of likes to corresponding post. Please suggest a solution/query for that
When a user creates a post, assign the user objectID to some post identifier property in the post table.
To retrieve all posts from a user, a query like this may help:
query.whereEqualTo("objectID", currentUser.objectID)
If you're just concerned about the like count, rather than each user who has liked the post, you may want to have an Integer value in the post table that is incremented after a user presses the like button on that post.
You should create your own webHook.
Following this exemples here you can easily learn and develop your own stuff, you can even add some triggers to your application!
https://parse.com/docs/cloudcode/guide#cloud-code-cloud-functions
https://parse.com/docs/cloudcode/guide#cloud-code-beforesave-triggers
If User table doesn't have Post<.Relation> column
I suggest you to read about how to join queries.
f.g. make a query1 to get the user, then another query2 to get all posts with whereMatchesQuery('UserColumnInPostTable', query1) function.
Now, you are going to get all posts of a specific user efficiently.
.
If User table have Post<.Relation> column
this is the easiest way
Just get the user object and get the relation object, then get the query, and find in background

Categories

Resources