I am using parse.com for my survey application, In that I am implementing like mechanism where I have set of two images which users will be able to see and they have to like one of them. which be part of my survey.
Now I am downloading 20 sets per query then asking user click More then i download next 20 sets n so on..
when I query all the 20 sets which user have already votes is getting downloaded again., so how do i stop that ? so I do not get those sets repeated again and again.
Have a look at the Anypic tutorial on parse.com how they use the Activity class to track likes, comments etc. Use this as a template for how to plan your data model as opposed to relational principals.
One possible solution is to store all voted photos in an array on i.e. a voting object, or even the user object, and query for photos that are NOT in this array.
You should store your voting operations in somewhere.
So let me analyze the two possibilities to store your operations:
In your local (in the device): If you decide store them in local, you will retrieve some objects (sets) from the Parse and then you will look for unvoted ones, and probably you will lose some of them, maybe all of them. So it is not feasible.
In the Parse: As i explained, you should store them in Parse.
You can do it using:
Relations. You can take a look at:
https://www.parse.com/docs/relations_guide#top
When retrieving new sets, you should get sets which the current user hasn't voted yet. You can do this with Relational Queries in Parse. You can take a look at the documentation about it:
https://www.parse.com/docs/ios_guide#queries-relational/iOS
Or creating a Join Table.
This would be a custom implementation of a new class where you join your user with a set. Maybe you can store additional info about voting operations, like the time of voting.
Related
I have a collection of Shop, every shop have a subcollection of item.
Item document has a property isAvailable which is a boolean.
Then, I need to put item in the user's shopping cart.
It's important to observe item isAvailable value to inform in real-time that an item is no longer available and auto-remove from all shopping cart.
So I decided to put in the Item object an array of user id and create a duplicated list of all objects at root level of db to simulate an observable shopping cart (I thought it's a good way to structure for this purpose, if you have bettere ideas just tell me).
My problem is: since I duplicate all the subcollections in a single collection and use the same document id, there may be duplicates in the final big collection, is it right?
In short, auto-generate iDs are statistically unique with a good enough probability to consider it all the time. See here.
Also in firestore, the time-based calculation has been removed so the ids are not chronological anymore compared to the real-time database.
Regarding your data structure, I wouldn't recommend duplicating as one of the benefits of firestore is to avoid that, versus real-time database which in some cases you would need to do that.
Also avoid arrays as much as you can and use the object instead of as you can query them.
As I understand, you just want to make sure the items are available. I suggest you do a check when a user wants to proceed to checkout or anytime the page is refreshed and this way you ensure no unavailable product is purchased. That's it.
If you still have a problem, perhaps give me a snapshot of your data rather than explaining, something like
ShopsCollection
- itemDocument
- isAvailable : true
I can't find an efficient way to query Posts(PFObject) or Users(PFUser) classes and also have the isPostLiked(boolean) and isUserFollowed(boolean) included in the results array respectively.
Lets say, I have queried and received 25 Posts from the server. I want to fill in the like heart button with red if I have previously liked this Post. It would be very inefficient to query all the likes of these Posts and see if current user is contained in the results.
Is it possible to write a cloud code function to insert an 'isLiked' field to the query results and return it to the User for instance?
I am open to new strategies since I am stuck here. It is obvious that most of the social apps are having this need as a standard so there must be an effective solution. Thanks
Your best action is to rid yourself of the relational database thinking. It seems to me you have a separate Likes class that tracks which user likes which post.
In the NoSQL space you should focus on your queries when you plan your datamodel. Ask yourself this question:
How do I want to query my data?
In this use case, I'm thinking you might want to
Show how many likes a Post has
Maybe show which users did like the Post
Track whether the current user has liked a certain post
Maybe find all the Posts the current user has liked?
To solve this, I would do the following:
On the Post class, add a column likedby.
On the User class, add a column likedposts.
Both these columns are Array columns
Every time a user likes a post, you add a Pointer to the current user to the likedby array column for the Post AND a pointer to the post to the likedposts array column for the User.
This makes it very easy to
find how many likes a post has (number of elements in likedby)
list all the users that liked the post (using query.includeKey("likedby") on the Post)
check if the current user has already liked the post (if likedby array contains currentuser)
list all the posts a user has liked (using query.includeKey("likedposts") on the User).
Use the same logic for followings.
I have found this post querying random PFObjects in the Parse forums to be quite helpful, but am having trouble implementing it as query for random PFUsers in my database. As a concrete example, suppose there are 10 users in the database, how would I be able to pull two users at random, and after showing to current_user in my app (by "showing" I mean that it will display some attributes of the two users, such as their hometowns), make sure they are not shown again?
My current thoughts are to add an array property to each PFUser called seen_by_current_user, of which contains a list of user ids which are those seen by the current_user, and when querying for random users using PFQuery, it would only query those !seen_by the current_user. Where I am having difficulty is understanding whether this is doable thorough PFQuery, and if so how I could implement it.
Thanks!
The PFQuery whereKey:notContainedIn: method is what you would want to use, the second parameter would be the array of user ids the current user has already seen.
I have an article model and a user model. The user can follow that article, but can't unfollow it. I'd like to keep track of new followers for an article, and to retrieve it for when I want to plot it. (Assume, for now, that plotting is not the problem.)
I'd like every article to have a list of the every date since that article was created. So it will have:
article.newfollowers[1/1/2012] = 35
article.newfollowers[2/1/2012] = 4
Every time a user follows an article I would do
This.newfollowers[Date()]++
Obviously saving such data means that each row in the article database has hundred of attributes (for each date).
How would I save the data within the article model? How do I declare/define such attribute?
If you don't need to query for the dates directly, take a look at ActiveRecord::Store. You'll need to be using Rails 3.2.0 at least, but would prevent you from having to add so many columns.
Another solution might be to use a Redis sorted set, using the date timestamp as the score, and the new followers as the value.
I'm using Parse (www.parse.com) for the backend database for an iPhone app. I'm creating a fitness application and want advice as the best way to structure the classes and relationships.
A few needs for the database:
1)Sets(have attribute or weight and reps)
2)Exercise (a single instance of an Exercise which can contain multiple Sets)
3)Workout (which will be a single instance for a particular workout which will represent 1 single day. No single day can have 2 Workout objects. Can contain multiple Exercise objects.
Then I also need some classes for Routine which is independent from the ones above.
4) Routine can contain ExercseForRoutine objects (which will look the same as Exercise but will just be used for routines, will not be able to add sets to.
5) ExercseForRoutine will be added to Routine, but not related to the actual Exercise objects that will be used when the user enters workout data, this object is just used to create a Routine.
Any advice on tips and how to structure this using Parse would be appreciated
Don't get too hung up on your object model or your data store. If you haven't done so already, generate a list of core use cases and use those to drive your object model. But assume that you will iterate on it until you find a good fit with your domain. The one thing that sounds like it might be a bit fishy about your spec is writing the single day constraint into your Workout class. That doesn't sound like it's essential to the domain and probably will be awkward to code (e.g., what happens if I start my workout at 11:55pm?).