Parse.com - 与我的应用程序中的系统一样

时间:2016-01-14 17:43:13

标签: ios swift parse-platform

我喜欢的系统看起来有点慢。我将粘贴下面的代码,但我想先解释代码。当用户按下“likeButton”以喜欢内容时,它首先检查用户是否已经喜欢该内容。如果用户喜欢它,则不喜欢内容,如果用户不喜欢该内容,则会喜欢它。

@IBAction func likeButtonAction(sender: AnyObject) {

        let buttonPosition = sender.convertPoint(CGPointZero, toView: self.collectionView)
        let indexPath = self.collectionView.indexPathForItemAtPoint(buttonPosition)

        let post = self.arrayOfDetails[indexPath!.item]

        let cell = collectionView.cellForItemAtIndexPath(indexPath!) as! CollectionViewCell

        cell.likeButton.enabled = false

        let query = PFObject(withoutDataWithClassName: "userUploads", objectId: self.arrayOfDetails[indexPath!.row].objID)
        if (post.likedBy.containsObject((PFUser.currentUser()?.username)!)) {
            query.removeObject((PFUser.currentUser()?.username)!, forKey: "likedBy")
            print("STEP 1: Current user removed from likedBy column")

            //Save
            query.saveInBackgroundWithBlock {
                (success: Bool, error: NSError?) -> Void in
                if (success) {
                    // The object has been incremented
                    print("STEP 2: Successfully saved")
                    post.likedBy
                    cell.likeButton.setBackgroundImage(self.Like, forState: .Normal)
                    self.queryCurrentUploads()

                } else {
                    // There was a problem, check error.description
                    //println(error!.description)
                }
            }
        }
        else{
            query.addObject((PFUser.currentUser()?.username)!, forKey: "likedBy")
            print("STEP 1: Current user added to likedBy column")

            // Save
            query.saveInBackgroundWithBlock {
                (success: Bool, error: NSError?) -> Void in
                if (success) {
                    // The object has been incremented
                    print("STEP 2: Successfully saved")
                    cell.likeButton.setBackgroundImage(self.LikeDone, forState: .Normal)
                    self.queryCurrentUploads()

                } else {
                    // There was a problem, check error.description
                    //println(error!.description)
                }
            }
        }
    }

我可以做些什么来简化代码,因为这段代码使用1-2秒完成,当我查看Instagram时它喜欢/不喜欢不到1秒。

1 个答案:

答案 0 :(得分:0)

让我们看一下,一旦您执行检查,您就会更新您喜欢的图像。
那绝对没问题。但是如果你在更新对象之前实际保留代码会更快 例如,

if (post.likedBy.containsObject((PFUser.currentUser()?.username)!)) {
            query.removeObject((PFUser.currentUser()?.username)!, forKey: "likedBy")
            print("STEP 1: Current user removed from likedBy column")

            //Save
            query.saveInBackgroundWithBlock {
                (success: Bool, error: NSError?) -> Void in
                if (success) {
                    // The object has been incremented
                    print("STEP 2: Successfully saved")
                    post.likedBy
                    cell.likeButton.setBackgroundImage(self.Like, forState: .Normal)
                    self.queryCurrentUploads()

                } else {
                    // There was a problem, check error.description
                    //println(error!.description)
                }
            }
        }

在保存对象后,不要更改状态,请在检查对象是否被喜欢后立即更改。

所以它可以是这样的,

 if (post.likedBy.containsObject((PFUser.currentUser()?.username)!)) {
        cell.likeButton.setBackgroundImage(self.Like, forState: .Normal)
        query.removeObject((PFUser.currentUser()?.username)!, forKey: "likedBy")
        print("STEP 1: Current user removed from likedBy column")
                query.saveInBackgroundWithBlock {
                    (success: Bool, error: NSError?) -> Void in
                    if (success) {
                        // The object has been incremented
                        print("STEP 2: Successfully saved")
                        post.likedBy

                        self.queryCurrentUploads()

                    } else {
                        // There was a problem, check error.description
                        //println(error!.description)
                    }
                }
            }


它为什么慢? 问题是您的控件打开一个新线程,执行所有保存和更新任务。一旦完成,您的喜欢按钮的图像将被更新。

其次,
当使用单元格填充表格视图或视图时,只需检查该帖子是否已被喜欢,因此会减少一点时间(可忽略不计,但是当您拥有庞大的数据集时非常有用)。

希望这有帮助。



---- ---- EDIT

@IBAction func likeButtonAction(sender: AnyObject) {

    let buttonPosition = sender.convertPoint(CGPointZero, toView: self.collectionView)
    let indexPath = self.collectionView.indexPathForItemAtPoint(buttonPosition)

    let post = self.arrayOfDetails[indexPath!.item]

    let cell = collectionView.cellForItemAtIndexPath(indexPath!) as! CollectionViewCell

    cell.likeButton.enabled = false

    let query = PFObject(withoutDataWithClassName: "userUploads", objectId: self.arrayOfDetails[indexPath!.row].objID)
    if (post.likedBy.containsObject((PFUser.currentUser()?.username)!)) {
        cell.likeButton.setBackgroundImage(self.Like, forState: .Normal)
        query.removeObject((PFUser.currentUser()?.username)!, forKey: "likedBy")
        print("STEP 1: Current user removed from likedBy column")

        //Save
        query.saveInBackgroundWithBlock {
            (success: Bool, error: NSError?) -> Void in
            if (success) {
                // The object has been incremented
                cell.likeButton.enabled = true;
                print("STEP 2: Successfully saved")
                self.queryCurrentUploads()

            } else {
                // There was a problem, check error.description
                //println(error!.description)
            }
        }
    }
    else{
        cell.likeButton.setBackgroundImage(self.LikeDone, forState: .Normal)
        query.addObject((PFUser.currentUser()?.username)!, forKey: "likedBy")
        print("STEP 1: Current user added to likedBy column")

        // Save
        query.saveInBackgroundWithBlock {
            (success: Bool, error: NSError?) -> Void in
            if (success) {
                // The object has been incremented
                print("STEP 2: Successfully saved")
                cell.likeButton.enabled = true;
                self.queryCurrentUploads()

            } else {
                // There was a problem, check error.description
                //println(error!.description)
            }
        }
    }
}

我没有做太多,只是在savInBackground函数之前移动了一些行。其次,我在查询完成后启用了按钮。那是在保存功能中。
这将是有用的,因为一旦解析表被类似更新或删除之类,默认情况下将启用该按钮。

干杯!