John Leonardo
John Leonardo

Reputation: 598

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!

Upvotes: 0

Views: 88

Answers (2)

Josh Homann
Josh Homann

Reputation: 16327

Generally speaking whenever you have a tableview or a collection view that is backed by some kind of expensive datastore with an arbitrary number of entries, whether its a database or a REST call or whatever else, you want to paginate the loading and get more records when the user scrolls. You certainly would not want to load all of your facebook posts, when a you go to your facebook feed for instance. Only the most recent feed items are loaded and as you scroll the view controller asks the server for more data, so you can scroll indefinitely.

    func refresh() {
        if isLoading == false {
            isEndOfData = false
            currentPage = 0
            loadPage(currentPage)
        }
    }

    func loadPage(_ page: Int) {
        isLoading = true
        Notification.getPage(currentPage, pageSize: pageSize) { error, notifications in
        //Handle API call here
        }
    }

    func loadNextPage() {
        if isLoading == false  && isEndOfData == false {
            currentPage += 1
            loadPage(currentPage)
        }
    }

    //MARK: UIScrollViewDelegate
    extension NotificationViewController: UIScrollViewDelegate {
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            let currentOffset = scrollView.contentOffset.y
            let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
            if (maximumOffset - currentOffset) <= GlobalConstants.paginationScrollOffset {
                loadNextPage()
            }
        }
    }

Upvotes: 1

random
random

Reputation: 8608

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)

Upvotes: 3

Related Questions