使用parse.com在iOS中发布评级系统 - upvote / downvote

时间:2015-06-19 19:43:38

标签: ios swift parse-platform

简单:我希望帖子(或图片)的评分系统与Yik Yak或9gag的工作方式相同 - 用户只能在帖子后进行upvote或downvote 。我不想将这些数据存储在设备上,但是所有内容都存放在parse.com上。我还想计算评论。理想情况下,只需要在parse.com的一个查询中执行所有操作

我目前正在使用此代码,但我不认为这是一个很好的方法,因为对于每个图像,它都会创建一个新的查询。目前我在Parse.com用户,图像,评论,评级中有4个表,所有内容都与指向图像或用户的指针相关联。

func loadData(){
    forPaginationStart = 0
    numberOfImagesPerPage = 5
    imageData.removeAllObjects()
    var findImageData: PFQuery = PFQuery(className: "Image")
    findImageData.whereKey("deleted", equalTo: 0)
    findImageData.orderByDescending("createdAt")
    findImageData.skip = forPaginationStart
    findImageData.limit = numberOfImagesPerPage
    findImageData.findObjectsInBackgroundWithBlock{
        (objects:[AnyObject]?, error:NSError?)->Void in

        if error == nil{
            for object in objects! {

                let image:PFObject = object as! PFObject
                self.imageData.addObject(image)

                //comment counts
                var countComments = PFQuery(className:"Comment_image")
                countComments.whereKey("image_id", equalTo: PFObject(withoutDataWithClassName: "Image", objectId: "\(image.objectId!)"))
                countComments.whereKey("deleted", equalTo: 0)
                countComments.countObjectsInBackgroundWithBlock {
                    (count: Int32, error: NSError?) -> Void in
                    if error == nil {
                        println("There is \(count) comments for image: \((object.objectId!)!)")
                        self.commentCount.append(countComments.countObjects())
                    }
                }
            }

            self.tableView.reloadData()

        }

    }

    //count number of records in DB for pagination end
    var totalNumOfRows: PFQuery = PFQuery(className: "Image")
    totalNumOfRows.whereKey("deleted", equalTo: 0)
    totalNumOfRows.countObjectsInBackgroundWithBlock {
        (count: Int32, error: NSError?) -> Void in
        if (error == nil) {
            self.totalNumRecords = Int(count)
            println("total records: \(self.totalNumRecords)")
        }


    }
}

我使用了php和SQL,但是使用解析同样的方法对我不起作用...我问DB对于该用户有什么价值,如果是1则用户upvoted,如果是负面的他downvoted, 0都没有,我根据值设置了按钮 SQL查询:

IFNULL((SELECT value FROM rating WHERE image.id = rating.image_id AND rating.user_id = (SELECT id FROM user WHERE uuid='$uuid')), 0) as UserRating

1 个答案:

答案 0 :(得分:0)

- (instancetype)whereKey:(NSString *)key matchesKey:(NSString *)otherKey inQuery:(PFQuery *)query

您可以使用this method on PFQuery,例如:

var countComments = PFQuery(className:"Comment_image")
var findImageData: PFQuery = PFQuery(className: "Image")
findImageData.whereKey("deleted", equalTo: 0)
findImageData.orderByDescending("createdAt")
findImageData.skip = forPaginationStart
findImageData.limit = numberOfImagesPerPage
countComments.whereKey("image_id", matchesKey:"objectId", inQuery: )
countComments.whereKey("deleted", equalTo: 0)
countComments.countObjectsInBackgroundWithBlock {
    (count: Int32, error: NSError?) -> Void in
     if error == nil {
         println("There is \(count) comments for all images!")
     }
}

通过设置这样的计数查询,您不必为每个图像调用计数,但我不知道Parse是否优化使其比当前查询更快。