新手使用Parse平台:关于获取关系对象的objectId的问题

huangapple go评论110阅读模式
英文:

New to Parse Platform: Question on getting objectId of a relation

问题

我有以下的表格:

  • 餐厅 (Restaurants)
  • 桌子 (Table)
  • 预约 (Reservation)

我正在为一个餐厅创建一个桌子的可用性搜索功能,用户需要输入日期和时间。搜索结果会显示在所选日期/时间可用的桌子,即在该时间之前没有被预订的桌子。

  1. 我在特定餐厅检查桌子的日期/时间:
    • 这个 objectID 是从之前的活动 (餐厅概况) 传递过来的。
  1. ParseQuery<ParseObject> restaurant = ParseQuery.getQuery("Restaurants");
  2. restaurant.get(objectID);
  1. 我从 "Table" 表中获取与餐厅关联的桌子:
  1. ParseQuery<ParseObject> tableQuery = ParseQuery.getQuery("Table");
  2. tableQuery.whereMatchesQuery("restaurant_id", restaurant);
  1. 然后我从 "Reservation" 表中获取与这些桌子关联的预约,并将它们收集在 reservationResults 列表中:
  1. ParseQuery<ParseObject> reservations = ParseQuery.getQuery("Reservation");
  2. reservations.whereMatchesQuery("table_id", tableQuery);
  3. reservations.include("table_id");
  4. List<ParseObject> reservationResults = reservations.find();
  1. 从这里开始,我尝试遍历 ParseObject 的列表,并在预约的日期/时间上进行一些计算和检查。如果开始选择的日期/时间与任何已经预约的日期/时间发生冲突,那么我会获取这些冲突的桌子的 objectId,并将它们添加到一个名为 tableListToExclude 的列表中。然后,这个列表会用于在 recyclerView 中检查是否不显示为所选时间预订的特定桌子。
  1. for(int i = 0; i < reservationResults.size(); i++) {
  2. Date reservationDateTime = reservationResults.get(i).getDate("time");
  3. ParseObject reservation = reservationResults.get(i);
  4. ParseObject table = reservation.getParseObject("table_id");
  5. String tableId = table.getObjectId();
  6. Log.i(TAG, "|||||||||||TEST|||||||| ------> " + tableId);
  7. // 计算预约的两小时窗口
  8. Calendar calRes = Calendar.getInstance();
  9. assert reservationDateTime != null;
  10. calRes.setTime(reservationDateTime);
  11. calRes.add(Calendar.HOUR_OF_DAY, 2);
  12. // 设置两小时窗口的结束时间
  13. Date reservationEndTime = calRes.getTime();
  14. // 计算所选时间的两小时窗口
  15. Calendar calSelected = Calendar.getInstance();
  16. calSelected.setTime(combinedSelectedDate);
  17. calSelected.add(Calendar.HOUR_OF_DAY, 2);
  18. // 设置所选时间的结束时间
  19. Date selectedEndTime = calSelected.getTime();
  20. // 将所选日期/时间及其两小时窗口与预约表中的时间进行比较
  21. if((combinedSelectedDate.after(reservationDateTime) && combinedSelectedDate.before(reservationEndTime)) || (selectedEndTime.after(reservationDateTime) && selectedEndTime.before(reservationEndTime))) {
  22. Log.i(TAG, "||||||||||||TEST||||||||||||| ---------> Some dates fall in the 2 hour window");
  23. tableListToExclude.add(tableId);
  24. System.out.println(Arrays.asList(tableListToExclude));
  25. }
  26. }

在 logcat 中的错误:

  1. java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseObject.getObjectId()' on a null object reference

我被卡在这里,但我相信这只是因为我没有完全理解 Parse 以及如何查询关系或指针等等。我是否需要在这里创建另一个查询?还是以某种方式获取 ParseRelation,然后将其转换为 ParseObject,然后获取 objectId?我在这里尝试了一些不同的场景,但都没有成功,我认为我已经很接近了,但是还有一些我没有完全理解或者遗漏的东西...

另外,如果对于基于日期/时间来筛选 RecyclerView 项有更好的方法,也欢迎提出建议!如果需要更多的代码或者说明,请随时告诉我。谢谢!

英文:

I have the following tables

  • Restaurants
  • Table
  • Reservation

I am creating a table availability search for a restaurant where the user inputs date and time. The result shows tables that are available AKA not already reserved at the selected date/time.

  1. I am checking the date/time for tables at a specific restaurant:
    • this objectID is passed from the previous activity (Restaurant profile)
  1. ParseQuery&lt;ParseObject&gt; restaurant = ParseQuery.getQuery(&quot;Restaurants&quot;);
  2. restaurant.get(objectID);
  1. I get the tables associated with the Restaurant from the Table table
  1. ParseQuery&lt;ParseObject&gt; tableQuery = ParseQuery.getQuery(&quot;Table&quot;);
  2. tableQuery.whereMatchesQuery(&quot;restaurant_id&quot;, restaurant);
  1. Then I get the reservations associated with those tables on the Reservation table and collect them in the reservationResults list
  1. ParseQuery&lt;ParseObject&gt; reservations = ParseQuery.getQuery(&quot;Reservation&quot;);
  2. reservations.whereMatchesQuery(&quot;table_id&quot;, tableQuery);
  3. reservations.include(&quot;table_id&quot;);
  4. List&lt;ParseObject&gt; reservationResults = reservations.find();
  1. From here I try to loop through the list of ParseObjects and do some calculations/checks on the reservation date/times. IF the date/time selected at the start clashes with any of the already made reservation date/times --> THEN I grab the table objectId of those that clash and add them to a 'List tableListToExclude'. This list is then used to check that certain tables aren't shown in a recyclerView if they are reserved for the selected time
  1. for(int i = 0; i &lt; reservationResults.size(); i++) {
  2. Date reservationDateTime = reservationResults.get(i).getDate(&quot;time&quot;);
  3. ParseObject reservation = reservationResults.get(i);
  4. ParseObject table = reservation.getParseObject(&quot;table_id&quot;);
  5. String tableId = table.getObjectId();
  6. Log.i(TAG, &quot;|||||||||||TEST|||||||| ------&gt; &quot; + tableId);
  7. // calculates the two hour window of the reservation
  8. Calendar calRes = Calendar.getInstance();
  9. assert reservationDateTime != null;
  10. calRes.setTime(reservationDateTime);
  11. calRes.add(Calendar.HOUR_OF_DAY, 2);
  12. //sets the end time of two hour window
  13. Date reservationEndTime = calRes.getTime();
  14. // calculates two hours window of selected time
  15. Calendar calSelected = Calendar.getInstance();
  16. calSelected.setTime(combinedSelectedDate);
  17. calSelected.add(Calendar.HOUR_OF_DAY, 2);
  18. // sets the end time of the selected
  19. Date selectedEndTime = calSelected.getTime();
  20. //compares the selected date/time and it&#39;s 2 hour window against the Reservation table
  21. if((combinedSelectedDate.after(reservationDateTime) &amp;&amp; combinedSelectedDate.before(reservationEndTime)) || (selectedEndTime.after(reservationDateTime) &amp;&amp; selectedEndTime.before(reservationEndTime))) {
  22. Log.i(TAG, &quot;||||||||||||TEST||||||||||||| ---------&gt; Some dates fall in the 2 hour window&quot;);
  23. tableListToExclude.add(tableId);
  24. System.out.println(Arrays.asList(tableListToExclude));
  25. }
  26. }

The error I get in the logcat:

  1. java.lang.NullPointerException: Attempt to invoke virtual method &#39;java.lang.String com.parse.ParseObject.getObjectId()&#39; on a null object reference

I'm stuck here but I believe it's just from not totally understanding Parse and how to query relationships or pointers etc,... Do I need to create another query here? Or somehow get the ParseRelation and then that into a ParsObject and then get the objectId? I have tried a few different scenarios here but no luck, I think I am close but there's just something I'm not fully understanding or missing...

Also, any suggestions on a better approach to filter out my recyclerview items based on a date/time is welcome too!! If you need any more code or clarification just let me know. Thanks!


EDIT

Current version I have made tweaks to the queries at the start and also used findInBackground() instead of find():

  1. // ParseQuery&lt;ParseObject&gt; restaurant = ParseQuery.getQuery(&quot;Restaurants&quot;);
  2. // restaurant.get(objectID);
  3. ParseQuery&lt;ParseObject&gt; tableQuery = ParseQuery.getQuery(&quot;Table&quot;);
  4. tableQuery.whereEqualTo(&quot;restaurant_id&quot;, ParseObject.createWithoutData(&quot;Restaurants&quot;, objectID));
  5. ParseQuery&lt;ParseObject&gt; reservations = ParseQuery.getQuery(&quot;Reservation&quot;);
  6. reservations.whereMatchesQuery(&quot;table_id&quot;, tableQuery);
  7. reservations.include(&quot;table_id&quot;);
  8. reservations.findInBackground(new FindCallback&lt;ParseObject&gt;() {
  9. @Override
  10. public void done(List&lt;ParseObject&gt; reservationResults, ParseException e) {
  11. for(int i = 0; i &lt; reservationResults.size(); i++) {
  12. Date reservationDateTime = reservationResults.get(i).getDate(&quot;time&quot;);
  13. ParseObject reservation = reservationResults.get(i);
  14. ParseObject table = reservation.getParseObject(&quot;table_id&quot;);
  15. String tableId = table.getObjectId();
  16. System.out.println(tableId);
  17. // calculates the two hour window of the reservation
  18. Calendar calRes = Calendar.getInstance();
  19. assert reservationDateTime != null;
  20. calRes.setTime(reservationDateTime);
  21. calRes.add(Calendar.HOUR_OF_DAY, 2);
  22. //sets the end time of two hour window
  23. Date reservationEndTime = calRes.getTime();
  24. // calculates two hours window of selected time
  25. Calendar calSelected = Calendar.getInstance();
  26. calSelected.setTime(combinedSelectedDate);
  27. calSelected.add(Calendar.HOUR_OF_DAY, 2);
  28. // sets the end time of the selected
  29. Date selectedEndTime = calSelected.getTime();
  30. //compares the selected date and time against the reservation times of the tables
  31. if((combinedSelectedDate.after(reservationDateTime) &amp;&amp; combinedSelectedDate.before(reservationEndTime)) || (selectedEndTime.after(reservationDateTime) &amp;&amp; selectedEndTime.before(reservationEndTime))) {
  32. Log.i(TAG, &quot;||||||||||||TEST||||||||||||| ---------&gt; Some dates fall in the 2 hour window&quot;);
  33. tableListToExclude.add(tableId);
  34. System.out.println(Arrays.asList(tableListToExclude));
  35. }
  36. }
  37. }
  38. });

答案1

得分: 1

尝试以下步骤1-3的代码:

  1. ParseQuery<ParseObject> tableQuery = ParseQuery.getQuery("Table");
  2. tableQuery.whereEqualTo("restaurant_id", ParseObject.createWithoutData("Restaurants", objectID));
  3. ParseQuery<ParseObject> reservations = ParseQuery.getQuery("Reservation");
  4. reservations.whereMatchesQuery("table_id", tableQuery);
  5. reservations.include("table_id");
  6. List<ParseObject> reservationResults = reservations.find();

现在步骤4应该可以工作。请注意,您正在使用find函数,它会阻塞主线程。建议您在应用程序中使用findInBackground以获得更好的性能。

英文:

Try this for steps 1-3:

  1. ParseQuery&lt;ParseObject&gt; tableQuery = ParseQuery.getQuery(&quot;Table&quot;);
  2. tableQuery.whereEqualTo(&quot;restaurant_id&quot;, ParseObject.createWithoutData(&quot;Restaurants&quot;, objectID));
  3. ParseQuery&lt;ParseObject&gt; reservations = ParseQuery.getQuery(&quot;Reservation&quot;);
  4. reservations.whereMatchesQuery(&quot;table_id&quot;, tableQuery);
  5. reservations.include(&quot;table_id&quot;);
  6. List&lt;ParseObject&gt; reservationResults = reservations.find();

Now step 4 should work. Note that you are using find function and it blocks the main thread. So it is recommended to use findInBackground for a better performance on your app.

huangapple
  • 本文由 发表于 2020年10月16日 00:58:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64376403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定