Invalid update: invalid number of rows while using Realm
<p>I keep getting the error below, however when I print the value of getEquippedGear().count I get "1", which is correct. When it prints again during the sellGear completion handler, I get "0". However it throws this error. What am I doing wrong?</p>

<p>Error</p>

<blockquote>
  <p>Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'</p>
</blockquote>

<p>Code</p>

<pre><code>override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -&gt; [UITableViewRowAction]? {

    let actionSell = UITableViewRowAction(style: .Destructive, title: "Sell", handler: {_,_ in
        if let cell = tableView.cellForRowAtIndexPath(indexPath) as? GearTableViewCell {
            if let gear = cell.gear {
                print("DEBUG: " + String(User.getEquippedGear()?.count))
                User.sellGear(self, gear: gear, completionHandler: {
                    print("DEBUG: " + String(User.getEquippedGear()?.count))
                    tableView.beginUpdates()
                    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
                    tableView.endUpdates()
                });
            }
        }
    });
    return [actionSell]
}
</code></pre>

<p>Sell gear</p>

<pre><code>static func sellGear(view: UIViewController, gear: Gear, completionHandler: (() -&gt; Void)?) {
    let alert = UIAlertController.init(title: "Are you sure you want to sell this item?", message: "", preferredStyle: .Alert)

    alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { UIAlertAction in
        view.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)

        completionHandler?()
    }))

    alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { UIAlertAction in
        removeGear(gear)

        User.addGold(500)

        view.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)

        completionHandler?()
    }))

    view.presentViewController(alert, animated: true, completion: nil)
}
</code></pre>

<p>numberOfSectionsInTableView and numberOfRowsInSection</p>

<pre><code>override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
    return 2
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
    switch section {
        case 0:
            guard let data = User.getEquippedGear() else {
                return 1
            }
            if data.count &gt; 0 {
                    print("numberOfRowsInSection - Section: " + String(0) + " Gear Count: " + String(data.count))
                    return data.count
            } else {
                return 1
            }
        case 1:
            guard let data = User.getUnequippedGear() else {
                return 1
            }
            if data.count &gt; 0 {
                print("numberOfRowsInSection - Section: " + String(1) + " Gear Count: " + String(data.count))
                return data.count
            } else {
                return 1
        }
        default:
            return 1
    }
}
</code></pre>

<p>getEquippedGear</p>

<pre><code>static func getEquippedGear() -&gt; Results&lt;Gear&gt;? {
    guard let gear = getGear() else {
        return nil
    } //Gets List&lt;Gear&gt;? from Realm

    return gear.filter("isEquipped == %@", NSNumber(bool: true))
}
</code></pre>