I'm making an application that populates the table view from a parse database. The problem is there's a bit of lag whenever I scroll. Through some searching, I've come to realize that the problem lies in the cellForRowAtIndexPath() function where I'm querying the database in order to get the data I need for that cell's labels. I have to query the database there because I use the cell's position to index into my messages array in order to grab the right message. Here's the meat of my cellForRowAtIndexPath() function:
if (path.row < messagesArray.count) {
var message = messagesArray[path.row]
var dateFormat = NSDateFormatter()
var query = PFQuery(className: "Messages")
query.whereKey("messageID", equalTo: message.messageID)
var first = query.getFirstObject()
if (message.senderID == loggedInUserID) {
cell.nameLabel?.text = "I said"
} else {
queryO?.whereKey("objectId", equalTo: message.senderID)
var second = queryO?.getFirstObject()
var name = second!.objectForKey("FIRST") as! String
var middle = second!.objectForKey("MIDDLE") as! String?
var last = second!.objectForKey("LAST") as! String
if (middle != nil) {
cell.nameLabel?.text = name + " " + middle! + " " + last
} else {
cell.nameLabel?.text = name + " " + last
}
}
cell.messageLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.messageLabel?.text = message.messageText
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy 'at' h:mm a" // superset of OP's format
let str = dateFormatter.stringFromDate(first!.objectForKey("TIME") as! NSDate)
cell.senderLabel?.text = str
}
Any ideas?
What I ended up doing was I took the query outside my function and ran it in my viewDidLoad() which improved the speed drastically. Thanks for your help guys :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.