L
L
LIAL2015-07-15 09:05:38
API
LIAL, 2015-07-15 09:05:38

Correct data loading for TableView?

Tell me how to properly organize data loading for TableView (TV) so that there are no lags and it is "correct".
There is an API from which I receive data (I have my own API, I can edit it as needed). Let's say I pull out the 20 nearest objects and give them to the application. They are shown on TV.
Further, the user, after viewing all 20, tries to scroll the table below 20 (we only have 20 objects), if I start loading the next 20, for example, then there will be a lag.
Tell me how to properly organize and where to load data into the DataSource for TableView - to solve such a problem?
PS: I'm still thinking of loading into an array in a singleton that implements access to the API, but I don't understand at what point to load it additionally (not all at once to score), and I'm not sure that this is a good solution

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Tikhonov, 2015-07-15
@LIAL

In general, you catch the event when the last cell is drawn. After that, you load the data and add it to the table (either using the insertRowsAtIndexPaths or reloadData method), before that, of course, pushing it into the model.

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        let lastRow = indexPath.row
        if lastRow == objects.count - 1 {
            fetchData(lastRow)
        }
    }
    
    private func fetchData(lastRow: Int) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * Int64(NSEC_PER_SEC)), dispatch_get_main_queue()) { () -> Void in
        let object = "New data"
        self.objects.append(object)
        self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: lastRow + 1, inSection: 0)], withRowAnimation: .Automatic)
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question