pixelspark / rethink-swift

A RethinkDB client driver for Swift
https://pixelspark.nl/2015/rethink-swift-a-swift-driver-for-rethinkdb
76 stars 11 forks source link

Example code #8

Closed tsebastiani closed 7 years ago

tsebastiani commented 7 years ago

Hi, I'm trying to implement a realtime application with rethinkdb and your driver. What I would like to achieve as first task Is to connect to the database and wait for changes in a table. Actually the connection is fine but waiting for changes seems not to work. Do you have any example app that covers the basics of the driver? This is my code: ´´´ R.connect(URL(string: "rethinkdb://192.168.250.96:28015")!, user: "user1", password: "user1") { err, connection in assert(err == nil, "Connection error: (err)")

        R.db("test").table("games").indexWait().run(connection) { response in
            assert(!response.isError, "Failed to wait for index: \(response)")
            R.db("test").table("games").changes().run(connection){ (response:ReResponse) in
                print ("There's a change!!")
            }
        }

    }

´´´ I would expect that any time I insert something in the table "There's a change!!" is printed in console , but that's not actually happening!

Thanks a lot.

pixelspark commented 7 years ago

Hi @tsebastiani! I am not currently behind my Mac so I cannot check your code. Perhaps you can take a look at the test file (https://github.com/pixelspark/rethink-swift/blob/master/Tests/RethinkTests/RethinkTests.swift)? This shows the basic usage patterns. A slightly more complicated usage example is here: https://github.com/pixelspark/warp/blob/master/WarpConduit/Sources/RethinkStream.swift.

alextarrago commented 7 years ago

Hi @pixelspark, same issue here. Following your tests i'm not able to get it working. I establish the connection and everything seems to work perfectly, but when I run the following code:

R.db(db).table(table).changes().run(connection) { response in
    print("Something changed")
}

The 'Something Changed' log is printed when connecting but never more, is that the right way of doing it?

Thanks a lot

pixelspark commented 7 years ago

@alextarrago does your program exit after the first message? The changes().run() command does not block nor keep the program alive by itself. As long as connection is referenced and connected, it should work. Is your program running an NSRunLoop?

alextarrago commented 7 years ago

Thanks for the fast reply @pixelspark. I got it working later yesterday.

R.connect(URL(string: "rethinkdb://\(server):\(port)")!, user: user, password: password) { (err, connection) in
    R.db(db).table(table).indexWait().run(connection) { (response) in
        R.db(db).table(table).changes().run(connection) { response in
            var consumeChanges: ((_ response: ReResponse) -> ())? = nil
            consumeChanges = { (response: ReResponse) -> () in
                if case ReResponse.rows(let docs, let cb) = response {
                    self.updateDocuments(response: response)
                    cb!(consumeChanges!)
                }
                else {
                    print("Received unexpected response for .changes request: \(response)")
                }
            }
            consumeChanges!(response)
        }
    }
}