I initialize this ValueObservation and I know it initializes but when I add a new course to the db, I get no subsequent notices. I can confirm that a MbyCourse is added to the db.
I see: mbyCourses refreshed: Optional(0)
and no increment after this code is exercised:
Any ideas why no observation happens again? Xcode 10.3, Swift 5, GRDB 4.4.0
Storing Code:
public func addCourse(_ course: MbyCourse,_ forOrgUuid: String) -> Single<MbyCourse> {
return Single.create { [unowned self] single in
let storeCourse: MbyCourse = course
do {
try self.dbPool.writeInTransaction { db in
print("addCourse: \(course)")
try storeCourse.insert(db)
return .commit
}
single(.success(storeCourse))
}
catch {
print("error: \(error)")
single(.error(error))
}
return Disposables.create {}
}
}
ValueObservation code:
let observation = ValueObservation.tracking { db -> Int in
var coursesCount: Int = 0
do {
let mbyCoursesCount = try MbyCourse.fetchCount(db)
coursesCount = mbyCoursesCount
} catch {
print("error observing courses: \(error)")
}
return coursesCount
}
// Start observing the database
let databasePool: DatabasePool = userService.rawDatabasePool()
do {
observer = observation.start(in: databasePool, onError: { error in
print("courses could not be fetched: \(error)")
}, onChange: { [weak self] (mbyCoursesCount: Int?) in
print("mbyCourses refreshed: \(String(describing: mbyCoursesCount))")
})
} catch {
print("courses could not be fetched: \(error)")
}