gpt4 book ai didi

java - Firebase 可扩展性 - 查询 DB 与自定义对象 ArrayList?

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:26 24 4
gpt4 key购买 nike

我不确定我是否正确理解了一些文献。 Android 文档说不要创建不必要的对象。本文 ( Are Firebase queries scalable ) 提到数据库查询的可扩展性是可以的,但我也读到最好将查询存储到 ArrayList 中并通过它进行搜索,而不是查询大型数据库。

就我而言,我使用的是 Android 版 Firebase 实时数据库,我想知道如果我得到的子节点可能有 200 个示例/子节点,我是否应该将每个快照放入模型类中,然后将每个快照添加到 ArrayList 中,然后可以在 RecyclerView 中显示?或者我应该在字段上运行 .getValue() 并以其他方式存储它们?

我特别希望查看与雇主关联的公司 ID,然后转到“公司”节点并获取该雇主的企业名称和企业城市

数据库:

FirebaseDB

这是我在 Activity 中的代码部分:

companiesRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()) {

Log.i("companiesSnap", ds.toString());
Log.i("KEYs", ds.getKey());
companyIDKey = ds.getKey();
//For each company ID in the Arraylist
for (int i = 0; i < myCompanyIDsList.size(); i++) {
//IF the IDs in arraylist from employee matches CompanyID from Companies node
if(myCompanyIDsList.get(i).equals(companyIDKey)) {
//IF THE ID matches, then get the associated company info
Log.i("city", ds.child("businessCity").getValue().toString());
Log.i("name", ds.child("businessName").getValue().toString());
Businesses business = new Businesses(ds);
myBusinessListItems.add(business);
mAdapter.updateDataSet(myBusinessListItems);
}
}

全类:

public class BusinessesActivity extends Activity {

private Context mContext;
LinearLayout mLinearLayout;
private RecyclerView mRecyclerView;
private MyAdapterBusiness mAdapter = new MyAdapterBusiness(this);
private RecyclerView.LayoutManager mLayoutManager;
ArrayList<Businesses> myBusinessListItems;
ArrayList<String> myCompanyIDsList;
DatabaseReference employeesRefID, companiesRef;
FirebaseDatabase database;

String currentUser, companyIDKey;

TextView getData;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_businesses);


//RECYCLERVIEW STUFF
mRecyclerView = (RecyclerView) findViewById(R.id.b_recycler_view);

mContext = getApplicationContext(); // Get the application context

// Define a layout for RecyclerView
mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false); //mLayoutManager = new LinearLayoutManager(mContext);
mRecyclerView.setLayoutManager(mLayoutManager);

// Initialize a new instance of RecyclerView Adapter instance
mRecyclerView.setAdapter(mAdapter);

//ARRAY List to Store EACH Company ID
myCompanyIDsList = new ArrayList<String>();

myBusinessListItems = new ArrayList<Businesses>();
currentUser =
FirebaseAuth.getInstance().getCurrentUser().getUid();

database = FirebaseDatabase.getInstance();

getData = (TextView) findViewById(R.id.getData);

companiesRef = database.getReference("Companies").child("CompanyIDs");


final DataSnapshotCallback callback = new DataSnapshotCallback() {
@Override
public void gotDataSnapshot(DataSnapshot snapshot) {
EmployeeUser employee = new EmployeeUser(snapshot);


//myCompanyIDsList.add(employee);

try {
for (DataSnapshot ds : snapshot.getChildren()) {
//WITHIN each UserId check the PushID
Log.i("TAG", "checkIfIDExists: datasnapshot: " + ds);

myCompanyIDsList.add(ds.getValue(EmployeeUser.class).getID());
Log.i("arrayList", myCompanyIDsList.toString());

//}
}

//GO THROUGH EACH COMPANY ID AND FIND INFORMATION
companiesRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()) {

Log.i("companiesSnap", ds.toString());
Log.i("KEYs", ds.getKey());
companyIDKey = ds.getKey();
//For each company ID in the Arraylist
for (int i = 0; i < myCompanyIDsList.size(); i++) {
//IF the IDs in arraylist from employee matches CompanyID from Companies node
if(myCompanyIDsList.get(i).equals(companyIDKey)) {
//IF THE ID matches, then get the associated company info
Log.i("city", ds.child("businessCity").getValue().toString());
Log.i("name", ds.child("businessName").getValue().toString());
Businesses business = new Businesses(ds);
myBusinessListItems.add(business);
mAdapter.updateDataSet(myBusinessListItems);
}
}


}

}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("Cancelled", databaseError.toString());
}

}); //END COMPANIES EVENT LISTENER

//} //END FOR
} //END TRY

catch (Exception e) {
Log.i("FNull?", e.toString());
}

//mAdapter.updateDataSet(myListItems);
}
};


getData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
employeesRefID = database.getReference("Employees").child(currentUser).child("Companies"); //SEE HOW ADD EMPLOYEES checks
employeesRefID.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

callback.gotDataSnapshot(dataSnapshot);
}


@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("Cancelled", databaseError.toString());
}

}); //END EMPLOYEE EVENT LISTENER
Log.i("OutsideEvent", myCompanyIDsList.toString());

}
}); //END ONCLICK
Log.i("OutsideonClick", myCompanyIDsList.toString());
}


interface DataSnapshotCallback {
void gotDataSnapshot(DataSnapshot snapshot);
}
}

最佳答案

使用 Firebase 时减少内存使用量的技巧是仅加载要向用户显示的数据。

如果您需要仅包含公司名称的列表,请创建仅包含数据库中的公司名称的节点并显示该节点。这样您就可以减少所使用的带宽和内存,因为您没有加载公司的其他属性。

您通常会有一个“主列表”,其中包含每个公司(或其他实体类型)的所有属性。然后,您可能有一个或多个“显示列表”,其中仅包含您需要在某些区域显示的业务信息。这种重复数据在 NoSQL 数据库中很常见,称为非规范化。

如需更好的介绍,请阅读 Denormalizing Your Data is Normal , NoSQL data modeling ,并观看 Firebase for SQL developers .

关于java - Firebase 可扩展性 - 查询 DB 与自定义对象 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48689541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com