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 retrive and show the name of users in class "messages" contained in the column "sender" wich contains pointers to PFUsers of which I need data for keys

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

How can I retrive 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

biggreentree
  • 1,633
  • 3
  • 20
  • 35

1 Answers1

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.

jjjjjjjj
  • 4,203
  • 11
  • 53
  • 72
  • // 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) – biggreentree Nov 16 '15 at 01:08
  • 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! – biggreentree Nov 16 '15 at 19:03
  • 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 – jjjjjjjj Nov 16 '15 at 19:15
  • 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 :) – biggreentree Nov 16 '15 at 19:31
  • you want to query the user class for the name and profile picture? – jjjjjjjj Nov 16 '15 at 19:40
  • seems this line `let file:PFFile = x.objectForKey("profile_image") as! PFFile` causes "fatal error: unexpectedly found nil while unwrapping an Optional value" (I'm on Xcode 7.1 and swift 2) – biggreentree Nov 16 '15 at 20:03
  • hmmm i will look back in a project of mine and see what I did. – jjjjjjjj Nov 16 '15 at 21:00
  • replace the line "if x.objectForKey("profile_picture") == nil" with either: x["profile_picture"] as? PFFile == nil or try if x.objectForKey("profile_picture") as? PFFile == nil – jjjjjjjj Nov 16 '15 at 21:04
  • the line x["profile_picture"] as? PFFile == nil worked for me in my project. if neither of those work for you then I'm not sure what it could be :/ – jjjjjjjj Nov 16 '15 at 21:05
  • yes. can you do that overstackoverflow or do you need my email? – jjjjjjjj Nov 16 '15 at 21:14
  • ah ok just give the link then – jjjjjjjj Nov 16 '15 at 21:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95282/discussion-between-joey-and-biggreentree). – jjjjjjjj Nov 16 '15 at 21:40
  • hi joey, maybe I found a solution! but I revolutioned the question, need a bit of help here if you want: http://stackoverflow.com/questions/33755310/how-can-i-add-uiimages-to-my-pffile-array-so-that-if-query-for-a-pffile-images-f – biggreentree Nov 17 '15 at 11:05
  • hi Joey, could you help me here? http://stackoverflow.com/questions/33943624/why-this-function-with-a-query-on-parse-causes-crash-if-else-occurs – biggreentree Nov 26 '15 at 16:51
  • i will try to look at it later, sorry, I have a lot of programming to do myself, I've been very busy lately. i'll try to look tonight though. – jjjjjjjj Nov 26 '15 at 18:42
  • I'll be grateful for all the help you can give, whenever you can :) (let's erase this last messages) – biggreentree Nov 26 '15 at 19:02