Reference documentation and code samples for the Cloud Firestore API class Google::Cloud::Firestore::QueryListener.
QueryListener
An ongoing listen operation on a query. This is returned by calling Google::Cloud::Firestore::Query#listen.
Inherits
- Object
Includes
- MonitorMixin
Example
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new # Create a query query = firestore.col(:cities).order(:population, :desc) listener = query.listen do |snapshot| puts "The query snapshot has #{snapshot.docs.count} documents " puts "and has #{snapshot.changes.count} changes." end # When ready, stop the listen operation and close the stream. listener.stop
Methods
#last_error
def last_error() -> Exception, nil
The most recent unhandled error to occur while listening for changes.
If an unhandled error has occurred the listener will attempt to recover from the error and resume listening.
- (Exception, nil) — error The most recent error raised.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new # Create a query query = firestore.col(:cities).order(:population, :desc) listener = query.listen do |snapshot| puts "The query snapshot has #{snapshot.docs.count} documents " puts "and has #{snapshot.changes.count} changes." end # If an error was raised, it can be retrieved here: listener.last_error #=> nil # When ready, stop the listen operation and close the stream. listener.stop
#on_error
def on_error(&block) { |error| ... }
Register to be notified of errors when raised.
If an unhandled error has occurred the listener will attempt to recover from the error and resume listening.
Multiple error handlers can be added.
- (callback) — The block to be called when an error is raised.
- error (Exception) — The error raised.
- (ArgumentError)
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new # Create a query query = firestore.col(:cities).order(:population, :desc) listener = query.listen do |snapshot| puts "The query snapshot has #{snapshot.docs.count} documents " puts "and has #{snapshot.changes.count} changes." end # Register to be notified when unhandled errors occur. listener.on_error do |error| puts error end # When ready, stop the listen operation and close the stream. listener.stop
#stop
def stop()
Stops the client listening for changes.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new # Create a query query = firestore.col(:cities).order(:population, :desc) listener = query.listen do |snapshot| puts "The query snapshot has #{snapshot.docs.count} documents " puts "and has #{snapshot.changes.count} changes." end # When ready, stop the listen operation and close the stream. listener.stop
#stopped?
def stopped?() -> Boolean
Whether the client has stopped listening for changes.
- (Boolean)
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new # Create a query query = firestore.col(:cities).order(:population, :desc) listener = query.listen do |snapshot| puts "The query snapshot has #{snapshot.docs.count} documents " puts "and has #{snapshot.changes.count} changes." end # Checks if the listener is stopped. listener.stopped? #=> false # When ready, stop the listen operation and close the stream. listener.stop # Checks if the listener is stopped. listener.stopped? #=> true