How to get most popular post? - android

I am creating an app in which users can make posts and other users can like and comment over these posts.To achieve this facility i am using tables likes POSTS, LIKES, COMMENTS.(I am using parse.com database to store these table).
POSTS have fields like postId, content, userWhoMadeThePost, dateOfPosting
LIKES contains filelds as id, postOnWhichLikeWasMade (foreign key to POSTS(postId)), userWhoLiked, dateofLike
COMMENTS have id, content, postOnWhichCommentWasMade(foreign key to POSTS(postId)), userWhoCommented, dateOfComment
I want to retrieve most popular posts (posts with most no of likes and comments).
First way to do this is count no of post and likes for each post each time whenever a request is made for popular posts but this can become very time consuming if
there are millions of posts.
Other way to do this one way is to include 'noOfLikes' and 'noOfComments' in POSTS table, so in order to get popular posts I will have to access only post table, but the problem with this approach is whenever a user makes a comment over a post then I will have to increment 'noOfComments' in POSTS table as well as make a entry in COMMENTS table, problem starts when the increment is successfully made to POST table and before making change to the COMMENTS table connection is lost. In that case POST and COMMENTS table would be showing wrong data.
How to do this?

This would be simple, if you had control over the database, but doing this with Parse means that you have two main problems (one of which you already mentioned):
As far as I can tell, Parse does not allow grouping in queries. That means that (with your schema) it's actually impossible to get the most liked/commented posts without retrieving all of them. This is because you can't actually dynamically add a new column with the number of likes/comments like you would in a standard SQL query (using JOIN and GROUP BY).
You can't update more than one object within one transaction, so there is the possibility that a user could add a comment but the number of comments in the post object would not change.
That being said I think (at least if you insist on using parse) you must add the 'noOfLikes' and 'noOfComments' columns to the Post object. Not only does it provide the only viable solution for retrieving N top posts (without getting all of them), but the actual risk of inconsistencies is pretty low in my opinion. You can always catch network (or other) exceptions and temporarely store the update requests locally and retry them later.

Related

Filter data in Firebase by follows

I have a simple Firebase database:
I would like to create an Android app where everybody can posts short texts. However, on the main screen I would like to show only those posts from other users who I followed. On the example, this means that if I logged in like Mrs. Nobody into the app, I can see only the first post created by Mr. Nobody.
Is that possible? (I tried to receive every posts in "posts" and after that I could show only posts what are relevant for me, but data traffic was really high in this case)
I have another idea, but I try to find a more efficient one. My idea is to receive every relations from "relations" where follow_uid is mine. After that, I iterate through all the elements and I download those posts where uid is equals with the uid from "relations"

How to model a timeline that automatically adds post from every follower on firebase database

I am building a social app, I have a database on firebase that is structured like this:
tJWRbfqUUbSQn5eI8ZS5vPG9znr1
bio:
coverPhotoUrl:
email:
firstname:
imgUrl:
lastname:
name:
phone:
posts
-KPN0b7QmNp8AjYy4yFl
-KPXmtvZdiQ--QjZ-j3m
-KPc7PpDkmFNU9JjlIJP
-KPc7PptOulQdrpsrGEp
-KPc7Pq6JC7bjt8-sRFU
-KPc7PqJ2651EIQWXIv6
the top level key is th user id, so i have that for every user, each user has posts too, inside the posts node, i have comments, pushed in the exact the same way as posts.
My question is that I am a bit curious about how i am going to model a Newsfeed, right now A Newsfeed to me is a list of posts from you and your everyone you are following in the exact order of the time they were posted while a timeline is just posts from the said user in that order too.
I have correctly done the timeline by just iterating over posts from the user
I have tried to do the Newsfeed part in two ways, which has bugs and I think is not very efficient
Since every user maintains a list of his followers in his own node, i made it such that anytime a user makes a post, he not only writes in his own 'posts node' but also in every other person following him's 'Newsfeed' node
Cons of this method
a. in the event of an unfollow, i am not able to remove user A's posts from User B's timeline
b. i am giving every user access to write in every other person's node, which as i have read, is not a very good practice.
Maintain the url of User A's post node in user B's Timeline node, which would solve the con B of method 1.
Cons of this method
a. I dont know how to implement it.
I dont know if there is any textbook way to do this(of course there has to be, there is only so much social network platforms out there), if there is i want to know, also if you could point me to how(not help me to) implement my method 2, i will very much appreciate it.
Also, i was following this similar question
which does not completely answer my question but looks like a very interesting approach
I think the solution you are looking for is referred to as client-side fan-out. This Firebase Blog post provides a great explanation and will point you in the right direction.
Client-side fan-out for data consistency
It looks like you need a new datastructure for keeping score who is following who. Use that information to build your newsfeed. That way there is no need for one user to write "into" another user. Every posting is only stored once for the user why created the post. If the follow and unfollow changes, you can simply recalculate on the fly which posts to include in a newsfeed

How to know when to insert into my database?

I am trying to figure out how I execute my database operations efficiently and effectively. I have it so that when a user logs in, I use the Facebook API and grab details regarding the user's friends and place them into my database. This is done in a method called createFriend. However, if I login a second time, I get an error because every friend has a unique user id. Therefore, if I log in the second time, my code calls the createFriend method yet again and tries to insert the same friend that was already inserted the first time I logged in, and I get a unique column violation.
As a result, I don't know when I should call the createFriend method. I thought of 2 solutions to fixing my problem:
I looked at this link to see if I could check each row and if that row exists, only then I do the insert. I found: SQLiteDatabase: Insert only if the value does not exist (not via raw SQL command). It just doesn't seem very efficient to check every single time I want to insert a friend, to see if the row exists, and if not, I don't insert, otherwise I do. I have to do this select statement for every friend the user has.
The other method I was thinking of was this: I can call the Facebook API and check if the number of friends returned from the Facebook API is equal to the number of rows in my local database. If so, I skip calling createFriend all together. If not, I can just remove the entire database and reinsert all the friends again. However, this doesn't seem to work well because there is the chance that the Facebook API can keep returning the same number of friends as are in my database, BUT those friends returned could be different people than what the database has. Therefore, the database won't update because the number of friends is still the same, but I would want it to update because I would like to insert those potential new friends into the database.
Can anyone please point me in the right direction on where I can solve this problem? Thank you!
You can update on duplicate key.
Have a look at this for reference,
SQLite UPSERT - ON DUPLICATE KEY UPDATE
It just doesn't seem very efficient to check every single time I want to insert a friend, to see if the row exists
You are jumping to conclusions.
Guesses are likely to be wrong; you do not know how fast a query is until you have actually measured it.
And when you have a UNIQUE constraint, the database searches for a matching row anyway, so there will not be any additional I/O.
In any case, make your algorithm correct before thinking about optimizing it.
And if you want to ignore friends that already exist in the database, just use INSERT OR IGNORE.

Android RSS reader app SQLite DB schema

this is my first question so I hope I wont mess anything :)
I'm working on an assignment for my university class. I'm implementing Android RSS reader app. All the parsing and UI code is done but I have some problems with SQLite DB schema. I need to store two data sets - blogs that user subscribes and posts for each blog.
Blog dataset contains: blogTitle, blogUrl and rssUrl
Post dataset contains: postTitle, postUrl, postPublicationDate and two booleans (isRead and isFavourite)
There is list of all posts (sorted by postPublicationDate)
There is separate list of all favourite posts
User is able to set post as fav or "un fav" it (from both lists)
After reading post (opening it in separate Activity) post is marked as "read" - with different color on the list - after user leaves app and goes back after some time read posts are not shown (or removed from db - not sure which way is better to implement it)
Posts set as "favourite" are shown in "favourite posts" list even when already read (so they are stored permanently for as long as user removes them manually)
I think it would be wise to limit number of posts stored for each blog (again, not sure if it's necessary) and remove oldest posts (lets say the limit will be 40)
There is "blogs" list which shows all subscribed blogs (with number of unread items)
I implemented it already but I think my implementation is a little bit "hacky". All the functionality above is my own design so feel free to point out some newbie mistakes (I'm not very experienced with databases and programming in general). I hope someone will help, cause I have short deadline for this assignment... :)

Delete Parse Table Row based on Username

Based on the parse AnyWall demo for android, and app that shows messages at certain locations, I made a similar app for food, essentially showing what the user is eating. I can successfully post to the Parse table and show the location of the user and what they are eating on a map. However, what I would like to do is when the user creates a new post, their old post gets deleted and their new post is added to the table. Essentially, any given user can only have 1 post on a map at a time. I have seen the deleteInBackground method, but cannot seem to find the table to specify and a specific username to specify. How would I go about doing this on Android? Thanks.
You can use username as column and you can write before save method on Parse cloud for your table. So when you want add a new record your before save method can detect the duplicate entry on your table and delete it. I hope I understand your question.
Regards.

Categories

Resources