简体   繁体   English

for循环在android try-catch块中不起作用

[英]for-loop doesn't work in android try-catch block

I am making database of my school's building and classroom with Realm. 我正在使用Realm建立我学校建筑物和教室的数据库。 But, 'for-loop' in try-catch doesn't work: 但是,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();
    }
}

In the first try-catch, the method, which does making database, is complete without Exception. 在第一个try-catch中,完成数据库的方法是完整的,没有Exception。 I was curious whether this database is right or wrong. 我很好奇这个数据库是对还是错。

So, in 2nd try-catch, I was trying to check realm files with queries. 因此,在第二次try-catch中,我试图通过查询检查领域文件。

The problem is "for-loop" doesn't work in 2nd try-catch. 问题是“ for-loop”在第二次try-catch中不起作用。 Below snippet is my logcat. 在摘要下方是我的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

I want to check my realm data with Log but, doesn't work as you can see. 我想用Log检查我的领域数据,但是,如您所见,它不起作用。

If there is no problem, the logcat shows lots of my building and room lists and ends with "D/Realm Test: 2nd Try Catch Complete + 2376". 如果没有问题,日志记录器会显示我的许多建筑物和房间列表,并以“ D / Realm测试:2nd Try Catch Complete + 2376”结尾。

Could you explain the reason why it doesn't work? 您能解释一下为什么它不起作用的原因吗? I cannot understand the reason why it doesn't work even though there is no Exception. 即使没有异常,我也无法理解为什么它不起作用的原因。

While in your use-case this doesn't pose a problem, when you're iterating a RealmResults inside a transaction, the results are live in every version <= 0.88.3 and >= 3.0.0. 在您的用例中,这并不构成问题,但是当您在事务内迭代RealmResults时,结果在每个版本== 0.88.3和> = 3.0.0中均有效。

So in that case, 所以在那种情况下

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

will fail (it will skip every second item!) 将会失败(它将跳过第二个项目!)

So you should use iterators instead (3.0.0+! on <= 0.88.3 you'd do reverse iteration) 因此,您应该改为使用迭代器(3.0.0+!在<= 0.88.3上您会进行反向迭代)

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

The reason why this works is because iterators by default create a new snapshot collection (3.0.0+), and iterating by index on a snapshot also works 之所以可行,是因为迭代器默认情况下会创建一个新的快照集合(3.0.0+),并且按快照上的索引进行迭代也是可行的

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

Simple: there is no exception thrown; 很简单:没有抛出异常; and you only have your print statements inside the loop. 你只需要在循环打印语句。

Thus the one and only conclusion: at that point in time when your for loops are executed, the corresponding list is empty . 因此是一个唯一的结论:在执行for循环的那一刻,相应的列表为 Therefore the loop body is not entered; 因此,不会输入循环体。 nothing gets printed. 什么都不会打印。 And that has nothing to do with the fact that this loop is within a try-catch block. 这与该循环位于try-catch块内无关

That is all there is to this. 这就是全部。 So, the direct answer is: print the list size directly in front of the loop to avoid such surprises. 因此,直接的答案是: 直接在循环的前面打印列表大小,以避免出现此类意外情况。

(of course, the interesting part is to understand what happens to the list which seems to be non-empty earlier on - but in order to debug that, you would have to add more of your code). (当然, 有趣的部分是了解列表发生了什么,而该列表在较早时似乎不是空的-但是要进行调试,您必须添加更多代码)。

Two inputs: 1.Haven't used realm but looks like the syntax for getting sorted entriesis a bit different Official documentation 两个输入:1.尚未使用领域,但看起来用于获得排序条目的语法有点不同官方文档

2.If the above point is wrong than from your code it looks like buildingList size is zero. 2.如果以上几点与您的代码错误,则似乎buildingList的大小为零。 Have you tried checking the size? 您是否尝试过检查尺寸?

Let me know the results. 让我知道结果。

Try logging your catch block. 尝试记录您的catch块。 Chances are the rest of the code including the loop didn't complete because your app was caught with the Exception. 很有可能是代码的其余部分,包括循环未完成,因为您的应用程序被异常捕获。

You should debug buildingLists.size(); 您应该调试buildingLists.size(); before for loop for loop之前

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

In that case you can find the value of buildingLists.size(); 在这种情况下,您可以找到buildingLists.size();的值buildingLists.size();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM