2

I am appending the username column and the image column to two different arrays from parse. I am then putting them into the collection view. I am anticipating that the username in the nameArray corresponds to the imageArray, but majority of the time they are in the wrong order. How do I get them to append into the array in the right order? i.e. User 1 has picture 1, user 2 has picture 2. username array = [User 1, User 2]. image array = [picture 1, picture 2].

func getFriendPicandName(){

    let imagequery = PFQuery(className: "_User")
    imagequery.findObjectsInBackgroundWithBlock {( objects: [AnyObject]?, error: NSError?) -> Void in
       // for object in objects!{
            var user = PFUser.currentUser()
            let relation = user!.relationForKey("Friendship")
            relation.query()!.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]?, error: NSError?) -> Void in
                for object in objects!{
                let userPic = object["ProPic"] as! PFFile
                userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
                    if(error == nil){
                        let image = UIImage(data: imageData!)
                        self.arrayOfFriends.append(image!)
                        print(self.arrayOfFriends)

                    }
                    dispatch_async(dispatch_get_main_queue()) {
                        self.collectionView.reloadData()
                    }
            })
        }

        }
    }



    var query = PFQuery(className: "_User")
    query.findObjectsInBackgroundWithBlock({
        (objects: [AnyObject]?, error: NSError?) -> Void in
        var user = PFUser.currentUser()
            let relations = user!.relationForKey("Friendship")
            relations.query()!.findObjectsInBackgroundWithBlock{
                (objects: [AnyObject]?, error: NSError?) -> Void in
                var objectIDs = objects as! [PFObject]
                for i in 0...(objectIDs.count){
                self.arrayOfFriendsNames.append(objectIDs[i].valueForKey("username") as! String)
                print(self.arrayOfFriendsNames)

            }
                dispatch_async(dispatch_get_main_queue()) {
                    self.collectionView.reloadData()
                }
        }



    })


   }


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return arrayOfFriendsNames.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: friendcellView = collectionView.dequeueReusableCellWithReuseIdentifier("friendcell", forIndexPath: indexPath) as! friendcellView




    cell.friendname.text = arrayOfFriendsNames[indexPath.item]
    cell.friendpic.image =  arrayOfFriends[indexPath.item]
    cell.friendpic.layer.cornerRadius = cell.friendpic.frame.size.width/2;
    cell.friendpic.clipsToBounds = true



    return cell
}
3
  • I think it's better if can get name and pic in the same function. Someone please helps him Commented Nov 4, 2015 at 5:02
  • I updated my code with your suggestion. Is that what you meant? Commented Nov 4, 2015 at 5:23
  • I mean you just need ` var query = PFQuery(className: "_User") query.findObjectsInBackgroundWithBlock({` one time. As you can see, you query the same table. Just different the key "ProPic" and "username", you should put them in the same block let relation = user!.relationForKey("Friendship") relation.query()!.findObjectsInBackgroundWithBlock { Commented Nov 4, 2015 at 5:35

2 Answers 2

2

You should not call your query twice, I would imagine your for loop to look something like this:

for object in objects!{
    let userPic = object["ProPic"] as! PFFile
    userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
    if(error == nil){
        let image = UIImage(data: imageData!)
        self.arrayOfFriends.append(image!) // Add image here
        print(self.arrayOfFriends)

    }

    self.arrayOfFriendsNames.append(object.valueForKey("username") as! String) // Add Name here
}
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know much about swift but I combined you both function into one...i hope it will help you...

func getFriendPic(){

    let imagequery = PFQuery(className: "_User")
    imagequery.findObjectsInBackgroundWithBlock {( objects: [AnyObject]?, error: NSError?) -> Void in
        // for object in objects!{
        var user = PFUser.currentUser()
        let relation = user!.relationForKey("Friendship")
        relation.query()!.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in

            var objectIDs = objects as! [PFObject]
            for i in 0...(objectIDs.count){
                self.arrayOfFriendsNames.append(objectIDs[i].valueForKey("username") as! String)
                print(self.arrayOfFriendsNames)

            }

            for object in objects!{
                let userPic = object["ProPic"] as! PFFile
                userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
                    if(error == nil){
                        let image = UIImage(data: imageData!)
                        self.arrayOfFriends.append(image!)
                        print(self.arrayOfFriends)

                    }
                    dispatch_async(dispatch_get_main_queue()) {
                        self.collectionView.reloadData()
                    }
                })
            }

        }
    }
}

Comments

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.