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

Prevent random pages from repeating themselves in Swift app

My app involves the user going to different random pages with the push of a button. I don't want any of these random pages to repeat themselves, even after the user closes out of the app and opens it the next time. How do I make it so that random pages don't ever appear again for the same user?

1 Answer

At a high level this is what I would do. I would add an ID for each page. I would create a shared page manager object through dependency injection or a singleton so that only one instance of that object can exist at a time. (This ensures you don't get multiple lists and get bugs from pages that were already shown being put in a different instance and showing up again.) The shared page manager object should store an array with the list of already used IDs. Check the ID of your current page against the list contained in the array of the page manager object.

This solution isn't very efficient for a very high number of pages. But if you don't have too many (under 100 or so) this shouldn't be that bad.