Obtaining And Storing A Firestore Instance In A Long Running Service
I have a static utility class which makes use of Firestore. The database operations are invoked occasionally by a background service. I expect this service to run for months on the user's phone.
I've been initializing an instance of Firestore in a static variable and using that throughout. Are there any bad effects of keeping an instance alive this way?
Is it better to call getInstance every time I need it? It seems like Firestore caches the result of getInstance.
class FSWrapper {
private static FirebaseFirestore fs = FirebaseFirestore.getInstance();
public static operation1() {
// do some reads
}
public static operation2() {
// do some writes
}
}
class BackgroundService extends Service {
public void onSomeEvent() {
FSWrapper.operation1();
FSWrapper.operation2();
.....
}
}
Answer
FirebaseFirestore.getInstance()
returns a singleton object, the same object every time. It's never going to get garbage collected, so you can hold it for as long as you want anywhere you want with no downside (other than not being able to unit test with it correctly - you would want to inject the singleton some other way to do that).
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View