I am building an iOS in Swift using Parse.com as my backend.
I have a table of objects: car, and each car can be owned by multiple users, so I have a Car table with the column owners which is a PRRelation of the _User table.
I am displaying all the cars in a TableView and want to determine (for each object) whether the PFUser.currentUser() is in the Relation of _User objects for each car.
Is there a way of doing this without creating a query which then makes a request to the Parse server? Doing that seems very inefficient to have to check again for each object, and would make a large number of Parse database calls which would make me hit the call limit quite quickly if multiple people are using the app...
So is there a way to simply do something like:
if carObject["owners].contains(PFUser.currentUser()) {
println("the current user is an owner of this car")
}
Might it be possible to run a query of all cars, and then another query of all the cars with a whereKey restriction on the students column and then comparing queries? How could I compare the queries?
Have you created your car class in your app? You can download all your car objects from parse at once, put them in an [Car] and then you'll have all the relational data as well.
I'm not 100% sure but you may need to use parsequery.includeKey("users") when you query parse so it also includes the parse user. User's being an attribute of Car.
Related
I'm using the Parse iOS SDK to pull data from my Parse backend. What I would like todo is create a compound query, so a query with a subquery as a where condition, but also have it bubble up properties from the subquery.
For example, lets say I have an object called car and I have an object called dealership. Now I want to pull the dealership that a particular car is located at based on some query parameters. This is where the compound query comes in.
I know how to create the compound query to only return cars that belong to a particular dealership, but how would I return the "name" property from the dealership object on my car object?
The idea here is to mimic a left join in SQL to return data from a joined table.
Any ideas how one would do this?
With the impending death of Parse, I'm re-writing an app using CloudKit. Suppose I'm making a recipe scheduling app. I want a given schedule to contain an ordered list of recipes, and recipes can be in multiple schedules. So I have:
a Schedule object, which has a one-to-many relationship to ScheduleItem objects called "scheduleItems"
a ScheduleItem object has a dayOfWeek field and a one-to-one relationship with a Recipe object called "recipe"
a Recipe object has some information, including its name and ingredients (more relationships)
Assume I have the Schedule object, and I want to get all of its ScheduleItems AND their associated Recipes in one single query. In Parse, I could set up this query:
PFQuery *query = [PFQuery queryWithClassName:#"Schedule"];
[query includeKey:#"scheduleItems.recipe"];
And that query would fetch all the scheduleItems along with all their recipes, allowing me to avoid having to perform multiple network requests.
Is there a way to do this in CloudKit? I see that I can use a CKFetchRecordsOperation to fetch multiple records at once given their recordIDs, but I wouldn't know the record IDs of the recipes until I had already fetched the scheduleItems, thus still necessitating a second network request.
CloudKit is not a relational database. It's a key-value store. There is no functionality to query multiple recordType's in one query. There is also no functionality for aggregation queries. In your case since it's a one to one relationship you could limit it to 2 queries by adding a CKReference in your Recipe to the Schedule. So then if you have a schedule, you could do one query to get all related ScheduleItem's and an other query to get all related Recipe's
There is a way in the coming version ('16): you can define parent relations and then fetching the parent records will result in the children being fetched as well. This is covered in the WWDC session 'What's New in CloudKit' (2016): https://developer.apple.com/videos/play/wwdc2016/226/
Correction, the parent reference introduced in '16 is not to make it possible to do a single fetch, but rather to automatically share all descendants, when using the new Sharing part of the API. So the answer is no there is still no way but when sharing records, it is a bit less onerous.
I have a custom Parse class that represents an event. The event has 3 PFRelation columns that represent users that have been invited to the event, users that have accepted the event and users that have declined the event.
Is there any way to count the total number of people in each PFRelation without querying the class again?
You would need to query each relation to get the count (which you can do without pulling all of the objects back and counting them).
A better idea is to use cloud code, either to update a variable whenever a relationship is changed (a save hook) to store the count or so that you can query all of the counts for one object in a single request.
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.
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.