1

I have a MutableObservableArray object which has a binding with observeNext function of Bond framework. At the first opening of the app, I fetch array from user defaults and insert it to this empty array.

My problem is that after I insert element to array, observeNext function called three times not once. What can be the problem?

var list = MutableObservableArray<Task>([])

_ = self.list.observeNext(with: { element in
        if element.diff.count != 0 {
            if element.diff.deletes.count >= 1 && element.collection.count == 0 {
                self.restoreUserDefaults(with: false)
            } else {
                self.saveListToUserDefaults(list: element.collection)
            }
        }
    })

Insert Function:

if let savedTask = userDef.object(forKey: self.userDefaultsKeyForList) as? Data {
    let decoder = JSONDecoder()
    if let loadedList = try? decoder.decode([Task].self, from: savedTask) {
        self.list.batchUpdate({ (a) in
            a.insert(contentsOf: loadedList, at: 0)
        })
    }
}

EDIT: I think when It first initialize array with [] It also gets into observeNext. Is it normal?

chwarr
  • 6,777
  • 1
  • 30
  • 57
Emre Önder
  • 2,408
  • 2
  • 23
  • 73

0 Answers0