构建Firestore数据以进行有效读取

时间:2019-06-02 08:11:37

标签: firebase google-cloud-firestore

假设我有一个显示餐厅列表和要显示的1000家餐厅的应用程序。

我的第一印象是创建一个@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_memory_game); GridLayout gridLayout = (GridLayout) findViewById(R.id.grid_layout_4x4); int numColumns = gridLayout.getColumnCount(); int numRows = gridLayout.getRowCount(); numberOfElements = numColumns +numRows; buttons = new MemoryButtons[numberOfElements]; buttonGraphics = new int[numberOfElements/ 2]; buttonGraphics[0] = R.drawable.button_1; buttonGraphics[1] = R.drawable.button_2; buttonGraphics[2] = R.drawable.button_3; buttonGraphics[3] = R.drawable.button_4; buttonGraphics[4] = R.drawable.button_5; buttonGraphics[5] = R.drawable.button_6; buttonGraphics[7] = R.drawable.button_7; buttonGraphics[8] = R.drawable.button_8; buttonGraphicLocations = new int[numberOfElements]; shuffleButtonGraphics(); for (int r = 0; r < numRows; r++){ for (int c = 0; c < numColumns; c++){ MemoryButtons tempButton = new MemoryButtons(this,r,c,buttonGraphics[buttonGraphicLocations[r* numColumns * c]]); tempButton.setId(View.generateViewId()); buttons[r * numColumns *c] = tempButton; gridLayout.addView(tempButton); } } } protected void shuffleButtonGraphics(){ Random rand = new Random(); for(int i = 0; i < numberOfElements; i++){ buttonGraphicLocations[i] = i% (numberOfElements / 2); } for (int i = 0; i < numberOfElements; i++){ int temp = buttonGraphicLocations[i]; int swapIndex = rand.nextInt(16); buttonGraphicLocations[i] = buttonGraphicLocations[swapIndex]; buttonGraphicLocations[swapIndex] = temp; } } ERROR: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.game/com.example.game.MemoryGame}: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2485) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545) at android.app.ActivityThread.access$1100(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5601) at java.lang.reflect.Method.invoke(Native Method) atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 个餐厅,而每个单独的餐厅都将是collection内的document

上述方法的问题在于,每个用户,Cloud Firestore都会注册1000 collection

我的问题是,是否有更好的方法来存储餐厅以减少读取次数?

1 个答案:

答案 0 :(得分:1)

  

我的第一印象是创建一个餐厅集合,每个餐厅都是该集合中的一个文档。

是的,这是正确的方法。

  

上述方法的问题在于,每个用户Firestore都会注册1000次读取

仅当您一次读取所有文档时,您才需要进行1000次读取操作。但这不是正确的方法,您需要限制获取的数据。关于如何实现此目标,请查看有关order and limit data in Cloud Firestore的官方文档。

另一种最合适的方法是将数据装入较小的块中。这种做法称为分页,可以非常简单地在Cloud Firestore中使用startAt()startAfter()方法使用。

对于Android, this 是推荐的方式,您可以通过将查询游标与limit()方法结合使用来对查询进行分页。我还建议您查看此 video ,以更好地理解。

  

我的问题是,是否有更好的方法来存储餐厅以减少读取次数?

要回答您的问题,问题不是关于存储数据的方式是关于如何读取数据的。