【问题标题】:New to Parse Platform: Question on getting objectId of a relation解析平台的新手:关于获取关系的 objectId 的问题
【发布时间】:2020-10-15 16:58:14
【问题描述】:

我有以下表格

  • 餐厅
  • 预订

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

  1. 我正在检查特定餐厅的餐桌日期/时间:
    • 此 objectID 是从上一个活动(餐厅配置文件)传递的
ParseQuery<ParseObject> restaurant = ParseQuery.getQuery("Restaurants");
restaurant.get(objectID);
  1. 我从 Table 表中获取与 Restaurant 关联的表
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. 从这里我尝试遍历 ParseObjects 列表并对预订日期/时间进行一些计算/检查。 IF 开始时选择的日期/时间与任何已预订的日期/时间发生冲突 --> THEN 我抓住那些发生冲突的表 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));
                    }
                }
            }
        });

【问题讨论】:

    标签: java android parse-platform back4app


    【解决方案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 以获得更好的应用性能。

    【讨论】:

    • 感谢您帮助我。我按照你建议的方式实现了它,但我仍然得到这个异常:java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseObject.getObjectId()' on a null object reference.
    • 你能用你正在尝试的整个当前代码更新你的问题吗?您还可以确认是哪一行引发了错误吗?
    • 正如我们在另一个聊天中谈到的,您需要确保 table_id 是指向 Table 类的类型指针列。
    • 再次感谢,你帮了我很大的忙!
    • 没问题。很高兴你知道了。
    猜你喜欢
    • 1970-01-01
    • 2011-02-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 2011-04-14
    • 2012-09-30
    • 2011-11-11
    相关资源
    最近更新 更多