Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Looping through NSMutuableArray

In my app, I retrieve around 3000 CKRecord, and adds them to an NSMutbaleArray named movies. I need to later loop through this array. And I need the looping to be as fast as possible. Right now my app works a little bit slow, because of this. What i have done up to this point is this:

   if let recordMovies = movies.objectEnumerator().allObjects as? [CKRecord] {
        for movie in recordMovies {
       //Looping
       {
    }

But this turns the NSMutuableArray into an CKRecord array, which doesn´t speed ut the process at all.

Hi Simen,

It's my understanding that for...in loops are highly optimized... in other words, it should be able to chew through your 3000 records reasonably quickly.

If there's a noticeable lag (and even if there's not), I'd recommend presenting an activity indicator in your UI before your loop, then dismiss it afterwards. I'm not sure what else you have going on in your app, but you could also make sure your UI receives the priority by placing all of its calls on the main thread:

dispatch_async(dispatch_get_main_queue(), {

    // UI calls here

}

Good luck!

1 Answer

Thank you for answering! One more question? Using NSMutabelArray instead of regular swift array, will speed up the looping process or?