Getting Swift runtime error: "Can't end BackgroundTask: no background task exists with identifier 1"

928 Views Asked by At

I’m trying to implement the TableView example from Chapter 5 in Swift Programming in Easy Steps exercise. I have checked and re-checked the example code (even downloaded and tested the actual example code), but I’m still getting this runtime error. Anyone know why this is happening?

2019-11-01 07:56:51.247052+0100 TableView_EasySteps[2067:39485] Can't end BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.

here is the ViewController code:

import UIKit

class WebsitesTableViewController: UITableViewController {

    var websites:[[String]] = [
        ["Apple",           "https://www.apple.com"]   ,
        ["NY Times",        "https://www.nytimes.com"] ,
        ["DN",              "https://www.dn.se"]   ,
        ["NFL",             "https://www.nfl.com"]   ,
        ["Premier League",  "https://www.premierleague.com"]   ,
        ["The Guardian",    "https://www.theguardian.com"]
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        // preserve selection between presentations
        self.clearsSelectionOnViewWillAppear = false
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return websites.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier")
        if cell == nil {
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cellIdentifier")
        }
        cell!.textLabel!.text = websites[indexPath.row][0]
        cell!.detailTextLabel!.text = websites[indexPath.row][1]

        return cell!
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let url = URL(string: websites[indexPath.row][1]) {
            UIApplication.shared.open(url)
        }
    }

    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            websites.remove(at: indexPath.row)
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }
}
1

There are 1 best solutions below

3
On

Check the SceneDelegate.swift file and make sure it's part of the Control files if you're using MVC standard otherwise just make sure that it's part of the files in your Xcode project alongside the AppDelegate.swift and the ViewController.swift amongst others.