I have two screens: the first one displays a table with data, and the second we add this data.

When there is a lot of data, the application starts to slow down.

I have this code in viewDidLoad ():

let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let context: NSManagedObjectContext = appDel.managedObjectContext do { let request = NSFetchRequest(entityName: "Diary") let result = try context.executeFetchRequest(request) if result.count > 0{ for item in result as! [NSManagedObject]{ let date = item.valueForKey("date") as! String if(self.dateArr.indexOf(date)==nil){ self.dateArr.append(date) } self.diary.append(item) } } else { self.dateArr = [] } }catch{ //print("ERROR READ DATA!") } 

In it, I check all the data in our table, check the dates in it (that would be divided into sections in the future) and add them all to the array, so that I can quickly work with them. I saw about this code on YouTube (there was no date check there), can it not be done like this and therefore the application slows down? And if so, could you tell me how to do it right, or can tell some article where it all signs.

  • one
    Transfer all resource-intensive tasks to a separate thread. - Max Mikheyenko
  • This answer is likely to help ru.stackoverflow.com/questions/458735/… - Max Mikheyenko
  • Try adding the line request.propertiesToFetch = ["date"] before executeFetchRequest This should reduce the number of calls to the database ... - Che

1 answer 1

Well, firstly, what you do it in viewDidLoad does not affect the work of the table, but as it was said in the comments, it will be a good decision to take resource-intensive actions into a separate thread. BUT the fall of an FPS table depends on various factors: What code is written in cellForRowAtIndexPath, whether you use auto-layouts or autoresizing-mask, if the first, then all the constraints are properly placed, if the second, where and how you arrange the elements, is also a particularly important role plays WHAT data is displayed in the table cells, if these are pictures, do you cache them, load them asynchronously, is the same as some do @property from Storyboard or .xib file strong, which results in a memory leak and as a result lags, Moreover, if you have all the cells of the same size - this is still ok, but e If dynamic cells, it is necessary to cache their size, unless of course you want to achieve the most smooth tables. Here is a good article about how you can achieve perfect tables: https://medium.com/ios-os-x-development/perfect-smooth-scrolling-in-uitableviews-fd609d5275a5#.ssbqdsgp3