'resignFirstResponder is called when trying to write to Realm

i am trying to update a realm object when a text view is edited inside a UICollectionViewCell:

var realm = try? Realm()
...
extension ItemCell: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        try? self.realm?.write({
            self.item?.string = textView.text
        })
    }
}

but every time i enter a character, the keyboard is dismissed (this only happens when i try to write to my realm object)



Solution 1:[1]

It was a simple mistake, I was observing changes and reloading the cell:

func observeChanges() {
        let results = list.listItems
        notificationToken = results.observe { [weak self] (changes: RealmCollectionChange) in
            switch changes {
            case .initial:
                self?.collectionView.reloadData()
            case .update(_, let deletions, let insertions, let modifications):
                self?.collectionView.performBatchUpdates({
                    self?.collectionView.deleteItems(at: deletions.map({ IndexPath(row: $0, section: 0)}))
                    self?.collectionView.insertItems(at: insertions.map({ IndexPath(row: $0, section: 0) }))
                    self?.collectionView.reloadItems(at: modifications.map({ IndexPath(row: $0, section: 0) }))
                }, completion: { finished in
                    // ...
                })
            case .error(let error):
                fatalError("\(error)")
            }
        }
    }

i just removed the reloadItems(at:) line

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Halpo