使用 Swift 进行领域保存时出现问题。错误的,我直接说 "Delete Object" From Realm Browser 0
Problem With Realm Saving Using Swift. By mistake, I said "Delete Object" directly From Realm Browser 0
为了测试如果我的领域文件中没有任何类别会发生什么,我在领域浏览器中直接删除了 "Categories" 的对象。现在,每当我从我的应用程序添加新项目时,它甚至都不会在 Realm 浏览器中注册。
import UIKit
import CoreData
import RealmSwift
class CategoryViewController: UITableViewController {
let realm = try! Realm()
var categories: Results<Category>?
override func viewDidLoad() {
super.viewDidLoad()
loadCategory()
}
// MARK: - Table View Datasource
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categories?.count ?? 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for : indexPath)
cell.textLabel?.text = categories?[indexPath.row].name ?? "No Categories Added Yet!"
return cell
}
// MARK: - Table View Delegate
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToItems", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! ToDoListViewController
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectCategory = categories![indexPath.row]
}
}
// MARK: - Data Manipulation Methods
func save(category: Category) {
do {
try realm.write {
realm.add(categories!)
}
} catch {
print("There was an error saving context, \(error.localizedDescription)")
}
tableView.reloadData()
}
func loadCategory() {
categories = realm.objects(Category.self)
tableView.reloadData()
}
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Add New Category", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Add", style: .default) { (action) in
let newCategory = Category()
newCategory.name = textField.text!
self.save(category: newCategory)
}
alert.addAction(action)
alert.addTextField { (field) in
textField = field
textField.placeholder = "Add A New Category"
}
present(alert, animated: true, completion: nil)
}
}
当我将名为 "Shopping" 的内容添加到我的类别时:
点击添加按钮后:
添加后我的领域浏览器"Shopping":
请记住,我之前也已将对象添加到领域文件中,所以即使它没有出现在领域文件中,"No Categories Added Yet" 也没有出现...肯定有问题。我没有从调试控制台收到任何错误。
我认为您误将 "categories" 添加到领域中。在 save() 函数中,只需将 "categories" 替换为 "category".
为了测试如果我的领域文件中没有任何类别会发生什么,我在领域浏览器中直接删除了 "Categories" 的对象。现在,每当我从我的应用程序添加新项目时,它甚至都不会在 Realm 浏览器中注册。
import UIKit
import CoreData
import RealmSwift
class CategoryViewController: UITableViewController {
let realm = try! Realm()
var categories: Results<Category>?
override func viewDidLoad() {
super.viewDidLoad()
loadCategory()
}
// MARK: - Table View Datasource
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categories?.count ?? 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for : indexPath)
cell.textLabel?.text = categories?[indexPath.row].name ?? "No Categories Added Yet!"
return cell
}
// MARK: - Table View Delegate
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToItems", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! ToDoListViewController
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectCategory = categories![indexPath.row]
}
}
// MARK: - Data Manipulation Methods
func save(category: Category) {
do {
try realm.write {
realm.add(categories!)
}
} catch {
print("There was an error saving context, \(error.localizedDescription)")
}
tableView.reloadData()
}
func loadCategory() {
categories = realm.objects(Category.self)
tableView.reloadData()
}
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Add New Category", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Add", style: .default) { (action) in
let newCategory = Category()
newCategory.name = textField.text!
self.save(category: newCategory)
}
alert.addAction(action)
alert.addTextField { (field) in
textField = field
textField.placeholder = "Add A New Category"
}
present(alert, animated: true, completion: nil)
}
}
当我将名为 "Shopping" 的内容添加到我的类别时:
点击添加按钮后:
添加后我的领域浏览器"Shopping":
请记住,我之前也已将对象添加到领域文件中,所以即使它没有出现在领域文件中,"No Categories Added Yet" 也没有出现...肯定有问题。我没有从调试控制台收到任何错误。
我认为您误将 "categories" 添加到领域中。在 save() 函数中,只需将 "categories" 替换为 "category".