TableView lagging too far and taking up too much memory Swift

I have an application that uses a UITableView inside a UIViewController and the way each cell is rendered is issue information or blog posts that are stored in CoreData after they are loaded. But when I ran the tools tool, I found out that every time you scroll down or up, it retrieves this CoreData. Even if the cell has already been initialized, so this in combination with explicitly NSDate also takes up some kind of memory. The program takes up about 40m bar and about 60% cpu when scrolling down! And I searched for answers as soon as initialize it once, but could find it anywhere. Here's my TableView code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("tableviewcell", forIndexPath: indexPath) as! BlogTableViewCell

    let sortedPosts = SortPosts()
    let realresults = sortedPosts.sortData()




    if realresults.count > 0 {
        println(cell)
        let date = realresults[indexPath.row].valueForKey("date") as? NSDate
        let numberOfTime = PostDateFormatter().getFormattedDate(date!)
        let content = realresults[indexPath.row].valueForKey("content") as! String
        let title = realresults[indexPath.row].valueForKey("title") as! String

        cell.configureCell(Post(title: title.html2String, author: realresults[indexPath.row].valueForKey("author") as! String, date: numberOfTime, content: content.html2String))
        tableView.hidden = false
        self.view.backgroundColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 255.0/255.0)
        myIndicator.stopAnimating()
        myIndicator.hidden = true
        }
    else {
        if PostServices().isConnectedToNetwork() {
            println("Error 0 results returned......")
            self.tableView.reloadData()
        }

    }

    return cell
}

      

and here it is SortPosts:

class SortPosts {
func sortData() -> NSArray {
    var jsonin = NSArray()

    let formatter = NSDateFormatter()
    formatter.dateFormat = "Y-MM-dd\'T\'HH:mm:ss"

    var managedObjectContext : NSManagedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!

    var request = NSFetchRequest(entityName: "Post")
    request.returnsObjectsAsFaults = false

    let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
    request.sortDescriptors = [sortDescriptor]

    var results : NSArray = managedObjectContext.executeFetchRequest(request, error: nil)!

    return results



}

      

}

And here's the PostDateFormatter:

class PostDateFormatter {
func getFormattedDate(date : NSDate) -> String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
let dateInShortFormat = dateFormatter.stringFromDate(date)

let inter = date.timeIntervalSinceNow

var seconds = Int(abs(inter))
var minutes = seconds/60
var hours = minutes/60
var days = hours/24
var weeks = days/7


if seconds < 60 {
return "\(seconds)s"
} else if minutes < 60 {
return "\(minutes)m"
} else if hours < 24 {
return "\(hours)h"
} else if days < 7 {
return "\(days)d"
} else if weeks <= 8 {
return "\(weeks)w"
} else if weeks > 8 {
return "\(dateInShortFormat)"
}
    return "Something went wrong with date formatting"
}

      

}

+3


source to share


3 answers


You understand that for every cell you call,

   let sortedPosts = SortPosts()
   let realresults = sortedPosts.sortData() 

      



it would be wiser to initialize all your data outside of your tableView, or of course at some point other than cellForRowAtIndexPath.

Create an array or dictionary of elements elsewhere, and then just specify the elements in the array in cellForRowAtIndexPath. This way you only get access to kernel data.

+3


source


What you say in your question describes how it works TableViews

. Each cell is removed only when it is visible on the screen. Otherwise, there are no other cells in memory and when new cells are displayed they must be deleted and thus require hitting the database and CPU to get information and draw whenever they are displayed.



0


source


If you solve the problem, I would suggest using: asyncdisplaykit.

I believe it will help your memory. It was originally created to make Facebook Paper possible.

http://asyncdisplaykit.org/

0


source







All Articles