Querying for Relation Data in a PFQuery Table View Controller in Swift - swift

I am trying to retrieve the the current user's friends by accessing the "friends" column in Parse. The way I set the friends up is by having the user select users in another view that are then added to a Relation called "friends" in their User row in Parse. I can get the users to save as a Relation in Parse but I can't figure out how to actually get those Relationed Users to show up in the Friends Table View. Here is my current code:
override func queryForTable() -> PFQuery {
let currentuser = PFUser.currentUser()
// Start the query object
let query = PFQuery(className: "_User")
// Add a where clause if there is a search criteria
if FriendSearch.text != "" {
query.whereKey("username", containsString: FriendSearch.text)
}
// Order the results
query.orderByDescending("score")
// Return the query object
return query
}
How do I get the current user's related users to appear in the PFTable View Controller? Thanks

You don't create a new query (particularly using the private class name for users). Instead you get the relation from the user and ask the relation for its query. Once you have that you can add additional parameters to it and return it.

I figured out what I did wrong. I needed to make a value for the relation in the current user, then make that a query.
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {
**let currentuser = PFUser.currentUser()?.relationForKey("friends")
// Start the query object
let query = currentuser?.query()**
// Add a where clause if there is a search criteria
if FriendSearch.text != "" {
query!.whereKey("username", containsString: FriendSearch.text)
}
// Order the results
query!.orderByDescending("score")
// Return the qwuery object
return query!
}

Related

Relational query on PFObject

I have a PFObject, Account that contains an array of Users which are subclasses of PFUserss. The User then has a NSDictonary property, allowableApps, that's a NSDictionary of arrays, where they arrays contain PFObjects.
So as a structure:
Account
var users: [User]
which points to....
User
// Each key is an array of AllowApp
var allowableApps: NSMutableDictionary
which points to...
AllowableApp
var appName: String
var appURL: String
var isAllowed: Bool
I'm trying to fetch all of these relations down to AllowableApp in a single query. I've tried using the .includeKey like this:
accountQuery?.includeKey("users")
accountQuery?.includeKey("allowableApps")
which didn't work. I've also tried:
accountQuery?.includeKey("users.allowableApps.appName")
accountQuery?.includeKey("users.allowableApps.appURL")
accountQuery?.includeKey("users.allowableApps.isAllowed")
I try to populate a UITableView with all the AllowableApp objects but I get this error:
Key "appName" has no data. Call fetchIfNeeded before getting its value.
Which I understand, I need to fetch all of them before trying to access the appName property. (which I'm trying to set cellForRowAtIndexPath).
Here is my full query:
let currentUser = User.currentUser()
let accountQuery = Account.query()
accountQuery?.whereKey("primaryUser", equalTo: currentUser!)
accountQuery?.includeKey("users.allowableApps")
accountQuery?.getFirstObjectInBackgroundWithBlock({ (account, error) in
if (error != nil) {
completion(users: nil, error: error)
}
else {
let users = (account as? Account)!.users
completion(users: users, error: nil)
}
})
My thought right now is to just loop through all of the AllowableApp objects in viewDidAppear calling fetchInBackgroundWithBlock. Then once they are all loaded I reload the table data.
This seems realllly messy and a common problem. Is there a more elegant solution that I'm just not seeing?
From what i understand you have the following structure:
Account
Users (Array of User)
AllowsableApps (Array of AllowApps)
First of all change the NSMutableDictionary to Array. NSMutableDictionary is a key-value pairs and in parse you should create one field. So you can use Array of AllowApps and it will do the same effect.
In order to fetch all accounts and users in each of the account and allowable apps per user you need to build the following query:
// You can do it with sub classing if you want
let query = PFQuery(className: "Account")
query.includeKey("users.allowableApps")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
}
Now for your users array. If your users array is users that needs to login the app it's better to inherit from PFUser and not from PFObject because PFUser contains all the logic for handling users in your app.

PFQuery categorizing results into multilevel arrays

I am trying to do a multiple PFQuery and storing the data properly.
My plan is to query for group members in my Group
I am looking for the key member. It gives me an array of objectIds. Then I want to query in my _User class for their picture with key firstImage. Displaying the data is not the problem, just getting it the right way.
The data needs to be stored considering it's groups. This is what i tried so far:
let fetchGroupsQuery = PFQuery(className: "Group")
fetchGroupsQuery.whereKey("member", equalTo: objectID!)
fetchGroupsQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error != nil
{
}
else {
if let objects = objects {
self.memberPhotos.removeAll()
for object in objects {
if let member = object["member"] as? [String]
{
// returns array of objectIds
for user in member
{
// returns single ids
self.groupIds.append(user)
let photoQuery = PFUser.query()
photoQuery?.whereKey("objectId", equalTo: user)
photoQuery?.findObjectsInBackgroundWithBlock({ (results, error) -> Void in
if error != nil {
// ....
}
else {
self.memberPhotos.removeAll()
if let results = results
{
for result in results
{
if result["firstImage"] != nil
{
self.memberPhotos.append(result["firstImage"])
}
}
}
}})
}
}
My idea was to store the data within an array of arrays.
So that I can later go into array[indexPath.row] to get the array I need to loop through in order to get the right group pictures.
Any ideas how to solve this?
I would use two separate queries function:
1) to get their ID's
2) for their pictures.
For the first query: what you need to change?
Your objectID array should be of type of NSMutableArray because in order to fetch their info you should use the constraint whereKey containedIn not whereKey equalTo
and for that constraint you should downcast your NSMutableArray to type AnyObject.
Also change the Type of your groupIds to NSMutableArray, then append your array.
For the Second query:
Since your groupIds Type is already NSMutableArray just cast it to AnyObject,then use the whereKey containedIn
Note: It would be better to download all images before appending your
array. So you have the option of using struct or class(easier to
group data to its owner) then create an array of that data
structure.Where it because easier to populate your UI.

Parse relation query error on swift

I have 2 table on parse.com. i want to make friendship system.
first table User -> username, email ...
Friendship Table -> RequestFrom(Related User Table), RequestTo(Related User Table), status
and i use this code :
let usr = PFObject(className: "User")
let friend = PFObject(className: "Friends")
var relation = friend.relationForKey("RequestFrom")
relation.addObject(PFUser.object())
relation.query().findObjectsInBackgroundWithBlock { (objects , error ) -> Void in
for obje in objects! {
print(obje)
}
}
this code error.
"NSInternalInconsistencyException" with reason "Tried to save an object with a new, unsaved child.":
This is Friends Table
https://www.dropbox.com/s/ywfpl7di4i767o7/Screenshot%202015-12-31%2014.09.40.png?dl=0
User table is classic parse User class.
How can i solve this problem?

Retrieve User info from Parse

I am coding an app and have user upon the user logging in I would like to take them to their dashboard and display info regarding their account. I am trying to display the username in a label but am having issues retrieving it. I am using the following code:
func getData(){
var query: PFQuery = PFQuery(className: "User")
query.whereKey("username", equalTo: "actualUsername")
query.getFirstObjectInBackgroundWithBlock{
(object: PFObject!, error: NSError!) - >Void; in
if object != nil {
NSLog("Success!")
} else {
NSLog("something went wrong")
}
}
I am using swift and xcode7
You can use the currentUser property of the PFUser object. This returns the informations about the user who is currently logged in. Then you can access the username etc:
PFUser.currentUser.username
Your problem is that the class you're querying, "User", doesn't exist. It's actually "_User", though it is more proper to use var query : PFQuery = PFUser.query() (see this answer)
Christian isn't wrong, though. If you have a user signed in, PFUser.currentUser returns the current user. So create a user PFUser variable and assign PFUser.currentUser to it, then you can access the elements using user["username"]

How to access custom user table columns from Parse.com

How should I access columns that I've added to the User table when I have a currentUser object?
I have a PFUser.currentUser() and I want to access the nickname column that I added via the web interface.
Can I use the currentUser to get the data e.g.:
var nickname = PFUser.currentUser()["nickname"] as String
Or do I have to use a user query? e.g.:
var query = PFUser.query()
query.whereKey("username", equalTo:PFUser.currentUser().username)
var user = query.findObjects().first as PFUser
var nickname = user["nickname"]
If you added date to the column locally, then you have to use the first way as you wrote, or if you added date in the browser, or uploaded to parse.com some way, you have to use the second way.
I would like to give my two cents too. First of all, Daniel was right in saying that if you added the date in the browser or uploaded it to parse.com, you need to use the second way. This is an updated answer with iOS 9 and Xcode 7.2:
var query = PFUser.query()
query!.whereKey("username", equalTo:PFUser.currentUser()!.username!)
do {
user = try query!.findObjects().first as! PFUser
} catch {
print("Error finding user")
}
if user?["rankNumber"] as? Int == nil {
user!["rankNumber"] = 0
user!.saveInBackground()
} else {
print(user!["rankNumber"] as! Int)
}
If I did it any other way, xcode would give me an error saying "failing to unwrap optional". I hope this can help someone!