C
C#16mo ago
A13u

❔ How to close a listener firestore.

public void ListenToDocument(string collectionName, string documentId)
{
DocumentReference docRef = db.Collection(collectionName).Document(documentId);
FirestoreChangeListener listener = docRef.Listen(snapshot =>
{
Console.WriteLine("Callback received document snapshot.");
Console.WriteLine("Document exists? {0}", snapshot.Exists);
if (snapshot.Exists)
{
Console.WriteLine("Document data for {0} document:", snapshot.Id);
Dictionary<string, object> documentData = snapshot.ToDictionary();
foreach (KeyValuePair<string, object> pair in documentData)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
}
});
listener.ListenerTask.Wait(); // Block until the listener is done
public void ListenToDocument(string collectionName, string documentId)
{
DocumentReference docRef = db.Collection(collectionName).Document(documentId);
FirestoreChangeListener listener = docRef.Listen(snapshot =>
{
Console.WriteLine("Callback received document snapshot.");
Console.WriteLine("Document exists? {0}", snapshot.Exists);
if (snapshot.Exists)
{
Console.WriteLine("Document data for {0} document:", snapshot.Id);
Dictionary<string, object> documentData = snapshot.ToDictionary();
foreach (KeyValuePair<string, object> pair in documentData)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
}
});
listener.ListenerTask.Wait(); // Block until the listener is done
how should I close this safely
2 Replies
A13u
A13uOP16mo ago
It's in a class and want to close the listener safely
class FirestoreListener
{
private FirestoreDb db;
private FirestoreChangeListener listener;

public FirestoreListener(string projectId)
{
// Initialize FirestoreDb instance
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "C:\\Users\\abuba\\source\\repos\\tryingAPI\\tryingAPI\\serviceAccountKey.json"); //google certificate
db = FirestoreDb.Create(projectId);
}

public void ListenToDocument(string collectionName, string documentId)
{
DocumentReference docRef = db.Collection(collectionName).Document(documentId);
listener = docRef.Listen(snapshot =>
{
Console.WriteLine("Callback received document snapshot.");
Console.WriteLine("Document exists? {0}", snapshot.Exists);
if (snapshot.Exists)
{
Console.WriteLine("Document data for {0} document:", snapshot.Id);
Dictionary<string, object> documentData = snapshot.ToDictionary();
foreach (KeyValuePair<string, object> pair in documentData)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
}
});
listener.ListenerTask.Wait(); // Block until the listener is done
}

public void stopListener()
{
listener.ListenerTask.Dispose();
}
}
class FirestoreListener
{
private FirestoreDb db;
private FirestoreChangeListener listener;

public FirestoreListener(string projectId)
{
// Initialize FirestoreDb instance
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "C:\\Users\\abuba\\source\\repos\\tryingAPI\\tryingAPI\\serviceAccountKey.json"); //google certificate
db = FirestoreDb.Create(projectId);
}

public void ListenToDocument(string collectionName, string documentId)
{
DocumentReference docRef = db.Collection(collectionName).Document(documentId);
listener = docRef.Listen(snapshot =>
{
Console.WriteLine("Callback received document snapshot.");
Console.WriteLine("Document exists? {0}", snapshot.Exists);
if (snapshot.Exists)
{
Console.WriteLine("Document data for {0} document:", snapshot.Id);
Dictionary<string, object> documentData = snapshot.ToDictionary();
foreach (KeyValuePair<string, object> pair in documentData)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
}
});
listener.ListenerTask.Wait(); // Block until the listener is done
}

public void stopListener()
{
listener.ListenerTask.Dispose();
}
}
Will this work ?
Accord
Accord16mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server