     
     @available(iOS 11.0, *)
     override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
+
+        let favorite = UIContextualAction(style: .normal, title: "Favorite", handler: { action, view, finished in
+            finished(true)
+            DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
+                self.updateFavoriteState(at: indexPath)
+            }
+        })
+        
         let classAtPath = classObj(at: indexPath)
-
-        let favorite = UIContextualAction(style: .normal, title: "Favorite", handler: { action, view, finished in
-            let realm = try! Realm()
-            try! realm.write {
-                classAtPath.isFavorite = !classAtPath.isFavorite
-            }
-            
-            self.tableView.reloadData()
-            
-            finished(true)
-        })
-        
         favorite.backgroundColor = UIColor.favorite
         
         let image: UIImage = classAtPath.isFavorite ? #imageLiteral(resourceName: "FavoriteIconFilled") : #imageLiteral(resourceName: "FavoriteIcon")

                return
        }
        
        var indexPath: IndexPath
        
        if favoritedClasses.count > 0 {
            indexPath = IndexPath(row: row, section: section + 1)
        } else {
            indexPath = IndexPath(row: row, section: section)
        }
        self.tableView.beginUpdates()
        self.tableView.insertRows(at: [indexPath], with: .automatic)
        self.tableView.reloadSections(IndexSet.init(integer: indexPath.section), with: .automatic)

        if isSearchActive {
            return filteredClasses.count
        } else {
            if section == 0{
                // The favorites section will only contain favorited classes duh...
                return favoritedClasses.count
            } else {
                return classesBySection[section - 1].count
            }
        }
    }
