Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FindFirst() might not work correctly #1018

Closed
cmelchior opened this issue Apr 8, 2015 · 16 comments
Closed

FindFirst() might not work correctly #1018

cmelchior opened this issue Apr 8, 2015 · 16 comments
Assignees

Comments

@cmelchior
Copy link
Contributor

Copy from: #826 (comment)

I see that this was merged into master in february. But I think I am still have issues related to different results from findFirst() and findAll().first()

I have a RealmResults which I further want to query: mAllUsersForCompany is of type RealmResults:

The following works. It gives me a user whos name has length of three and who was in the mAllUsersForCompany list:

mAllUsersForCompany.where().equalTo("nameLength", 3).findAll().first()
But the following seems to completely ignore the additional equalTo constraint and gives me a user which has namelength != 3

mAllUsersForCompany.where().equalTo("nameLength", 3).findFirst()

@cmelchior
Copy link
Contributor Author

Hi @gillde
I tried to reproduce the behaviour described here, but it looks fine to me. Can you provide code samples or a sample project with a reproducible error?

@cmelchior
Copy link
Contributor Author

We cannot reproduce this behaviour, so closing. Please reopen if there is any new information about this.

@kevinvanmierlo
Copy link

I'd like to reopen this. I found a way to reproduce this behaviour.

Game:

public class Game extends RealmObject
{
    @PrimaryKey
    private long id;

    private long levelId;

    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public long getLevelId()
    {
        return levelId;
    }

    public void setLevelId(long levelId)
    {
        this.levelId = levelId;
    }
}

GameState:

public class GameState extends RealmObject
{
    @PrimaryKey
    private long id;

    private long levelId;

    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public long getLevelId()
    {
        return levelId;
    }

    public void setLevelId(long levelId)
    {
        this.levelId = levelId;
    }
}

MainActivity:

        Realm realm  = Realm.getInstance(this);

        realm.beginTransaction();
        for(int i = 0; i < 10; i++)
        {
            Game game = new Game();
            game.setId(i);
            game.setLevelId(1);
            realm.copyToRealmOrUpdate(game);
        }
        realm.commitTransaction();

        realm.beginTransaction();
        for(int i = 11; i < 20; i++)
        {
            Game game = new Game();
            game.setId(i);
            game.setLevelId(2);
            realm.copyToRealmOrUpdate(game);
        }
        realm.commitTransaction();

        RealmResults<Game> games = realm.where(Game.class).equalTo("levelId", 2).findAll();

        RealmResults<GameState> gameStates = realm.where(GameState.class).equalTo("levelId", 2).findAll();
        if(gameStates.size() < games.size())
        {
            for(Game game : games)
            {
                if(gameStates.where().equalTo("id", game.getId()).count() == 0)
                {
                    realm.beginTransaction();
                    GameState stateGame = new GameState();
                    stateGame.setId(game.getId());
                    stateGame.setLevelId(game.getLevelId());
                    realm.copyToRealm(stateGame);
                    realm.commitTransaction();
                }
            }
        }

        gameStates = realm.where(GameState.class).equalTo("levelId", 2).findAll();
        for(Game game : games)
        {
            GameState gameState = gameStates.where().equalTo("id", game.getId()).findFirst();
            GameState gameState1 = gameStates.where().equalTo("id", game.getId()).findAll().first();

            Log.d("TAG", "gameState: " + gameState + " | gameState1: " + gameState1);
        }

These are the results:

05-22 09:52:27.807  19230-19230/? D/TAG﹕ gameState: GameState = [{id:0},{levelId:1}] | gameState1: GameState = [{id:11},{levelId:2}]
05-22 09:52:27.807  19230-19230/? D/TAG﹕ gameState: GameState = [{id:1},{levelId:1}] | gameState1: GameState = [{id:12},{levelId:2}]
05-22 09:52:27.808  19230-19230/? D/TAG﹕ gameState: GameState = [{id:2},{levelId:1}] | gameState1: GameState = [{id:13},{levelId:2}]
05-22 09:52:27.808  19230-19230/? D/TAG﹕ gameState: GameState = [{id:3},{levelId:1}] | gameState1: GameState = [{id:14},{levelId:2}]
05-22 09:52:27.808  19230-19230/? D/TAG﹕ gameState: GameState = [{id:4},{levelId:1}] | gameState1: GameState = [{id:15},{levelId:2}]
05-22 09:52:27.808  19230-19230/? D/TAG﹕ gameState: GameState = [{id:5},{levelId:1}] | gameState1: GameState = [{id:16},{levelId:2}]
05-22 09:52:27.808  19230-19230/? D/TAG﹕ gameState: GameState = [{id:6},{levelId:1}] | gameState1: GameState = [{id:17},{levelId:2}]
05-22 09:52:27.808  19230-19230/? D/TAG﹕ gameState: GameState = [{id:7},{levelId:1}] | gameState1: GameState = [{id:18},{levelId:2}]
05-22 09:52:27.808  19230-19230/? D/TAG﹕ gameState: GameState = [{id:8},{levelId:1}] | gameState1: GameState = [{id:19},{levelId:2}]

I hope this is enough information to find the problem. Nasty one to reproduce, but this particular case went wrong with my game.

@cmelchior
Copy link
Contributor Author

Hi @kevinvanmierlo
Thank you very much for this info. We will take a look at it.

@cmelchior cmelchior reopened this May 22, 2015
@cmelchior cmelchior self-assigned this May 29, 2015
@cmelchior
Copy link
Contributor Author

Hi @kevinvanmierlo
I have tried to reproduce the above error but cannot. I am getting the following output:

05-29 05:48:03.187  22193-22193/io.realm.examples.intro D/TAG﹕ gameState: GameState = [{id:11},{levelId:2}] | gameState1: GameState = [{id:11},{levelId:2}]
05-29 05:48:03.187  22193-22193/io.realm.examples.intro D/TAG﹕ gameState: GameState = [{id:12},{levelId:2}] | gameState1: GameState = [{id:12},{levelId:2}]
05-29 05:48:03.187  22193-22193/io.realm.examples.intro D/TAG﹕ gameState: GameState = [{id:13},{levelId:2}] | gameState1: GameState = [{id:13},{levelId:2}]
05-29 05:48:03.188  22193-22193/io.realm.examples.intro D/TAG﹕ gameState: GameState = [{id:14},{levelId:2}] | gameState1: GameState = [{id:14},{levelId:2}]
05-29 05:48:03.188  22193-22193/io.realm.examples.intro D/TAG﹕ gameState: GameState = [{id:15},{levelId:2}] | gameState1: GameState = [{id:15},{levelId:2}]
05-29 05:48:03.188  22193-22193/io.realm.examples.intro D/TAG﹕ gameState: GameState = [{id:16},{levelId:2}] | gameState1: GameState = [{id:16},{levelId:2}]
05-29 05:48:03.188  22193-22193/io.realm.examples.intro D/TAG﹕ gameState: GameState = [{id:17},{levelId:2}] | gameState1: GameState = [{id:17},{levelId:2}]
05-29 05:48:03.188  22193-22193/io.realm.examples.intro D/TAG﹕ gameState: GameState = [{id:18},{levelId:2}] | gameState1: GameState = [{id:18},{levelId:2}]
05-29 05:48:03.188  22193-22193/io.realm.examples.intro D/TAG﹕ gameState: GameState = [{id:19},{levelId:2}] | gameState1: GameState = [{id:19},{levelId:2}]
05-2

Which should be correct. What version of Realm are you testing against?

@kevinvanmierlo
Copy link

@cmelchior I'm using 0.80.3. I always test on Genymotion with lollipop. The weird thing is when I just tested it on my normal device it worked as expected while on Genymotion it gives back the wrong result.

@cmelchior
Copy link
Contributor Author

That sounds really odd, because I usually run these tests on the same setup. Can you possibly share the full sample project so I am absolutely sure I don't do anything different on my end?

@kevinvanmierlo
Copy link

@cmelchior sure! I'm grabbing some lunch first and then I'll share it with you.

@kevinvanmierlo
Copy link

@cmelchior I pushed the sample project to GitHub. You can find it here.
Also I'm using Genymotion 2.4.0 free version. Hope this helps.

@cmelchior
Copy link
Contributor Author

This is really strange. I cannot reproduce your behaviour even when using the same project on Genymotion 2.4 with a Nexus 5 - 5.1.0 emulator. Do you only see this error on Genymotion but not on your device / Android emulator?

@kevinvanmierlo
Copy link

Actually I'm using the Google Nexus 5 - 5.0.0 - API 21 Genymotion. So perhaps that's the difference. The system has 4 processors and the Base Memory is 2048 MB. I don't see the error on my device (5.0) or emulator(5.0.1). I don't really know why the error only appears on this specific configuration.

Edit: The error also doesn't show on my Genymotion 2.4 with Nexus 5 - 5.1.0 emulator

@cmelchior
Copy link
Contributor Author

Hi @kevinvanmierlo
I tried to reproduce it on the exact same GenyMotion image as you, but it still shows the correct result for me. Have you tried deleting your image and creating a new one?

@kevinvanmierlo
Copy link

@cmelchior After deleting and creating a new one, this also worked. Very weird that it happened though. Thanks for the quick responses!

@cmelchior
Copy link
Contributor Author

Yes, it is a bit strange. It might be a bit late, but if you still has a copy of a Realm file where this error occurred it would be nice to see it. Perhaps we can see what was wrong with it?

@kevinvanmierlo
Copy link

@cmelchior I deleted the emulator, so no way of getting that back. I should've backed it up. I'll see if I can recreate the error somehow.

@cmelchior
Copy link
Contributor Author

Thanks. If you manage to reproduce it please send the file to [email protected]. In the meantime I'll close this issue.

@cmelchior cmelchior removed the Pending label Jun 1, 2015
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants