UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Project 9, GCD, Challenge 1

Forums > 100 Days of Swift

"Modify project 1 so that loading the list of NSSL images from our bundle happens in the background"

Am I solving it right? Please answer my questions

Step 1: Move loading into a separate method

Pretty basic

    @objc private func loadPictures() {
        let fm = FileManager.default
        let path = Bundle.main.resourcePath!
        let items = try! fm.contentsOfDirectory(atPath: path)

        for item in items {
            if item.hasPrefix("nssl") {
                pictures.append(item)
            }
        }
        pictures.sort()
    }

Step 2: Add a performSelector to the viewDidLoad()

Pretty obvious too

        performSelector(inBackground: #selector(loadPictures), with: nil)

But

My inner voice start asking questions

  • Are we sure that the loading will be finished by the time tableView will start pulling data source methods?
  • viewDidLoad() is a place where view is loaded, guns fired, we can move on to the next steps, innit?

If this is the case I have ideas which I can't yet implement unfortunately

  1. Move loading to loadView, but this does not make sense becase we will be still blocking UI from loading
  2. Ask tableView to not fire loading at initialization and do that after loading completed. How? Ofcourse I can do a second reload, but since I can't even catch that condition on my own device - it may cause only flickering in most general case.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.