Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我的应用程序中类似Parse.com的系统_Ios_Swift_Parse Platform - Fatal编程技术网

Ios 我的应用程序中类似Parse.com的系统

Ios 我的应用程序中类似Parse.com的系统,ios,swift,parse-platform,Ios,Swift,Parse Platform,我喜欢的系统似乎有点慢。我将粘贴下面的代码,但我想先解释一下代码。当用户按下“likeButton”以喜欢某个内容时,它首先检查用户是否已经喜欢该内容。如果用户喜欢它,它就不喜欢该内容;如果用户不喜欢该内容,它就喜欢它 @IBAction func likeButtonAction(sender: AnyObject) { let buttonPosition = sender.convertPoint(CGPointZero, toView: self.collectionV

我喜欢的系统似乎有点慢。我将粘贴下面的代码,但我想先解释一下代码。当用户按下“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秒。

让我们看看,一旦执行检查,您就会更新您喜欢的图像
那绝对好。但如果在更新对象之前保留代码,则速度会更快。
比如说,

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)
                    }
                }
            }

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

其次,
在使用单元格编辑表视图或视图时,只需检查该帖子是否受到欢迎,从而减少一点时间(可以忽略不计,但在拥有大量数据集时很有用)

希望这有帮助



----编辑--

我没有做太多,只是在savInBackground函数之前移动了一些行。其次,我已经启用了按钮,一旦您的查询完成。这在save函数中
这将很有帮助,因为一旦解析表被like或removing like更新,默认情况下该按钮将被启用


干杯

所以,这不是在1秒内完成任务。你的应用程序实际更新按钮需要1-2秒。但问题是,即使在instagram中,也可能需要那么多时间。第一个原因可能是你的互联网连接。第二,在instagram中,类似的情况会立即发生,但在后台,可能也会发生1-2秒。所以你能做的就是在加载自己的时候显示“喜欢的”。我能想到的唯一一件事是,如果你在Parse的用户类中放置一个喜欢和不喜欢的数组。这个您可以使用PFUser.currentUser()[“liked”]==post.text或类似的东西。这将减少检查的时间。希望这有帮助。但是在你的代码中没有类似于删除的函数吗?-你能看看我提出的这个问题吗:我只是想说,在你开始更新你的解析类之前,你做了“按钮图像更改”操作。我刚才给出了一个使用您的代码的示例。你可以做另一件事,因为它只是移动一些代码行。如果用户快速按下like按钮(?)会把事情搞砸,我想。我不这么认为,唯一能把你的应用搞砸的是,如果他一次又一次地按下按钮。然后会创建多个线程,性能实际上可能会变慢。所以你甚至需要记住这一点。
@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)
            }
        }
    }
}