gpt4 book ai didi

ios - Parse.com - 就像我的应用程序中的系统

转载 作者:行者123 更新时间:2023-11-30 13:47:05 25 4
gpt4 key购买 nike

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


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

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

希望这有帮助。



----编辑----

@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 函数之前移动了一些行。其次,一旦您的查询完成,我就启用了该按钮。那是在保存功能中。
这将很有帮助,因为一旦通过类似或删除类似更新了解析表,该按钮将默认启用。

干杯!

关于ios - Parse.com - 就像我的应用程序中的系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34796259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com