John Leonardo John Leonardo - 1 month ago 15
Swift Question

What would be more efficient for limiting array capacity in swift?

So, I've stumbled upon a mixed blessing here. My app is growing faster than expected, and I noticed the performance is starting to go down. It's a social media, and the user data is stored in Firebase. On viewDidLoad, I have the user data parsed into a Table View. I have hundreds of records that has to be loaded from Firebase onto the device, and now I want to limit how many users are shown in the Table View (100).

So, for my question(s):

Which would be a better performance option? Just simply limiting the numberOfRowsInSection to 100? or removing all elements in my users array after index 100?

If it's the second option, how would I do that exactly? Unfortunately, I need the user data stored in the array initially so I can sort them, but then after sorting, I'd like to remove every element in the array after a certain index. (I know this seems weird, I'll fix the code later and make this smoother).

Thank you!

Answer

Ideally you would want to handle this issue on the server side. So when you call your endpoint for user data it would handle the sort and only return at most 100 items.

If you're not wanting to do this and only choose between your two options I would suggest deleting the elements after 100 if you don't need them after the sort. This could free up a rather large amount of memory depending on the size of each object. To easily remove items after 100 you can use this:

myArray.removeRange(100..<myArray.count)