解析平台的新手:关于获取关系的 objectId 的问题

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

我有以下 tables

我正在为用户输入日期和时间的餐厅创建 table 可用性搜索。结果显示 table 可用 AKA 尚未在所选 date/time.

保留
  1. 我正在检查特定餐厅的 date/time 是否有 table:
    • 这个objectID是从之前的activity(餐厅简介)
    • 传来的
ParseQuery<ParseObject> restaurant = ParseQuery.getQuery("Restaurants");
restaurant.get(objectID);
  1. 我从 Table table
  2. 中获取与餐厅关联的 tables
ParseQuery<ParseObject> tableQuery = ParseQuery.getQuery("Table");
tableQuery.whereMatchesQuery("restaurant_id", restaurant);
  1. 然后我得到与 Reservation table 上的那些 table 相关联的预订,并将它们收集到 reservationResults 列表中
ParseQuery<ParseObject> reservations = ParseQuery.getQuery("Reservation");
reservations.whereMatchesQuery("table_id", tableQuery);
reservations.include("table_id");
List<ParseObject> reservationResults = reservations.find();
  1. 从这里开始,我尝试遍历 ParseObjects 列表并在保留 date/time 上做一些 calculations/checks。 IF 开始时选择的 date/time 与任何已进行的预订冲突 date/times --> THEN I获取冲突对象的 table objectId,并将它们添加到 'List tableListToExclude'。然后使用此列表检查某些 tables 是否未显示在 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?我在这里尝试了几种不同的场景,但没有成功,我想我已经很接近了,但有些东西我还没有完全理解或遗漏...

另外,也欢迎任何关于基于 date/time 过滤掉我的 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 以获得更好的应用性能。