I'm using Parse.com on Swift, but TableView update doesn't work.

Asked 2 months ago, Updated 2 months ago, 4 views

I am creating a program to display textField characters in TableView using Parse, MBaaS on Swift, but the part that I pull TableView to update is not working.

override func viewDidLoad(){
    super.viewDidLoad()

    Retrieving data from //parse
    self.loadData()
    // Configure DataSource settings.
    tableView.dataSource=self

    // Configure Delete.
    tableView.delegate=self

    // Pull to update
    self.pullrefresh()

}

Retrieving data from //parse
funcloadData(){
    varquery —PFQuery=PFQuery (className: "Comment")
    query.orderByDescending("createdAt")
    query.findObjectsInBackgroundWithBlock{(objects:[AnyObject]!, error:NSError!) ->Void in
        if(error!=nil){
            // error handling
        }

        // Put parse data in PFObject comments
        for object in objects {
            self.comments.addObject(object)
        }
       self.tableView.reloadData()
    }

}

// Pull to update function
func pullrefresh(){
    self.refreshControl=UIRefreshControl()
    self.refreshControl.attributedTitle=NSAttributedString(string: "↓pull")
    self.refreshControl.addTarget(self, action: "refresh:", forControlEvents:UICControlEvents.ValueChanged)
    self.tableView.addSubview(refreshControl)
}
// How to get data for updates
func refresh (sender: AnyObject)
{
    self.loadData()
    Finish //refresh
    self.refreshControl.endRefreshing()
}

I wrote a program like this, but when I pull it and update it, the same content appears double.
How do I update only the text that is already displayed on the screen without duplication?
I look forward to hearing from you.

ios swift xcode parse.com

2022-09-30 14:30

1 Answers

I've never used Parse, but I'll write down what I noticed.

As PFQuery does not seem to be specifically narrowed down, I always get everything from query.findObjectsInBackgroundWithBlock.I added it to self.comments that already contain data, so it seems to have been added in duplicate.

Therefore, if possible, PFQuery will narrow it down by date, or if not, replace self.comments with the retrieved array

self.comments=objects

I think it's better to do so.

Also, query.findObjectsInBackgroundWithBlock seems to be handled in the background, but which thread is the callback called?It would be nice if the main thread is always referred to as a specific thread, but if not, there is a possibility of simultaneous execution failures.Also, tableView.reloadData must be run on the main thread.


2022-09-30 14:30

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.