Pkmmte / PkRSS

A powerful RSS feed manager for Android
Apache License 2.0
100 stars 23 forks source link

request.callback.get() is null #24

Closed ghost closed 8 years ago

ghost commented 8 years ago

I load a RSS and use callback like this

PkRSS.with(this).setLoggingEnabled(true);
PkRSS.with(this).load(url).callback(new Callback() {
            @Override
            public void onPreload() {
                Log.d(TAG, "onPreload");
            }

            @Override
            public void onLoaded(List<Article> list) {
                Log.d(TAG, "onLoaded");
            }

            @Override
            public void onLoadFailed() {
                Log.d(TAG, "onLoadFailed");
            }
        }).async();

And I find onPreload work but onLoaded not call, I find out the reason is invokeCallback return 0, because request.callback.get() get null, but it not null when onPreload notify callback

// Notify callback
handler.onPreload(safe, request.callback.get());

...

// Parse articles from response and inset into global list
List<Article> newArticles = request.parser == null ? parser.parse(response) : request.parser.parse(response);

after parse articles from response, request.callback.get() become null. but I have no idea what happen

ghost commented 8 years ago

WeakReference maybe the key point, after GC it clear from memory, and it turn out to be null.

Pkmmte commented 8 years ago

This is actually a feature because it avoids memory leaks caused by passing your activity/fragment as the callback. I suppose I should have documented this a bit better...

To avoid this issue, you can store that callback in a variable and pass that variable instead.

ghost commented 8 years ago

thank you! I have try that before, but I store request.callback instead. After you remind me to store callback, so I try to store request.callback.get() response and it work now!

request.callback; -> still null request.callback.get(); -> callback work fine

Pkmmte commented 8 years ago

Glad to hear your problem is solved!

Perhaps I'll make an optional parameter in the future for PkRSS to automatically hold strong references. In the meantime, you know what to do. :)