如何避免重复输入? [重复]

时间:2022-04-08 07:34:38

This question already has an answer here:

这个问题在这里已有答案:

I added a liking system with a like class, whenever a user likes a post it will be added to an array based on their user. Whenever the button is clicked, a new entry is created even if the user pointer already exists, how do I avoid this?

我添加了一个类似类的喜欢系统,每当用户喜欢一个帖子时,它会根据用户添加到一个数组中。只要单击该按钮,即使用户指针已存在,也会创建一个新条目,如何避免这种情况?

如何避免重复输入? [重复]

func loved(sender: UIButton){
   let query = PFQuery(className: "Like")
     var object = PFObject(className: "Like")
    query.whereKey("user", equalTo: PFUser.currentUser()!)

    query.findObjectsInBackgroundWithBlock { (likes: [AnyObject]?, error: NSError?) -> Void in
        if(likes?.count > 0){
            object.addUniqueObject(self.playertitle.text!, forKey: "LikedSongsTitle")
            object.addUniqueObject(self.playerartist.text!, forKey: "LikedSongsArtist")
        }
        object.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in

            if(error != nil){
                print(error)
            }else{

                print("saved")
            }
        })
    }


}

1 个解决方案

#1


1  

Instead of creating a new "Like" object and saving it as your code shows, you'll first need to query this table to determine if there's already a row for that user. If it returns a result, you can then call addUniqueObject on that existing object and save it. Otherwise, you can create a new object just as you've already done.

您不必创建新的“Like”对象并将其保存为代码显示,而是首先需要查询此表以确定该用户是否已有一行。如果它返回结果,则可以在该现有对象上调用addUniqueObject并保存。否则,您可以像创建一样创建新对象。

Your query might look something like this:

您的查询可能如下所示:

let query = PFQuery(className: "Like")
query.whereKey("user", equalTo: PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock { (likes: [PFObject]?, error: NSError?) -> Void in
    //if likes.count > 0 you already have a like - just update it and save
    //otherwise create a new Like object and save it
}

You may wish to revisit how this works though. Arrays for song titles and artists does not seem to be a good way to accomplish what you're after here - especially if you add more info such as album or year etc. A pointer to a Song class would be much more appropriate.

您可能希望重新审视它的工作原理。歌曲标题和艺术家的数组似乎并不是一个很好的方式来完成你在这里所取得的成就 - 特别是如果你添加更多的信息,如专辑或年份等。指向宋类的指针将更合适。

#1


1  

Instead of creating a new "Like" object and saving it as your code shows, you'll first need to query this table to determine if there's already a row for that user. If it returns a result, you can then call addUniqueObject on that existing object and save it. Otherwise, you can create a new object just as you've already done.

您不必创建新的“Like”对象并将其保存为代码显示,而是首先需要查询此表以确定该用户是否已有一行。如果它返回结果,则可以在该现有对象上调用addUniqueObject并保存。否则,您可以像创建一样创建新对象。

Your query might look something like this:

您的查询可能如下所示:

let query = PFQuery(className: "Like")
query.whereKey("user", equalTo: PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock { (likes: [PFObject]?, error: NSError?) -> Void in
    //if likes.count > 0 you already have a like - just update it and save
    //otherwise create a new Like object and save it
}

You may wish to revisit how this works though. Arrays for song titles and artists does not seem to be a good way to accomplish what you're after here - especially if you add more info such as album or year etc. A pointer to a Song class would be much more appropriate.

您可能希望重新审视它的工作原理。歌曲标题和艺术家的数组似乎并不是一个很好的方式来完成你在这里所取得的成就 - 特别是如果你添加更多的信息,如专辑或年份等。指向宋类的指针将更合适。