0

On Parse I have users with Facebook profile and Email login profile. So I want to bury for users data in my twitter-like app.

In my "messages" class on Parse I have column "sender" that contains pointers to parse users.

I just want to retrieve and show the name of users in class "messages" contained in the column "sender" which contains pointers to PFUsers of which I need data for keys

  • "first_name"
  • "last_name"
  • "profile_picture"

How can I retrieve their data like name and image in order to show them in a tableview?

these are the declarations of my arrays:

var sendersArray : [String] = []
var picturesArray : [NSData] = []

maybe I could use something like this tuple, but I can't understand how to grab data from pointers

for user in list  {
    
    let firstName = "fist_name"
    let lastName = "last_name"
    let oProfileImage = NSData() //"image_profile" as! NSData
    
    otherUsers.append((oName: firstName, oLastName: lastName, oImageProfle: oProfileImage))
    
}

version - 1:

I started with printing the whole pf object

//******************************************************

func theSearch() {



    let theSearchQuery = PFQuery(className: "Messages")


    theSearchQuery.findObjectsInBackgroundWithBlock({
        (objects : [AnyObject]?, error : NSError?) -> Void in

        for object in objects!  {

            let theName = object.sender!

            print(object)
            print(theName)

            sendersArray.append(theName)

            let profilePicture = object["profile_pic"] as! PFFile
            picturesArray.append(profilePicture)

        }

        self.tableView.reloadData()

    })

}
//*******************************************************

version - 2:

then, found this solution, but still, doesn't

func theSearch() {

    let theSearchQuery = PFQuery(className: "Messages" )
    
     theSearchQuery.includeKey("sender")

    theSearchQuery.findObjectsInBackgroundWithBlock({
        (objects : [AnyObject]?, error : NSError?) -> Void in

        for object in objects!  {

            let theName = object.sender!["first_name"] as? String

            print(object)
            print(theName)

            sendersArray.append(theName)

            let profilePicture = object["profile_pic"] as! PFFile
            picturesArray.append(profilePicture)

        }

        self.tableView.reloadData()

    })

}

errors:

enter image description here

seems to be a problem with sender, maybe I shouldn't use it

enter image description here

thanks in advance

2
  • Your last solution should work. What happens? Commented Nov 15, 2015 at 11:52
  • updated the question with code and errors , problems: can't unwrap correctly the data, the name seems to be not correctly set. Commented Nov 15, 2015 at 22:41

1 Answer 1

1
  let theName = object.objectForKey("sender")!.objectForKey("first_name") as! String

Complete Code:

   func theSearch() {



let theSearchQuery = PFQuery(className: "Messages")

 theSearchQuery.includeKey("sender")
 theSearchQuery.findObjectsInBackgroundWithBlock({
    (objects : [AnyObject]?, error : NSError?) -> Void in

    for object in objects!  {

        let theName = object.objectForKey("sender")!.objectForKey("first_name") as! String

        print(object)
        print(theName)

        self.sendersArray.append(theName)

        let profilePicture = object["profile_picture"] as! PFFile
        self.picturesArray.append(profilePicture)

    }

    self.tableView.reloadData()

   })

}

Also, your picturesArray should be of type PFFile, like this:

 var picturesArray = [PFFile]()

NOT NSData. change that at the top of your class.

-----EDIT------: If you want to retrieve an image from a parse query, do this:

1) at the top of your class, declare the following arrays to store the results:

    // your images will be stored in the file array
    var fileArray = [PFFile]()

  // your first and last names will be stored in String Arrays:
  var firstNameArray = [String]()
  var lastNameArray = [String]()

2) perform the query:

   let query1 = PFQuery(className: "_User")
   query1.orderByDescending("createdAt")
   query1.findObjectsInBackgroundWithBlock({
      (objects : [AnyObject]?, error : NSError?) -> Void in
         if error == nil {
           for x in objects! {
             let firstName = x.objectForKey("first_name") as! String
             let lastName = x.objectForKey("last_name") as! String
             self.firstNameArray.append(firstName)
             self.lastNameArray.append(lastName)

            if x.objectForKey("profile_picture") as? PFFile == nil { 
               print("do nothing cause it's nil")
            }
           else {
              let file:PFFile = x.objectForKey("profile_image") as! PFFile
              self.fileArray.append(file)
            }


            } 
            self.tableView.reloadData()

         }

    })

Note I am using Swift 2 and Xcode 7. Syntax is slightly different in Xcode 6.4 and Swift 1.2.

Sign up to request clarification or add additional context in comments.

16 Comments

// update - the matter is, if user logs in via parse, in User class, there are no data at all for given keys! but will use username Changed the array type. Users login in both via Facebook and Parse itself (in that way, giving email but not an image) so yes, I want to retrive images too, if there are any, so, how could we handle if there are no images for that sender's profile? (infact your code gives me an error regarding unwrapping an optional value)
Could you give me an idea of how to retrive images in your own version? don't know how to convert in UIImage and put in the array. This way, I can mark your answer as complete!
ok so let me try to understand. you want to retrieve images NOT from a pointer, but directly from the class you are querying, and sometimes the column has an image file, and sometimes it doesn't (so sometimes the image file is nil). is that correct? If so, yes I know how to do that, just making sure before I post an answer
yes, I try to explain :) in another query, I retrive from "Messages" class the object, message, date, put them in an array and show on a table view. Now, I'd like to add more info, and in order to keep this simple to me, I'm making this new query. Again in "Messages", I have "sender" column (as you know, is a pointer to users) and "senderNickname". If you login via Facebook, your full name and image are stored in the pointer (that's what I understand, correct me if I'm wrong) so yes, you should understood :)
you want to query the user class for the name and profile picture?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.