无法使用从 Mac Apple Store 下载的 Realm Browser 2.1.6 添加新记录到领域数据库

Cannot add new record to realm database using Realm Browser 2.1.6 downloaded from Mac Apple Store

我刚刚使用 Realm 数据库构建了一个简单的 Todo 应用程序。我使用从 Mac Apple Store 下载的 Realm Browser 2.1.6 来保存数据。通过使用 Realm Browser,我可以正常编辑现有记录的值并显示在 Todo App 屏幕上,但是通过(Command +)添加的新记录无法在模拟器屏幕上显示。我正在使用 Xcode 8.2 和 swift 3。 我是不是遗漏了什么或者这是一个错误?

感谢您的工作。

我最诚挚的问候,

鸭川

如果您添加到 Realm 浏览器的那些新对象确实存在(即,您可以关闭 Realm 文件并重新打开它,它们仍然存在),那么听起来您需要在您的应用程序中添加一个通知块,以检测领域浏览器何时更改数据并触发 UI 更新。

如果您将这些记录显示为 table 或集合视图,则可以使用 Realm's collection change notifications 来检测何时添加了新对象。

class ViewController: UITableViewController {
  var notificationToken: NotificationToken? = nil

  override func viewDidLoad() {
    super.viewDidLoad()
    let realm = try! Realm()
    let results = realm.objects(Person.self).filter("age > 5")

    // Observe Results Notifications
    notificationToken = results.addNotificationBlock { [weak self] (changes: RealmCollectionChange) in
      guard let tableView = self?.tableView else { return }
      switch changes {
      case .initial:
        // Results are now populated and can be accessed without blocking the UI
        tableView.reloadData()
        break
      case .update(_, let deletions, let insertions, let modifications):
        // Query results have changed, so apply them to the UITableView
        tableView.beginUpdates()
        tableView.insertRows(at: insertions.map({ IndexPath(row: [=10=], section: 0) }),
                           with: .automatic)
        tableView.deleteRows(at: deletions.map({ IndexPath(row: [=10=], section: 0)}),
                           with: .automatic)
        tableView.reloadRows(at: modifications.map({ IndexPath(row: [=10=], section: 0) }),
                           with: .automatic)
        tableView.endUpdates()
        break
      case .error(let error):
        // An error occurred while opening the Realm file on the background worker thread
        fatalError("\(error)")
        break
      }
    }
  }

  deinit {
    notificationToken?.stop()
  }
}

如果没有,那么您可能需要使用其他类型的 Realm 通知。

在任何情况下,即使您的应用程序中的 UI 没有更新,Realm 对象仍然存在并且会在其值更改时自动更新。您应该能够在您的应用程序中设置断点并直接检查对象以确认您的数据正在通过。祝你好运!