Ios 添加解析对象后出现重新加载表视图的问题
我有一个UITableViewController,它显示解析查询中的数据。它获取数据并显示良好,除非我创建一个新对象并再次运行查询以获取新数据。创建新对象时,表视图会将现有数据保留在数组中并显示,但会将查询中的所有数据附加到数组中,以便在创建新对象之前已经存在的对象显示两次。我尝试在查询函数开始时清空数组,但由于我在查询中设置了skip属性,因此我无法执行此操作,因为我的数组只有在达到限制时才会在跳过之后获取所有内容。那么,如何将新对象添加到数组中呢 我还应该提到,我不能简单地将新对象名添加到addCollection()中的数组中,因为我必须将objectId添加到我的objectId数组中Ios 添加解析对象后出现重新加载表视图的问题,ios,swift,uitableview,parse-platform,Ios,Swift,Uitableview,Parse Platform,我有一个UITableViewController,它显示解析查询中的数据。它获取数据并显示良好,除非我创建一个新对象并再次运行查询以获取新数据。创建新对象时,表视图会将现有数据保留在数组中并显示,但会将查询中的所有数据附加到数组中,以便在创建新对象之前已经存在的对象显示两次。我尝试在查询函数开始时清空数组,但由于我在查询中设置了skip属性,因此我无法执行此操作,因为我的数组只有在达到限制时才会在跳过之后获取所有内容。那么,如何将新对象添加到数组中呢 我还应该提到,我不能简单地将新对象名添加到
func getCollections() {
activityIndicator?.startAnimating()
// collections = [] - Can't do this because of the skip (if the skip is used)
// objectID = []
let query = PFQuery(className: "Collections")
query.whereKey("user", equalTo: PFUser.currentUser()!)
query.orderByAscending("collectionName")
query.limit = limit
query.skip = skip
query.findObjectsInBackgroundWithBlock( {
(objects, error) -> Void in
if error == nil {
if let objects = objects as [PFObject]! {
for object in objects {
let collectionName = object["collectionName"] as! String
let id = object.objectId
self.collections.append(collectionName)
self.objectID.append(id!)
}
}
if objects!.count == self.limit {
self.skip += self.limit
self.getCollections()
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
self.activityIndicator!.stopAnimating()
}
} else {
var errorString = String()
if let message = error!.userInfo["error"] {
errorString = message as! String
}
print(errorString)
}
})
}
func addCollection(name: String) {
let collection = PFObject(className: "Collections")
collection["user"] = PFUser.currentUser()
collection["collectionName"] = name
collection.saveInBackground()
getCollections()
}
此代码在逻辑上有缺陷,可以简化:
func addCollection(name: String) {
let collection = PFObject(className: "Collections")
collection["user"] = PFUser.currentUser()
collection["collectionName"] = name
collection.saveInBackground()
getCollections()
}
问题包括:
保存
上添加一个完成块,并在其中:
此代码在逻辑上有缺陷,可以简化:
func addCollection(name: String) {
let collection = PFObject(className: "Collections")
collection["user"] = PFUser.currentUser()
collection["collectionName"] = name
collection.saveInBackground()
getCollections()
}
问题包括:
保存
上添加一个完成块,并在其中: