ValueObservation tracks fetchCount 0 upon initialization, no more updates

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)")
        }

Hi @mazz,

Your sample code does not reveal anything concerning. Make sure you store the observer result of the observation.start method for the whole duration of the observation, because the observation stops when the observer is deallocated. Also make sure that the insertion is actually done, of course. It's worth checking all assumptions when a program does not behave as expected.

If you can't figure out where the problem lies, will you please open a Github issue and provide some sample code that reproduces your problem? Thank you.

Yeah I'll try to narrow-down if it is actually "pilot error" here by adding something to the db on a user event. I'm adding something to the db very early in the app launch cycle so maybe that is causing a race? Will look into it.

So it was "pilot error" after all. I was inadvertently resetting the db connection. I eventually narrowed-down to this root cause by trying to use RxGRDB and I was seeing the exact same problem. Once I fixed the code that was resetting the db connection, ValueObservation started working as expected(and also RxGRDB).

1 Like

Good! It's pretty rare that one has to "reset" a connection (close and reopen): usually apps open one connection to the database(s) they use, and leave them open. This is actually required for observation to work. Do you happen to really need such a reset?

Sorry about late reply. No, I don't need to "reset" the connection. I definitely want a persistent connection to the database. I was under the impression that I had one but it was my error with how I was using swinject as a matter of fact. If I continue to use the original DataStore() pointer that I "register" with in Swinject, it works as expected(persistent). I never noticed the reset before because I wasn't using Transaction Observer before and was not affected by the resetting of the db.