Search code examples
iosrealmpersistencexcode8.2

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


I've just built a simple Todo App using Realm database. I used Realm Browser 2.1.6 downloaded from Mac Apple Store to persist data. By using Realm Browser, I can edit normally values of existing records and display on Todo App screen, however the new records added by (Command +) are not able to display on screen of simulator. I'm using Xcode 8.2 and swift 3. Am I missing something or is this a bug?

Thanks for your work.

My Best Regards,

Kamogawa


Solution

  • If those new objects you've added to the Realm Browser have definitely persisted (ie, you can close the Realm file and re-open it, and they're still there), then it sounds like you need to add a notification block in your app to detect when the Realm Browser has changed the data and to trigger a UI update.

    If you're displaying those records as a table or a collection view, you can use Realm's collection change notifications to detect when a new object was added.

    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: $0, section: 0) }),
                               with: .automatic)
            tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
                               with: .automatic)
            tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, 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()
      }
    }
    

    If not, then you might need to use another type of Realm notifications.

    In any case, even if your UI in your app doesn't update, Realm objects are live and will auto-update on their own when their values change. You should be able to set breakpoints in your app and examine the objects directly to confirm your data is coming across. Good luck!