I have a UITableViewController that displays data from a Parse query. It get the data and displays it fine except when I create a new object and run the query again to get the new data. When I create a new object the table view keeps the existing data in my array and displays it but it appends all the data from the query to the array so the objects that already existed prior to creating the new object get displayed twice. I tried emptying the arrays at the start of the query function but since I have the skip property set on the query I can't do that because my array will only get everything after the skip if the limit is reached. So, how can I just add the new object to my array?
I should also mention that I can't simply add the new object name to the array in addCollection() because I have to add the objectId to my objectID array.
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()
}