Skip to content

PFQuery pointing to PFUser object failing to fetch data. #3325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
HackShitUp opened this issue Jan 5, 2017 · 8 comments
Closed

PFQuery pointing to PFUser object failing to fetch data. #3325

HackShitUp opened this issue Jan 5, 2017 · 8 comments

Comments

@HackShitUp
Copy link

HackShitUp commented Jan 5, 2017

The below code fetches the user's object from Parse Class (soon to be MongoDB Collection) titled "Newsfeeds" where an object can hold a pointer to the PFUser class. The following code's query FETCHES the user's objects but cannot recognize the subscript syntax and in turn, DOES NOT append the user's object to the PFObject array:

`
// Query Current User's Friends
func queryFriends() {

let fFriends = PFQuery(className: "FriendMe")
fFriends.whereKey("endFriend", equalTo: PFUser.current()!)
fFriends.whereKey("frontFriend", notEqualTo: PFUser.current()!)

let eFriends = PFQuery(className: "FriendMe")
eFriends.whereKey("frontFriend", equalTo: PFUser.current()!)
eFriends.whereKey("endFriend", notEqualTo: PFUser.current()!)

let friends = PFQuery.orQuery(withSubqueries: [eFriends, fFriends])
friends.includeKeys(["frontFriend", "endFriend"])
friends.whereKey("isFriends", equalTo: true)
friends.findObjectsInBackground(block: { (
    objects: [PFObject]?, error: Error?) in
    if error == nil {
        
        // Clear array
        self.friends.removeAll(keepingCapacity: false)
        
        // First, append Current User
        self.friends.append(PFUser.current()!)
        
        for object in objects! {
            
            if object["frontFriend"] as! PFUser == PFUser.current()! {
                self.friends.append(object["endFriend"] as! PFUser)
            }
            
            if object["endFriend"] as! PFUser == PFUser.current()! {
                self.friends.append(object["frontFriend"] as! PFUser)
            }
        }
        
        //
        // ISSUE: 
        // CANNOT APPEND FRIENDS; PFUser PFObjects
        // Which in turn, cannot fetch the correct posts in the news feeds for the following
        // .whereKey("byUser", containedIn: self.friends) 
        //
        print("Friends Count: \(self.friends.count)")
        print("\nFRIENDS DATA:\n\(self.friends)\n")
        
        // Newsfeeds
        let newsfeeds = PFQuery(className: "Newsfeeds")
        newsfeeds.includeKeys(["byUser","pointObject","toUser"])
        newsfeeds.whereKey("contentType", containedIn: self.friendsType)
        newsfeeds.whereKey("byUser", containedIn: self.friends)
        newsfeeds.order(byDescending: "createdAt")
        newsfeeds.limit = self.page
        newsfeeds.findObjectsInBackground(block: {
            (objects: [PFObject]?, error: Error?) in
            if error == nil {
                
                // Dismiss
                SVProgressHUD.dismiss()
                
                // Clear array
                self.friendsContent.removeAll(keepingCapacity: false)
                
                for object in objects! {
                    self.friendsContent.append(object)
                }
                
                
                // Set DZN
                if self.friendsContent.count == 0 {
                    self.tableView!.emptyDataSetSource = self
                    self.tableView!.emptyDataSetDelegate = self
                }
                
                
                print("Friends feed count: \(self.friendsContent.count)")
                
            } else {
                print(error?.localizedDescription as Any)
                
                
                // Dismiss
                SVProgressHUD.dismiss()
                
            }
            
            // Reload data
            self.tableView!.reloadData()
        })
        
        
    } else {
        print(error?.localizedDescription as Any)
        
        // Dismiss
        SVProgressHUD.dismiss()
    }
})

}
`

The terminal outputs the user's object correctly being fetched, and even the terminal output is 100% fine. Why can't it recognize the subscript? I've also tried this with the .object(forKey: "") protocol and it won't work for the current app using Parse.com (I have not yet migrated it; testing it out before I replicate it with the actual data).

@natanrolnik
Copy link
Contributor

@HackShitUp is this working when you point the SDK to Parse.com?

@dstarke
Copy link
Contributor

dstarke commented Jan 5, 2017

When you check whether to append the object, shouldn't you be checking the object ids rather than using an object equality test?

@HackShitUp
Copy link
Author

@natanrolnik yes it works with Parse.com
@dstarke to minimize the number of queries, I append the object itself to handle some of the user's object's properties later in the app session.

@HackShitUp
Copy link
Author

@dstarke chiefly because I haven't enabled the local datastore and don't plan to

@dstarke
Copy link
Contributor

dstarke commented Jan 5, 2017

My point was that object["frontFriend"] as! PFUser == PFUser.current()! can return false for two objects which this code probably wants to consider the same. If you are finding things are not getting appended to self.friends, that could be why.

@HackShitUp
Copy link
Author

HackShitUp commented Jan 6, 2017

@dstarke Hmmm why is that? And I've figured out how to fix this but it won't work for the version in the app store that points to Parse.com @natanrolnik. I want to fix this so that the transition occurs flawlessly; the next of my problems is actually figuring out how to save video files to the app...

@dstarke
Copy link
Contributor

dstarke commented Jan 6, 2017

I don't think that PFUser or PFObject defines an isEqual() function, which means that == tests on the object's identity, which is based on it's memory address. What you want here is a test as to whether the two objects are the same Parse object. It is possible to have two objects that are not identical, but which represent the same parse object. The PFUser object that you get from your query results might not be the exact same one you get from PFUser.current(), even if they have the same ID.

@HackShitUp
Copy link
Author

HackShitUp commented Jan 6, 2017

@dstarke Alright I just tested this out, and it looks like the following syntax, before appending it to the array works:

if (object.object(forKey: "frontFriend") as! PFUser).objectId! != PFUser.current()!.objectId! { self.friends.append(object.object(forKey: "frontFriend") as! PFUser) } else { self.friends.append(object.object(forKey: "endFriend") as! PFUser) }

Thanks @dstarke for the clarity!
:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants