title: Super Category in realm framework disappears as soon as another view controller is loaded
body: <p>So I am currently in the process of making a todo-list app: incorporating timers and also other things. While I was adding some finishing touches to the application, I wanted to make sure that I could set a due date to the task that the person sets. The problem however, arises when the application switches from the &quot;ToDoList&quot; table-view controller to a separate view controller with a date picker and a UITextField for adding the task to the &quot;REALM&quot; database with a given category, name, due date and date created.</p>
  <p>Everything was set up and the whole system was working until I clicked save task and no task was added to the realm directory. After hours of testing with print statements, I discovered that the error arose RIGHT after a function was called in the other view controller which imported the data to the ToDoList view controller for saving. As can be seen in the code snippet below, in order for the task to be added the optional &quot;categorySelected&quot; needs to have a non-nil value. And when I change view controllers, this value turns to nil:</p>
  <p>&quot;AddTaskViewController&quot; :</p>
  <pre><code>        if task != &quot;&quot; {
  
          let mainVC = storyboard?.instantiateViewController(withIdentifier: &quot;tasksTable&quot;) as! TodoListViewController
  
          dateFormatter.dateFormat = &quot;dd-MM-yyyy&quot;
          date = dateFormatter.string(from: datePicker.date)
  
          mainVC.dataRecieved(data1: task, data2: date)
  
          performSegue(withIdentifier: &quot;unwindToTable&quot;, sender: self)
      }
  </code></pre>
  <p>&quot;dataRecieved Function on the other VC + Point of error&quot; :</p>
  <pre><code>func dataRecieved (data1: String, data2 : String) {
  
      let taskName = data1
      let taskDueDate = data2
  
      if taskName != &quot;&quot; {
  
          if let currentCateogry = selectedCategory {
  // This is the point of error
          do {
              try realm.write{
                  let newItem = Item()
                  newItem.title = taskName
                  newItem.dateDue = taskDueDate
                  newItem.dateCreated = Date()
                  currentCateogry.items.append(newItem)
                  print(&quot;finished appending&quot;)
              }
          } catch {
              print(&quot;Error aappending new items into realm: \(error)&quot;)
          }
          tableView.reloadData()
      }
      } else {
          tableView.reloadData()
      }
  }
  </code></pre>
  <p>The variable which turns to nil is the &quot;selectedCategory&quot;. In order to show you how this variable works, let me introduce you to a small wire-frame layout of the application.</p>
  <ol>
  <li>categoryViewController is the ROOT-View controller</li>
  <li>Creating a category just includes a UIAlertView where you put the name</li>
  <li>Selecting the category takes you to the ToDoList view controller.</li>
  <li>A task has a parent category of &quot;categorySelected&quot; and is therefore appended in that portion of realm.</li>
  </ol>
  <p>The selectedCategory variable is defined as such:</p>
  <pre><code>var selectedCategory : Category? {
      didSet{
          loadItems()
      }
  }
  </code></pre>
  <p>The loadItems() method:</p>
  <pre><code>func loadItems() {
      
      todoItems = selectedCategory?.items.sorted(byKeyPath: &quot;title&quot;, ascending: true)
      tableView.reloadData()
  }
  </code></pre>
  <p>Selected Category in the CategoryViewController:</p>
  <pre><code>    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      
      if segue.identifier == &quot;goToItems&quot; {
          let destinationVC = segue.destination as! TodoListViewController
          if let indexPath = tableView.indexPathForSelectedRow{
              destinationVC.selectedCategory = categories?[indexPath.row]
          }
      }
  </code></pre>
  <p>**Sorry for the missing } at the end.</p>
  <p>So basically, when the person using the application clicks on a category cell, that value is transported through the view controllers before the segue is made. For that reason, the value stays inside the variable as long as the tableView controller is on the fore-ground.</p>
  <p>When I actually go to the new view controller, this value is erased and so... despite everything working... when I press the &quot;saveTaskButton&quot;, since the &quot;selectedCategory&quot; is <code>nil</code>... the appending never happens, and there is no task to be saved.</p>
  <p>I am requesting for one of 2 solutions:</p>
  <ol>
  <li>If I could somehow use a UIAlertView for a date input... so therefore I do not have to even change view controllers for the add task method.</li>
  <li>Some way to restore the category value (which is of type category due to class defining systems)</li>
  </ol>
  <p>The solutions I have tried thus far and the problems:</p>
  <ol>
  <li><p>ViewDidAppear --&gt; The function is called in the other view, when the value for the category is nil, therefore the viewDidAppear really serves no purpose</p>
  </li>
  <li><p>Somehow storing the value of the &quot;selectedCategory&quot; type category item --&gt; Since I use the &quot;didSet&quot; method... If I force this value to be preserved to the next view controller I get the error <code>selectedCategory is a &quot;get&quot; only dataType</code></p>
  </li>
  </ol>
  <p>I do not think that this is a very distinct problem, but I have only been learning swift for about 2-3 months and this is only the first time I have encountered such an issue. I hope you guys can help me.</p>
  <p>Thanks!</p>
  <p>(Also, please just comment if you need to see any other part of the code)</p>