0

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

1 Answer 1

2

This code is logically flawed and can be simplified:

func addCollection(name: String) {
    let collection = PFObject(className: "Collections")
    collection["user"] = PFUser.currentUser()
    collection["collectionName"] = name
    collection.saveInBackground()
    getCollections()
}

problems include:

  1. your save runs in the background and isn't complete before you try to reload
  2. your reload doesn't update or reset the skip and limit values

Unless you need to check for updates from other users then you shouldn't make a new request to the server to get new details. Instead you should add a completion block on the save and in there:

  1. get the name and id and add those values to your data source arrays
  2. update the skip value by adding one
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.