Search code examples
javaandroidmultithreadingcallbackfacebook-android-sdk

Multithreading in Android - why doesn't my onCompleted callback get called?


So I'm trying to add multithreading to my app and I'm running into a weird issue. I create a new runnable to handle parsing all the data so that the application doesn't freeze while it's doing so, and then create a new thread to run that runnable. However, when the thread starts and run() gets called, I noticed it never calls onCompleted(). This is weird to me because if I take that exact block of code out of the run() part, it works perfectly.

Can anybody let me know what I'm doing wrong? I'm a bit new to multithreading in Java, so I would really appreciate it.

    Runnable parseRunnable = new Runnable(){
        @Override
        public void run(){
            new Request(session, id + "/comments", null, HttpMethod.GET, new Request.Callback() {
                @Override
                public void onCompleted(Response response) {
                    try {
                        JSONArray msgs = response.getGraphObject().getInnerJSONObject().getJSONArray("data");
                        populateData(msgs);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Request next = response.getRequestForPagedResults(Response.PagingDirection.NEXT);
                    retrieveMsgs(next);
                }
            }).executeAsync();
        }
    };

    Thread parseThread = new Thread(parseRunnable);
    parseThread.start();

I set a breakpoint at run() and at "new Request" which always gets hit, but when I set breakpoints for onCompleted(), those are never reached.


Solution

  • Your thread will finish when you reach the end of

    public void run ()
    

    I'm going to assume you don't get your callback because your thread has already finished since you call executeAsync (which will execute on a separate thread). Removing your thread altogether will probably fix your problem.