解析平台的新功能:有关获取关系的objectId的问题

问题描述

我有下表

  • 餐馆
  • 预订

我正在为用户输入日期和时间的餐厅创建可预订性搜索。结果显示在所选日期/时间尚未保留的可用AKA表。

  1. 我正在检查特定餐厅餐桌的日期/时间:
    • 此objectID是从上一个活动(餐厅资料)中传递的
ParseQuery<ParSEObject> restaurant = ParseQuery.getQuery("Restaurants");
restaurant.get(objectID);
  1. 我从“表格”表中获取与“餐厅”相关的表格
ParseQuery<ParSEObject> tableQuery = ParseQuery.getQuery("Table");
tableQuery.whereMatchesQuery("restaurant_id",restaurant);
  1. 然后,在保留表上获取与这些表关联的保留,并将它们收集到ReservationResults列表中
ParseQuery<ParSEObject> reservations = ParseQuery.getQuery("Reservation");
reservations.whereMatchesQuery("table_id",tableQuery);
reservations.include("table_id");
List<ParSEObject> reservationResults = reservations.find();
  1. 从这里,我尝试遍历ParSEObjects列表,并对保留日期/时间进行一些计算/检查。 如果,在开始时选择的日期/时间与任何已预订的日期/时间相冲突-> 之后,我抓取那些冲突的表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);

    // 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's 2 hour window against the Reservation table
    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,然后将其转换为ParsObject,然后获取objectId?我在这里尝试了几种不同的情况,但是没有运气,我想我很亲近,但是有些事情我还没有完全理解或错过……

此外,我们也欢迎您提供有关基于日期/时间过滤我的recyclerview物品的更好方法的建议!如果您需要更多代码或说明,请告诉我。谢谢!


编辑

当前版本中,我从一开始就对查询进行了调整,并且还使用了findInBackground()而不是find():

//        ParseQuery<ParSEObject> restaurant = ParseQuery.getQuery("Restaurants");
//        restaurant.get(objectID);

        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");

        reservations.findInBackground(new FindCallback<ParSEObject>() {
            @Override
            public void done(List<ParSEObject> reservationResults,ParseException e) {
                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();

                    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) && 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));
                    }
                }
            }
        });

解决方法

请尝试执行步骤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来提高应用程序的性能。