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

SOLVED: Project 9 Challenge Issue

Forums > 100 Days of Swift

Hi! I'm trying to do challanges that are part of Project 9 day two. I need to modify Project 1 so that loading the list of NSSL images from bundle happens in the background and then call reloadData() method to update tableView. Xcode doesn't have any error or alerts but when trying to run I get an error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Project_1.ViewController reloadData]: unrecognized selector sent to instance 0x7ff33b706fb0' terminating with uncaught exception of type NSException

I also get similar error when trying to run Paul's code

Here is the code:

 override func viewDidLoad() {
        super.viewDidLoad()
        title = "Iceland"
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareAppTapped))
        performSelector(inBackground: #selector(loadImages), with: nil)
    }
@objc func loadImages() {
    let fm = FileManager.default
    let path = Bundle.main.resourcePath!
    let items = try! fm.contentsOfDirectory(atPath: path).sorted()

    for item in items {
        if item.hasPrefix("IMG"){
            pictures.append(item)
        }
    }
    performSelector(onMainThread: #selector(UITableView.reloadData), with: nil, waitUntilDone: false)
}

3      

Hi Artem,

Hope you're doing great.

I would like to post my code here, to help you with this issue. Frankly, I didn't encounter this same error (yet 😅) but hope this could help.

Note that in my case, I'm using Grand Central Dispatch instead of the good old performSelector() method.

let globalQueue = DispatchQueue.global()

override func viewDidLoad() {
    super.viewDidLoad()

    title = "Storm Viewer"
    navigationController?.navigationBar.prefersLargeTitles = true

    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))

    let fm = FileManager.default
    let path = Bundle.main.resourcePath!
    let items = try! fm.contentsOfDirectory(atPath: path)

    globalQueue.async { [weak self] in
        for item in items {
            if item.hasPrefix("nssl") {
                self?.pictures.append(item)
            }
        }
        self?.pictures.sort()
    }

    tableView.reloadData()
}

4      

Thank you @rungxanh2901 for your feedback! I knew I could solve this issue by going the way you provided but still wanted to know how to use performSelector() correctly so it doesn't throw errors.

3      

Dear Artem,

So I kind of figured out what caused the crash here: seems like the background async-ed UITableView.reloadData, albeit coded to be pushed to the main thread, is the reason.

Here's how I would tackle the problem, just a slight diff from yours. Let me know if it helps.

override func viewDidLoad() {
    super.viewDidLoad()

    title = "Storm Viewer"
    navigationController?.navigationBar.prefersLargeTitles = true

    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))

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

    tableView.reloadData()  // mine is kept in viewDidLoad(), not in loadImages()
}

@objc func loadImages() {
    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()
}

Cheers!

6      

Hi you can use this version as well wrapped in DispatchQueue

    DispatchQueue.global(qos: .background).async { [weak self] in
    let fm = FileManager.default // Access to filesystem
    let path =  Bundle.main.resourcePath! // Shows the location of the requested file from app bundle[directory]
    let items = try! fm.contentsOfDirectory(atPath: path) // Returns the contents of the file from the bundle at a path

    for item in items { // Each time an item is found this loop will iterate
        if item.hasPrefix("nssl") {
            self?.pictures.append(item)
           }
        }

        self?.pictures.sort() // Displays each item in a sorted order in the table view
    }
    tableView.reloadData()
}

4      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.