title: Realm.io 'RLMArray is no longer valid' when deleting an RLMObject that has had children objects
body: <p>In my app I keep receiving, 'RLMException', reason: 'RLMArray is no longer valid' while attempting to delete an RLMObject that contains a one-to-many relationship with another RLMObject. For example: 'Task' is the RLMObject which contains an RLMArray 'records' with the type 'Record' RLMObjects within it. The code throwing the error is as follows:</p>
  
  <pre><code>public class func deleteTask(#taskName: String, retainRecords: Bool) {
      let realm = Database.getRealm()
      let currentTask = (Task.objectsWhere("name = '\(taskName)'").objectAtIndex(0) as Task)
      let loopCount:UInt = currentTask.records.count
  
      if (retainRecords) {
          for var i:UInt = 0; i &lt; loopCount; ++i {
              Database.moveRecord(record: (currentTask.records.objectAtIndex(0) as Record), newTask: "Taskless Records")
          }
      } else {
          for var i:UInt = 0; i &lt; loopCount; ++i {
              Database.deleteRecord(record: currentTask.records.objectAtIndex(0) as Record)
          }
      }
  
      realm.beginWriteTransaction()
      realm.deleteObject(currentTask)
      realm.commitWriteTransaction()
  }
  </code></pre>
  
  <p>The program throws the exception on the line,</p>
  
  <pre><code>realm.commitWriteTransaction()
  </code></pre>
  
  <p>And the loops are moving or deleting all of the child database objects under the Task before attempting to delete the task. Both moving and deleting the records works properly. Also one last important note, if the task doesn't contain records under it then there is no issue deleting it and no exception thrown.</p>
  
  <p>I appreciate anyone's help, I've been banging my head on a brick wall for this issue.</p>
  
  <hr>
  
  <p><strong>--New Changes based on the reply from yoshyosh--</strong></p>
  
  <p>I have since modified my code to the following. I still receive the same error.</p>
  
  <pre><code>public class func deleteTask(#taskName: String, retainRecords: Bool) {
      let realm = Database.getRealm()
      let currentTask = (Task.objectsWhere("name = '\(taskName)'").firstObject() as Task)
      let currentRecords = currentTask.records
      let parent = currentTask.parent
  
      if (retainRecords) {
          let loopCount:UInt = currentTask.records.count
  
          for var i:UInt = 0; i &lt; loopCount; ++i {
              Database.moveRecord(record: (currentTask.records.firstObject() as Record), newTaskName: "Taskless Records")
          }
      } else {
          realm.transactionWithBlock { () -&gt; Void in
              realm.deleteObjects(currentRecords)
          }
      }
  
      realm.transactionWithBlock { () -&gt; Void in
          realm.deleteObject(currentTask)
      }
  }
  
  public class func moveRecord(#record: Record, newTaskName: String) {
      let realm = Database.getRealm()
      let oldTask = record.parent
      let oldIndex = oldTask.records.indexOfObject(record)
      let newTask = (Task.objectsWhere("name = '\(newTaskName)'")).firstObject() as Task
  
      realm.transactionWithBlock { () -&gt; Void in
          oldTask.records.removeObjectAtIndex(oldIndex)
          newTask.records.addObject(record)
      }
  }
  
  public class func addTask(#name: String, memo: String, time: Double, categoryName: String) {
      let realm = Database.getRealm()
      let parentCategory = (Category.objectsWhere("name = '\(categoryName)'")).firstObject() as Category
  
      realm.transactionWithBlock { () -&gt; Void in
          let newTask = Task()
  
          newTask.parent = parentCategory
          newTask.name = name
          newTask.memo = memo
          newTask.timeRemaining = time
  
          parentCategory.tasks.addObject(newTask)
      }     
  }
  
  public class func addRecord(#taskName: String, note: String, timeSpent: Double, date: NSDate) {
      let realm = Database.getRealm();
  
      let parentTask = Task.objectsWhere("name = '\(taskName)'").firstObject() as Task
      realm.transactionWithBlock { () -&gt; Void in
          let newRecord = Record()
  
          newRecord.parent = parentTask
          newRecord.note = note
          newRecord.timeSpent = timeSpent
          newRecord.date = date
  
          parentTask.records.addObject(newRecord)
      }
  }
  </code></pre>
  
  <p>If needed I can also add the code used to add new Records and new Tasks to the Database. Yoshyosh I appreciate your comment as it did help me clean up my code, unfortunately the error is still being thrown when deleting a Task. </p>
  
  <p>One strange thing I've notice while testing the app, is that when I try deleting the Task it crashes, but once I open the app again the Task and all Records are deleted.</p>
  
  <hr>
  
  <p><strong>-- Solution --</strong></p>
  
  <p>I found that I was looking in the completely wrong spot for the error. it was caused by a TableView View Controller that populated with records from a task that was passed into it. It used an RLMArray to retrieve the recordList from the task and used a notificationToken to reload tableView data.</p>
  
  <pre><code>var currentTask:Task!
  var recordList:RLMArray!
  var notificationToken: RLMNotificationToken?
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      recordList = currentTask.records
  
      notificationToken = RLMRealm.defaultRealm().addNotificationBlock { note, realm in
          self.tableView.reloadData()
      }
  }
  
  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return Int(self.recordList.count)
  }
  
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      return Factory.prepareRecordCell(tableView: tableView, recordList: self.recordList, indexPath: indexPath)
  }
  
  func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
      return 1
  }
  </code></pre>
  
  <p>When I would delete a task from a separate view it would try to reload data using the 'recordsList' RLMArray of a task that was deleted.</p>
  
  <p>After removing the notificationToken and manually reloading tableView data inside the viewWillAppear() function deleting tasks works flawlessly.</p>