I don't if I should ask this question or not but it's confusing me that why some tutors make FirebaseFirestore
object Global and some Local. can anyone please elaborate between these two. Following is the code for your understanding.
PS: I know that Global is accessible by the whole class but Local are not.
Global object of FirebaseFirestore:
public class MainActivity extends AppCompatActivity {
//Global object db.
FirebaseFirestore db = FirebaseFirestore.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//rest of the code.
}
}
Local Object of FirebaseFirestore:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//local object
FirebaseFirestore db = FirebaseFirestore.getInstance();
}
});
}
}
It all comes down to your use case. If you need to make Firebase request in multiple places in your app, a global object would save you the time and resources to reinitialize FirebaseFirestore
. And if you only need to load data just once, or you rarely use it in other places, a local object would be fine.
Personaly I would just put it in global scope and use it anywhere i want without worrying about its scope