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.
Related
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.
I am developing a social network app that uses Parse.com as a back-end
I gave users the ability to change their name , email and profile picture (which is a parseFile)
And when users are logged in i gave them the ability to add posts
I add the posts by getting the user email and name by
ParseUser.getCurrentUser()
and saving the returned content to my ParseObject
but now the question is what if the user updated his data which is the profile picture or name or email how to update the post data dynamcly
You're going to have to write some cloud code. https://parse.com/docs/js/guide
I would create an beforeSave trigger for your users, check to see if the relevant fields are 'dirty' (have been changed), and, if they have been, create a new object of a customer class that just has a pointer to that user.
Save a pointer to the user on each post.
Create a background job that runs each day/hour/however often you want to do these updates that goes through all of your custom objects that contains a user, use Query.each() to go through each of those objects, and then do a query for all posts where the user key is equal to the user of the custom object. Then set the name/email fields as appropriate.
Make sure that when you're done, you delete all of the custom objects so that you don't continuously perform this job on more and more objects each time.
Alternatively, you could just add the user pointer to the post, and when you fetch your posts, include the user key, so that gets fetched as well. Then you can read the name/email directly from the user, which will always be dynamic. You have to make sure that your ACLs are set up so that users can't edit all of another user's info or something, though.
I'm using parse as a backend for an android app.What I'm trying to do is to automatically delete data corresponding to a particular user from other tables when that user is deleted from main User table.I am using pointer of user in other tables as a column to identify user.But it isn't working as per what I want to do.Please let me know if anyone here is having knowledge to accomplish this thing.
Thanks :)
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.
I'm pretty sure it's not viable to do what I'd like to based on some initial research, but I figured it couldn't hurt to ask the community of experts here in case someone knows a way.
I'd like to create a custom field for contacts that the user is able to edit from the main Contacts app; however, the user should only be allowed to select from a list of four specific values. A short list of string values would be ideal, but an int with a min/max range would suffice.
I'm interested in knowing if it's possible either way, but also wondering if it make sense to go this route performance wise. More specifically, would it be better to look up a contact (based on a phone number) each time a call or SMS message is received or better to store my own set of data (consisting of name, numbers, and the custom field) and just syncing contact info in a thread every so often? Or syncing contacts the first time the app is run and then registering for changes using ContentObserver?
Here is a similar question with an answer that explains how to add a custom field to a contact.
Thanks in advance.
I don't see the purpose to have your own set of data against contacts stored in your separate database, as you obviously will run into sync issues. You can use the mimetype and store whatever you want against the contact, in the manner you linked.
Whenever you want to looup contacts you can do that by using your custom mimetype. You get the contact id (from ContactsContract.Data.CONTENT_URI), and then you run another query to get the contact details (from ContactsContract.Contacts.CONTENT_URI). Please note these are different tables.
I'd like to create a custom field for contacts that the user is able to edit from the main Contacts app
I don't see that possible, editable from the main app, when you use your custom mimetypes, and you don't have much options here. The main contact app will display only the fields that are in SDK. You can store details against contacts but they won't show up in the inbuilt edit contact screen.