Setting listener on a Glide image load only captures local variable once












0















I'm using Glide to load an image and I'm setting a new listener on it each time I call it. I expect the anonymous "new RequestListener" to capture the local variable "x" each time, but it only appears to capture it the first time. It's almost like Glide is ignoring my request to set a new listener and reusing the original listener. Here is the code to reproduce it:



public void loadImage(int x) {
GlideApp.with(getContext()).load("http://url.to/image.png").listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { return false; }

@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
Log.d(TAG, "Value of x: " + x);
return false;
}
}).into(imageView);
}


Calling loadImage(0) results in:

Value of x: 0



Subsequent calls to loadImage, no matter what value of x I pass in, will always result in:

Value of x: 0



Does anybody know what's going on here?










share|improve this question























  • is GlideApp part of Glide library or is it a wrapper of yours?

    – lelloman
    Feb 9 '18 at 20:38











  • GlideApp is a module / class that gets generated by Glide.

    – dazza5000
    Feb 9 '18 at 20:43











  • I see, so from the code it seems that GlideApp is not caching the RequestBuilder but it's using the new instance every time, so, are you 100% sure that you're calling the method with values different than 0? did you check with the debugger?

    – lelloman
    Feb 9 '18 at 20:50











  • I tried to made a sample project, from which what I could figure is, when you call into(imageView); only the last call is accepted. (I called the function in a for loop. Instead if you call submit, it works fine and every time listener is called.

    – karandeep singh
    Feb 9 '18 at 20:58






  • 1





    @MahmoudAli, It looks like Glide does some magic where it caches and reuses the original listener. I worked around this by having the listener rely on a class instance variable rather than a local variable. That way I can change the value of the variable and the listener will be able to see that change.

    – SilentByte
    Nov 11 '18 at 16:03
















0















I'm using Glide to load an image and I'm setting a new listener on it each time I call it. I expect the anonymous "new RequestListener" to capture the local variable "x" each time, but it only appears to capture it the first time. It's almost like Glide is ignoring my request to set a new listener and reusing the original listener. Here is the code to reproduce it:



public void loadImage(int x) {
GlideApp.with(getContext()).load("http://url.to/image.png").listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { return false; }

@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
Log.d(TAG, "Value of x: " + x);
return false;
}
}).into(imageView);
}


Calling loadImage(0) results in:

Value of x: 0



Subsequent calls to loadImage, no matter what value of x I pass in, will always result in:

Value of x: 0



Does anybody know what's going on here?










share|improve this question























  • is GlideApp part of Glide library or is it a wrapper of yours?

    – lelloman
    Feb 9 '18 at 20:38











  • GlideApp is a module / class that gets generated by Glide.

    – dazza5000
    Feb 9 '18 at 20:43











  • I see, so from the code it seems that GlideApp is not caching the RequestBuilder but it's using the new instance every time, so, are you 100% sure that you're calling the method with values different than 0? did you check with the debugger?

    – lelloman
    Feb 9 '18 at 20:50











  • I tried to made a sample project, from which what I could figure is, when you call into(imageView); only the last call is accepted. (I called the function in a for loop. Instead if you call submit, it works fine and every time listener is called.

    – karandeep singh
    Feb 9 '18 at 20:58






  • 1





    @MahmoudAli, It looks like Glide does some magic where it caches and reuses the original listener. I worked around this by having the listener rely on a class instance variable rather than a local variable. That way I can change the value of the variable and the listener will be able to see that change.

    – SilentByte
    Nov 11 '18 at 16:03














0












0








0








I'm using Glide to load an image and I'm setting a new listener on it each time I call it. I expect the anonymous "new RequestListener" to capture the local variable "x" each time, but it only appears to capture it the first time. It's almost like Glide is ignoring my request to set a new listener and reusing the original listener. Here is the code to reproduce it:



public void loadImage(int x) {
GlideApp.with(getContext()).load("http://url.to/image.png").listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { return false; }

@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
Log.d(TAG, "Value of x: " + x);
return false;
}
}).into(imageView);
}


Calling loadImage(0) results in:

Value of x: 0



Subsequent calls to loadImage, no matter what value of x I pass in, will always result in:

Value of x: 0



Does anybody know what's going on here?










share|improve this question














I'm using Glide to load an image and I'm setting a new listener on it each time I call it. I expect the anonymous "new RequestListener" to capture the local variable "x" each time, but it only appears to capture it the first time. It's almost like Glide is ignoring my request to set a new listener and reusing the original listener. Here is the code to reproduce it:



public void loadImage(int x) {
GlideApp.with(getContext()).load("http://url.to/image.png").listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { return false; }

@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
Log.d(TAG, "Value of x: " + x);
return false;
}
}).into(imageView);
}


Calling loadImage(0) results in:

Value of x: 0



Subsequent calls to loadImage, no matter what value of x I pass in, will always result in:

Value of x: 0



Does anybody know what's going on here?







java android android-glide glide-image-library






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 9 '18 at 20:30









SilentByteSilentByte

362212




362212













  • is GlideApp part of Glide library or is it a wrapper of yours?

    – lelloman
    Feb 9 '18 at 20:38











  • GlideApp is a module / class that gets generated by Glide.

    – dazza5000
    Feb 9 '18 at 20:43











  • I see, so from the code it seems that GlideApp is not caching the RequestBuilder but it's using the new instance every time, so, are you 100% sure that you're calling the method with values different than 0? did you check with the debugger?

    – lelloman
    Feb 9 '18 at 20:50











  • I tried to made a sample project, from which what I could figure is, when you call into(imageView); only the last call is accepted. (I called the function in a for loop. Instead if you call submit, it works fine and every time listener is called.

    – karandeep singh
    Feb 9 '18 at 20:58






  • 1





    @MahmoudAli, It looks like Glide does some magic where it caches and reuses the original listener. I worked around this by having the listener rely on a class instance variable rather than a local variable. That way I can change the value of the variable and the listener will be able to see that change.

    – SilentByte
    Nov 11 '18 at 16:03



















  • is GlideApp part of Glide library or is it a wrapper of yours?

    – lelloman
    Feb 9 '18 at 20:38











  • GlideApp is a module / class that gets generated by Glide.

    – dazza5000
    Feb 9 '18 at 20:43











  • I see, so from the code it seems that GlideApp is not caching the RequestBuilder but it's using the new instance every time, so, are you 100% sure that you're calling the method with values different than 0? did you check with the debugger?

    – lelloman
    Feb 9 '18 at 20:50











  • I tried to made a sample project, from which what I could figure is, when you call into(imageView); only the last call is accepted. (I called the function in a for loop. Instead if you call submit, it works fine and every time listener is called.

    – karandeep singh
    Feb 9 '18 at 20:58






  • 1





    @MahmoudAli, It looks like Glide does some magic where it caches and reuses the original listener. I worked around this by having the listener rely on a class instance variable rather than a local variable. That way I can change the value of the variable and the listener will be able to see that change.

    – SilentByte
    Nov 11 '18 at 16:03

















is GlideApp part of Glide library or is it a wrapper of yours?

– lelloman
Feb 9 '18 at 20:38





is GlideApp part of Glide library or is it a wrapper of yours?

– lelloman
Feb 9 '18 at 20:38













GlideApp is a module / class that gets generated by Glide.

– dazza5000
Feb 9 '18 at 20:43





GlideApp is a module / class that gets generated by Glide.

– dazza5000
Feb 9 '18 at 20:43













I see, so from the code it seems that GlideApp is not caching the RequestBuilder but it's using the new instance every time, so, are you 100% sure that you're calling the method with values different than 0? did you check with the debugger?

– lelloman
Feb 9 '18 at 20:50





I see, so from the code it seems that GlideApp is not caching the RequestBuilder but it's using the new instance every time, so, are you 100% sure that you're calling the method with values different than 0? did you check with the debugger?

– lelloman
Feb 9 '18 at 20:50













I tried to made a sample project, from which what I could figure is, when you call into(imageView); only the last call is accepted. (I called the function in a for loop. Instead if you call submit, it works fine and every time listener is called.

– karandeep singh
Feb 9 '18 at 20:58





I tried to made a sample project, from which what I could figure is, when you call into(imageView); only the last call is accepted. (I called the function in a for loop. Instead if you call submit, it works fine and every time listener is called.

– karandeep singh
Feb 9 '18 at 20:58




1




1





@MahmoudAli, It looks like Glide does some magic where it caches and reuses the original listener. I worked around this by having the listener rely on a class instance variable rather than a local variable. That way I can change the value of the variable and the listener will be able to see that change.

– SilentByte
Nov 11 '18 at 16:03





@MahmoudAli, It looks like Glide does some magic where it caches and reuses the original listener. I worked around this by having the listener rely on a class instance variable rather than a local variable. That way I can change the value of the variable and the listener will be able to see that change.

– SilentByte
Nov 11 '18 at 16:03












1 Answer
1






active

oldest

votes


















0














I had this exact problem today. glide kept using the old listener. It some how caused the same setOnClickListener to keep being used, so basically all my buttons would display the same view. It was fixed once I added:



.apply(RequestOptions.skipMemoryCacheOf(true))






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48713655%2fsetting-listener-on-a-glide-image-load-only-captures-local-variable-once%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I had this exact problem today. glide kept using the old listener. It some how caused the same setOnClickListener to keep being used, so basically all my buttons would display the same view. It was fixed once I added:



    .apply(RequestOptions.skipMemoryCacheOf(true))






    share|improve this answer






























      0














      I had this exact problem today. glide kept using the old listener. It some how caused the same setOnClickListener to keep being used, so basically all my buttons would display the same view. It was fixed once I added:



      .apply(RequestOptions.skipMemoryCacheOf(true))






      share|improve this answer




























        0












        0








        0







        I had this exact problem today. glide kept using the old listener. It some how caused the same setOnClickListener to keep being used, so basically all my buttons would display the same view. It was fixed once I added:



        .apply(RequestOptions.skipMemoryCacheOf(true))






        share|improve this answer















        I had this exact problem today. glide kept using the old listener. It some how caused the same setOnClickListener to keep being used, so basically all my buttons would display the same view. It was fixed once I added:



        .apply(RequestOptions.skipMemoryCacheOf(true))







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 10:15

























        answered Nov 22 '18 at 1:17









        David C.David C.

        11




        11
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48713655%2fsetting-listener-on-a-glide-image-load-only-captures-local-variable-once%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith