emilsjolander / sprinkles

Sprinkles is a boiler-plate-reduction-library for dealing with databases in android applications
Apache License 2.0
772 stars 84 forks source link

Returning false in the callback for getAsync() still calls the callback #19

Closed praneetloke closed 10 years ago

praneetloke commented 10 years ago

OK. I'll try my best to describe this correctly. Right now, I am not sure if I am doing something wrong or if there is a bug in the async callback.

I have a scenario where I check for the existence of a certain item in the DB before I save it and hence I have a Query.One().getAsync() call. The callback given to the getAsync() part is returning false and yet it gets called again when I actually do save the item if it doesn't exist in the database. Moreover, since I save to the DB using saveAsync(), Sprinkles seems to call the callback anyway even when the record has not yet been saved and, thereby resulting in two records in the DB for a single save.

I guess a simple code sample to illustrate would be clearer:

private OneQuery.ResultHandler<Example> onExampleLoaded = new OneQuery.ResultHandler<Example>() {
        @Override
        public boolean handleResult (Example example) {
            if (example == null) {
                String something = "hello";

                Example newExample = new Example(something);
                newExample.saveAsync(new Model.OnSavedCallback() {
                    @Override
                    public void onSaved () {
                        Log.i(TAG, "Saved"); //this executes twice
                    }
                });
            }
            //don't want to be notified of further changes
            return false;
        }
    };
emilsjolander commented 10 years ago

will look into it!

praneetloke commented 10 years ago

Thanks, dude!

emilsjolander commented 10 years ago

I can't replicate this bug. And after looking through the code a couple of times i cannot understand why this is happening. So some questions.

praneetloke commented 10 years ago

Android version is 4.2.2. I am using the support library (and, ABS as well) loaders. I am passing in the loader manager instance from getSupportLoaderManager().

Query.one(Example.class, "select * from Example where something=?", something).getAsync(getSupportLoaderManager(), onExampleLoaded);
emilsjolander commented 10 years ago

I managed to replicate and fix a similar issue using the support loaders. Please comment here if your issue is still present after testing the new version (should be hitting maven central within the next couple hours)

praneetloke commented 10 years ago

wow!Thanks, man! I'll check it out and get back to you.