0

I am trying to work on this piece of code from a project I downloaded and trying to use in my code. In the viewDidLoad, a pickerView is populated either by "New Puzzle" or if a user saved a game in the previous VC, the saved game. The way this was originally written, the user saved the game and with a segue came back to this controller with the pickerView. In my "mod", I embedded the two VC in a navigation controller, thus getting a nice Back button to go back to the pickerView controller. However, loading the pickerView is in the viewDidLoad function, which my understanding is, is loaded only once.

Can someone please help me, on which items from the viewDidLoad need to be moved. And when moved, would they need to go into a viewWillAppear, or viewDidAppear? My understanding is it should be in the viewWillAppear.

override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "Games"

        DispatchQueue.main.async {
            if !Settings.shared.adsEnabled {
                self.removeTableViewHeader()
            } else {
                //self.tableView.layoutTableHeaderView()
            }
        }
        //Picker Stuff
        // Connect data:
            self.picker.delegate = self
            self.picker.dataSource = self


            copyDatabaseIfNeeded("puzzles")
        // open database
            if sqlite3_open(finalDatabaseUrl2.path, &db2) != SQLITE_OK {
                //print("error opening db")
            }
            else{

            }

            pickerData.append("New Puzzle")

        //retrieve all dates from previous puzzles and add them to pickerview
            var queryStatement2 = ""

            queryStatement2 = "SELECT * FROM puzzle;"

            self.statement2 = nil
            if sqlite3_prepare_v2(self.db2,queryStatement2,-1,&self.statement2,nil) == SQLITE_OK{

                while(sqlite3_step(self.statement2) == SQLITE_ROW){
                    let date = sqlite3_column_text(self.statement2,2)
                    let comp = sqlite3_column_text(self.statement2,3)

                    let dateString = String(cString: date!)
                    let compString = String(cString: comp!)

                    pickerData.append("\(dateString) - \(compString)")
                }
            //sqlite3_reset(self.statement2)
                sqlite3_finalize(self.statement2)

            }
        }
midlifecrisis
  • 57
  • 1
  • 6
  • `viewDidLoad()` -> `viewWillAppear()` -> `viewDidAppeared()`. `viewDidLoad()` only call 1 time when you init the view. The other will call when you access the view. If you want something in UI changed when access the view ( the view did not `deinit` before), call it in `viewDidAppeared()` – King.lbt May 25 '20 at 03:44
  • Thanks King. I tried moving everything below //Picker Stuff. However, now when I go back, I get two entries for "New Puzzle". If I go once more back to the root and back into this VC, I get what I'm trying to accomplish. One "New Puzzle" and one saved. How can I get rid of the second "New Puzzle" appearing? – midlifecrisis May 25 '20 at 03:55
  • It's duplicate because when you get back to VC, you query and append new items to your picker. On your purpose, you can init Picker in ViewDidLoad() and data can be adapted to viewDidAppeared but not append, if you query all like this, you need to set to array. Not append – King.lbt May 25 '20 at 06:11
  • Is it possible to help me out, please? this is beyond my knowledge at this point. – midlifecrisis May 25 '20 at 06:15

1 Answers1

0
override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "Games"

        DispatchQueue.main.async {
            if !Settings.shared.adsEnabled {
                self.removeTableViewHeader()
            } else {
                //self.tableView.layoutTableHeaderView()
            }
        }
        //Picker Stuff
        // Connect data:
            self.picker.delegate = self
            self.picker.dataSource = self



        }

override func viewDidAppear() {
 copyDatabaseIfNeeded("puzzles")
 pickerData = []
            // open database
                if sqlite3_open(finalDatabaseUrl2.path, &db2) != SQLITE_OK {
                    //print("error opening db")
                }
                else{

                }

                pickerData.append("New Puzzle")

            //retrieve all dates from previous puzzles and add them to pickerview
                var queryStatement2 = ""

                queryStatement2 = "SELECT * FROM puzzle;"

                self.statement2 = nil
                if sqlite3_prepare_v2(self.db2,queryStatement2,-1,&self.statement2,nil) == SQLITE_OK{

                    while(sqlite3_step(self.statement2) == SQLITE_ROW){
                        let date = sqlite3_column_text(self.statement2,2)
                        let comp = sqlite3_column_text(self.statement2,3)

                        let dateString = String(cString: date!)
                        let compString = String(cString: comp!)

                        pickerData.append("\(dateString) - \(compString)")
                    }
                //sqlite3_reset(self.statement2)
                    sqlite3_finalize(self.statement2)

                }
}

You can update your code like this.

King.lbt
  • 843
  • 5
  • 15
  • I tried. Initially it didn't let me override (Method does not override any method from its superclass), so i removed override. Then running it, the "New Puzzle" doesn't appear, which causes (Thread 1: Fatal error: Index out of range). – midlifecrisis May 25 '20 at 06:28
  • Sorry, my mistake. It's 'viewDidAppear()' not 'viewDidAppeared()' – King.lbt May 25 '20 at 06:58
  • I did it as viewDidAppear. That's when the "New Puzzle" did not populate giving the error when pressing play (since there's no value to go to). – midlifecrisis May 25 '20 at 15:38
  • No data populate because in your code don't have anything to call your picker reload data. In your sample code above don't include that. Call that function after `pickerData `append all your data. – King.lbt May 26 '20 at 04:30
  • if I unwind the segue, does the viewDidLoad work or is viewDidAppear again? – midlifecrisis May 26 '20 at 05:54
  • https://stackoverflow.com/questions/28969032/what-the-equivalent-of-activity-life-cycle-in-ios. Check this a better understanding life circle of iOS app. – King.lbt May 26 '20 at 06:11