【问题标题】:for-loop doesn't work in android try-catch blockfor-loop 在 android try-catch 块中不起作用
【发布时间】:2017-05-08 08:11:44
【问题描述】:

我正在使用 Realm 制作学校建筑和教室的数据库。但是,try-catch 中的“for-loop”不起作用:

public void startCheckRealm() {
    // Writing DataBase with Realm
    try {
        Log.d("Realm", "Init");
        InitializeAPI.init_BuildingRoom(getActivity().getApplicationContext());
        Log.d("Realm", "Complete");
    } catch(Exception e) {
        e.printStackTrace();
    }

    // Trying to check the Database whether it is right or wrong
    try {
        Log.d("Realm Test", "2nd Try Catch");

        Realm.init(getActivity().getApplicationContext());
        Realm realm = Realm.getDefaultInstance();

        RealmResults<BuildingList> buildingLists = realm.where(BuildingList.class).findAllSorted("buildingCode");

        int totalNumber = 0;

        for(int i = 0; i < buildingLists.size(); i++) {
            Log.d("For", "index = " + i);
            RealmResults<RoomList> rooms = buildingLists.get(i).getRoomList().sort("roomCode");

            String BuildingName = buildingLists.get(i).getBuildingName();
            String BuildingCode = buildingLists.get(i).getBuildingCode();

            for(int idx = 0; idx < rooms.size(); idx++) {
                totalNumber++;
                String RoomCode = rooms.get(idx).getRoomCode();
                String RoomName = rooms.get(idx).getRoomName();

                Log.d("Realm Test", "Number :: " + String.valueOf(totalNumber) + "   BuildingCode :: " + BuildingCode + "\t\t BuildingName :: " + BuildingName + "\t\t RoomCode :: " + RoomCode + "\t\t RoomName :: " + RoomName);
            }
        }
        Log.d("Realm Test", "2nd Try Catch Complete + " + String.valueOf(totalNumber));
    } catch(RealmException e) {
        e.printStackTrace();
    }
}

在第一个try-catch中,做数据库的方法是完整的,没有异常。我很好奇这个数据库是对还是错。

所以,在第二次 try-catch 中,我尝试使用查询检查领域文件。

问题是“for-loop”在第二次 try-catch 中不起作用。 sn-p 下面是我的 logcat。

D/Realm: Init
I/System.out: bdList getLength :: 52
I/System.out: roomList getLength :: 2376
D/Realm: Complete
D/Realm Test: 2nd Try Catch
D/Realm Test: 2nd Try Catch Complete + 0

我想用 Log 检查我的领域数据,但是如您所见,它不起作用。

如果没有问题,logcat 会显示很多我的建筑和房间列表,并以“D/Realm Test: 2nd Try Catch Complete + 2376”结尾。

你能解释一下它不起作用的原因吗?即使没有异常,我也无法理解为什么它不起作用。

【问题讨论】:

  • buildingLists 为空?
  • 您的查询很可能是错误的,并返回零记录。我对 Realm 一点也不熟悉,但你能手动调查一下数据库,看看记录是否真的存在吗?
  • @Jerry06 也许不。 InitializeAPI.init_BuildingRoom(getActivity().getApplicationContext());第一个 try-catch 中的方法生成 52 个 buildingList。在logcat中可以看到buildingList的长度。
  • @KenY-N 如果我连续调用 2 次方法,它会说“buildingList 的主键重复”。 (当然,我调用了 2 次方法。)也就是说,我在领域中的写作数据库似乎是正确的,不是吗?
  • 试试把 Log.d("buildingLists.size() ", buildingLists.size());在你的循环之前

标签: java android for-loop try-catch realm


【解决方案1】:

虽然在您的用例中这不会造成问题,但当您在事务中迭代 RealmResults 时,结果会出现在每个版本 = 3.0.0 中。

那么在这种情况下,

    RealmResults<BuildingList> buildingLists = realm.where(BuildingList.class).findAllSorted("buildingCode");
    for(int i = 0; i < buildingLists.size(); i++) {
        BuildingList buildingList = buildingLists.get(i); // <-- !!!

会失败(它会跳过第二个项目!)

所以你应该改用迭代器(3.0.0+!在

    RealmResults<BuildingList> buildingLists = realm.where(BuildingList.class).findAllSorted("buildingCode");
    for(BuildingList buildingList : buildingLists) { // <-- !!!

之所以可行,是因为迭代器默认创建一个新的快照集合(3.0.0+),并且通过快照上的索引迭代也可以

OrderedRealmCollection<BuildingList> snapshot = buildingLists.createSnapshot();
for(int i = 0; i < ...

【讨论】:

  • 哇.. 我在一个月前的领域官方文档中阅读了“快照”,但我没有准确地抓住它。 (当然,我也不认为“我应该使用它”)我必须阅读它。 :) 谢谢给我新的学习东西!!!
【解决方案2】:

简单:不抛出异常;而你只有你的打印语句循环中。

因此,唯一的结论是:在执行 for 循环的那个时间点,相应的列表是。因此没有进入循环体;什么都没有打印出来。这与此循环位于 try-catch 块中这一事实无关

这就是全部。所以,直接的答案是:在循环前面直接打印列表大小,以避免出现这种意外。

(当然,有趣的部分是了解之前似乎非空的列表发生了什么 - 但为了调试它,您必须添加 更多您的代码)。

【讨论】:

  • 是的。你是对的。列表大小为 0。但是,我很好奇为什么我的列表没有使用上述方法更新,命名为“InitializeAPI.init_BuildingRoom(getActivity().getApplicationContext());”......我应该更多地研究我的项目:(
  • 当然。除此之外:您可以对有用的内容进行投票;如果您找到“回答”问题的答案之一,请考虑接受。就个人而言,我建议您退后一步,针对“列表不为空然后变为空”的问题创建一个minimal reproducible example,然后写一个新问题。
【解决方案3】:

两个输入: 1.没用过realm,但是获取排序条目的语法好像有点不一样Official documentation

2.如果上述观点与您的代码相比是错误的,则 buildingList 的大小为零。你试过检查尺寸吗?

让我知道结果。

【讨论】:

  • 我认为排序很顺利。但是,你是对的 BuildingList.size() 是 0。我的 logcat 说“buildingLIsts.size() 是 0”:(
  • 尝试用我建议的链接所说的替换读取语法。让我知道。请点赞,如果有帮助谢谢。 :)
【解决方案4】:

尝试记录您的 catch 块。可能包括循环在内的其余代码没有完成,因为您的应用程序被异常捕获。

【讨论】:

  • 我正在编写“e.printStackTrace();”在 catch 块中。不是结束了吗?是否还有其他工作可以在我的 logcat 中打印错误日志?
  • 使用 Log.e(TAG, "Message", e);
【解决方案5】:

你应该在for loop之前调试buildingLists.size();

Log.d("Building List Size ", buildingLists.size()+""); 

在这种情况下你可以找到buildingLists.size();的值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    相关资源
    最近更新 更多