qwerty567111
qwerty567111

Reputation: 3

Struggling to append arrays across Parse classes

Hi guys I'm trying to build a simple swipe app to like and dislike uploaded photos. I'm struggling with adding the likes/dislikes to Parse the way that I want them to. I've tried two ways so far:

Ideally I want the users who have liked/disliked the photo in a single array so I can query this later. I don't have a great understanding of Parse, it's my first time using it so any help will be massively appreciated.

Here is the code I'm using when an image is swiped (adding to Post class):

       if gesture.state == UIGestureRecognizerState.Ended {

        var likedOrDisliked = ""

        if label.center.x < 100 {

            print("Dislike")
            likedOrDisliked = "disliked"

        } else if label.center.x > self.view.bounds.width - 100 {

            print("Like")
            likedOrDisliked = "liked"

        }

        if likedOrDisliked != ""{

            var post = PFObject(className: "Post")

            post.addUniqueObjectsFromArray([(PFUser.currentUser()?.objectId!)!], forKey: likedOrDisliked)
            post.saveInBackground()


        }

This is the snippet of when I try adding to User class:

PFUser.currentUser()?.addUniqueObjectsFromArray([displayedUserID], forKey: likedOrDisliked)
            do {

                try PFUser.currentUser()?.save()

            } catch {

            }

Here is what happens in the dashboard, new rows created

Upvotes: 0

Views: 65

Answers (2)

qwerty567111
qwerty567111

Reputation: 3

I've tried a lot of things and eventually something stuck, the desired effect was achieved through (added the current user to the liked or disliked fields) :

            if likedOrDisliked != ""{

            var post = PFQuery(className: "Post")


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

                if error == nil {
                    if let objects = objects {
                        for object in objects {
                            var objId = object["objectId"]
                            var query = PFQuery(className: "Post")

                            query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

                                if error == nil {

                                    object.addUniqueObjectsFromArray([(PFUser.currentUser()?.objectId)!], forKey: likedOrDisliked)

                                    object.saveInBackground()

                                }

                            })
                        }
                    }
                }

            })

Upvotes: 0

Kamini
Kamini

Reputation: 146

What you wanted is to update the actual Post with the like/dislike user

  • Create a Post (This part you have not explained but i am show a simple assumption - pseuodo code)
var post = PFObject(class:"Post")
post["image"] = PFFile(image)
post.save()
  • Next you show the image on screen by getting the image from the post When the user dislikes/likes

    you add the current PFUser to the liked/disliked column and save back the object.

let arrayMut = NSMutableArray()
var array = NSArray()
if let arrayData = post.objectForKey("likedUser") as? NSArray {
    array  =  arrayData
}

loop through now the array to find if current user is there.. if not find .. add current PFUser

arrayMut.addObject(PFUser.currentUser().objectId);
post.setObject(arrayMut, forKey: "likedUser")
post.save()

Upvotes: 4

Related Questions