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

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

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

问题

我有以下的表格:

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

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

  1. 我在特定餐厅检查桌子的日期/时间:
    • 这个 objectID 是从之前的活动 (餐厅概况) 传递过来的。
ParseQuery<ParseObject> restaurant = ParseQuery.getQuery("Restaurants");
restaurant.get(objectID);
  1. 我从 "Table" 表中获取与餐厅关联的桌子:
ParseQuery<ParseObject> tableQuery = ParseQuery.getQuery("Table");
tableQuery.whereMatchesQuery("restaurant_id", restaurant);
  1. 然后我从 "Reservation" 表中获取与这些桌子关联的预约,并将它们收集在 reservationResults 列表中:
ParseQuery<ParseObject> reservations = ParseQuery.getQuery("Reservation");
reservations.whereMatchesQuery("table_id", tableQuery);
reservations.include("table_id");
List<ParseObject> reservationResults = reservations.find();
  1. 从这里开始,我尝试遍历 ParseObject 的列表,并在预约的日期/时间上进行一些计算和检查。如果开始选择的日期/时间与任何已经预约的日期/时间发生冲突,那么我会获取这些冲突的桌子的 objectId,并将它们添加到一个名为 tableListToExclude 的列表中。然后,这个列表会用于在 recyclerView 中检查是否不显示为所选时间预订的特定桌子。
for(int i = 0; i < reservationResults.size(); i++) {

    Date reservationDateTime = reservationResults.get(i).getDate("time");
    ParseObject reservation = reservationResults.get(i);
    ParseObject table = reservation.getParseObject("table_id");
    String tableId = table.getObjectId();
    Log.i(TAG, "|||||||||||TEST|||||||| ------> " + tableId);

    // 计算预约的两小时窗口
    Calendar calRes = Calendar.getInstance();
    assert reservationDateTime != null;
    calRes.setTime(reservationDateTime);
    calRes.add(Calendar.HOUR_OF_DAY, 2);
    // 设置两小时窗口的结束时间
    Date reservationEndTime = calRes.getTime();

    // 计算所选时间的两小时窗口
    Calendar calSelected = Calendar.getInstance();
    calSelected.setTime(combinedSelectedDate);
    calSelected.add(Calendar.HOUR_OF_DAY, 2);
    // 设置所选时间的结束时间
    Date selectedEndTime = calSelected.getTime();

    // 将所选日期/时间及其两小时窗口与预约表中的时间进行比较
    if((combinedSelectedDate.after(reservationDateTime) && combinedSelectedDate.before(reservationEndTime)) || (selectedEndTime.after(reservationDateTime) && selectedEndTime.before(reservationEndTime))) {
        Log.i(TAG, "||||||||||||TEST||||||||||||| ---------> Some dates fall in the 2 hour window");
        tableListToExclude.add(tableId);
        System.out.println(Arrays.asList(tableListToExclude));
    }
}

在 logcat 中的错误:

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)
ParseQuery&lt;ParseObject&gt; restaurant = ParseQuery.getQuery(&quot;Restaurants&quot;);
restaurant.get(objectID);
  1. I get the tables associated with the Restaurant from the Table table
ParseQuery&lt;ParseObject&gt; tableQuery = ParseQuery.getQuery(&quot;Table&quot;);
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
ParseQuery&lt;ParseObject&gt; reservations = ParseQuery.getQuery(&quot;Reservation&quot;);
reservations.whereMatchesQuery(&quot;table_id&quot;, tableQuery);
reservations.include(&quot;table_id&quot;);
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
for(int i = 0; i &lt; reservationResults.size(); i++) {

    Date reservationDateTime = reservationResults.get(i).getDate(&quot;time&quot;);
    ParseObject reservation = reservationResults.get(i);
    ParseObject table = reservation.getParseObject(&quot;table_id&quot;);
    String tableId = table.getObjectId();
    Log.i(TAG, &quot;|||||||||||TEST|||||||| ------&gt; &quot; + tableId);

    // calculates the two hour window of the reservation
    Calendar calRes = Calendar.getInstance();
    assert reservationDateTime != null;
    calRes.setTime(reservationDateTime);
    calRes.add(Calendar.HOUR_OF_DAY, 2);

    //sets the end time of two hour window
    Date reservationEndTime = calRes.getTime();

    // calculates two hours window of selected time
    Calendar calSelected = Calendar.getInstance();
    calSelected.setTime(combinedSelectedDate);
    calSelected.add(Calendar.HOUR_OF_DAY, 2);

    // sets the end time of the selected
    Date selectedEndTime = calSelected.getTime();

    //compares the selected date/time and it&#39;s 2 hour window against the Reservation table
    if((combinedSelectedDate.after(reservationDateTime) &amp;&amp; combinedSelectedDate.before(reservationEndTime)) || (selectedEndTime.after(reservationDateTime) &amp;&amp; selectedEndTime.before(reservationEndTime))) {
     Log.i(TAG, &quot;||||||||||||TEST||||||||||||| ---------&gt; Some dates fall in the 2 hour window&quot;);

           tableListToExclude.add(tableId);
           System.out.println(Arrays.asList(tableListToExclude));
    }
}

The error I get in the logcat:

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():

//        ParseQuery&lt;ParseObject&gt; restaurant = ParseQuery.getQuery(&quot;Restaurants&quot;);
//        restaurant.get(objectID);

        ParseQuery&lt;ParseObject&gt; tableQuery = ParseQuery.getQuery(&quot;Table&quot;);
        tableQuery.whereEqualTo(&quot;restaurant_id&quot;, ParseObject.createWithoutData(&quot;Restaurants&quot;, objectID));

        ParseQuery&lt;ParseObject&gt; reservations = ParseQuery.getQuery(&quot;Reservation&quot;);
        reservations.whereMatchesQuery(&quot;table_id&quot;, tableQuery);
        reservations.include(&quot;table_id&quot;);

        reservations.findInBackground(new FindCallback&lt;ParseObject&gt;() {
            @Override
            public void done(List&lt;ParseObject&gt; reservationResults, ParseException e) {
                for(int i = 0; i &lt; reservationResults.size(); i++) {
                    
                    Date reservationDateTime = reservationResults.get(i).getDate(&quot;time&quot;);

                    ParseObject reservation = reservationResults.get(i);
                    ParseObject table = reservation.getParseObject(&quot;table_id&quot;);
                    String tableId = table.getObjectId();

                    System.out.println(tableId);

                    // calculates the two hour window of the reservation
                    Calendar calRes = Calendar.getInstance();
                    assert reservationDateTime != null;
                    calRes.setTime(reservationDateTime);
                    calRes.add(Calendar.HOUR_OF_DAY, 2);

                    //sets the end time of two hour window
                    Date reservationEndTime = calRes.getTime();

                    // calculates two hours window of selected time
                    Calendar calSelected = Calendar.getInstance();
                    calSelected.setTime(combinedSelectedDate);
                    calSelected.add(Calendar.HOUR_OF_DAY, 2);

                    // sets the end time of the selected
                    Date selectedEndTime = calSelected.getTime();

                    //compares the selected date and time against the reservation times of the tables
                    if((combinedSelectedDate.after(reservationDateTime) &amp;&amp; combinedSelectedDate.before(reservationEndTime)) || (selectedEndTime.after(reservationDateTime) &amp;&amp; selectedEndTime.before(reservationEndTime))) {
                        Log.i(TAG, &quot;||||||||||||TEST||||||||||||| ---------&gt; Some dates fall in the 2 hour window&quot;);

                        tableListToExclude.add(tableId);
                        System.out.println(Arrays.asList(tableListToExclude));
                    }
                }
            }
        });

答案1

得分: 1

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

ParseQuery<ParseObject> tableQuery = ParseQuery.getQuery("Table");
tableQuery.whereEqualTo("restaurant_id", ParseObject.createWithoutData("Restaurants", objectID));

ParseQuery<ParseObject> reservations = ParseQuery.getQuery("Reservation");
reservations.whereMatchesQuery("table_id", tableQuery);
reservations.include("table_id");
List<ParseObject> reservationResults = reservations.find();

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

英文:

Try this for steps 1-3:

ParseQuery&lt;ParseObject&gt; tableQuery = ParseQuery.getQuery(&quot;Table&quot;);
tableQuery.whereEqualTo(&quot;restaurant_id&quot;, ParseObject.createWithoutData(&quot;Restaurants&quot;, objectID));

ParseQuery&lt;ParseObject&gt; reservations = ParseQuery.getQuery(&quot;Reservation&quot;);
reservations.whereMatchesQuery(&quot;table_id&quot;, tableQuery);
reservations.include(&quot;table_id&quot;);
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:

确定