I am trying my hands on Firebase for the first time and I ran into kind of a problem.
Getting data out of my Firebase storage/database only works if the getter method fits the variable name or the member variables are public. But my naming convention for member variables is mVariableName
and i leave that "m" out of my getter methods name. Now I have multiple questions:
Is making the model member variables public a viable option or is that bad practice?
What is the best approach here for naming? Should i name the getter methods getmName
or should i leave the "m" out of the member variable names? Should I then change it for the whole project or just for this class?
I just want to know what the best practices are here.
This is the class that reads the entries:
public class ImagesActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ImageAdapter mAdapter;
private FirebaseStorage mFirebaseStorage;
private DatabaseReference mDatabase;
private List<Upload> mUploads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_images);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUploads = new ArrayList<>();
mFirebaseStorage = FirebaseStorage.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
Log.i("UPLOAD", "Upload : " + upload.getName());
mUploads.add(upload);
}
mAdapter = new ImageAdapter(getApplicationContext(), mUploads);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
And these are the rules:
Database:
{
"rules": {
".read": true,
".write": true
}
}
Storage:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if true;
}
}
}
And the Upload.class (only works if either fields are public or getter method names fit m-convention, which is ugly):
public class Upload {
public String mName;
public String mImageUrl;
public Upload() {
}
public Upload(String name, String imageUrl) {
if (name.trim().equals("")) {
name = "No Name";
}
mName = name;
mImageUrl = imageUrl;
}
public String getName() {
return mName;
}
public String getImageUrl() {
return mImageUrl;
}
}