Thursday, April 28, 2016

A Peculiar Problem With Retrofit and Android-Priority-Jobqueue

I am currently developing a new(ish) app which uses several libraries. I did some research on loads of networking and job queue libraries and decided to use Retrofit from Square for networking and Android Priority Jobqueue from Path (Now Yigit).

Everything was going along very well. I was executing API calls inside Jobs using Retrofit...but one day data stopped getting to our servers.

I ran the application again, and it would execute several jobs successfully, so I thought perhaps it was a once off problem, but I was unfortunately wrong.

It happened again on my phone. So I tried a few other devices and they all exhibited similar outcomes.

I tried everything with the job queue. I removed all groups, and priorities and all persistence. I updated to later versions (I wasn't too keen on going to v2 as it is not released as of yet.) I added a logger and explicitly told the JobManager to start but it still stopped after a few jobs. I did some research on the load factor, thinking it might be something to do with that.
It wasn't.
After a few days of debugging, I found a piece of code, written in haste, that I believed to be causing the issue.


public class GenericResponseAdapter extends TypeAdapter<GenericResponse> {

    @Override
    public void write(JsonWriter jsonWriter, GenericResponse genericResponse) throws IOException {

    }

    @Override
    public GenericResponse read(JsonReader jsonReader) throws IOException {



        while(jsonReader.hasNext()){
            jsonReader.skipValue();
        }

        return new GenericResponse();
    }
}

I had created a response for those API calls that weren't anything in particular and had hoped that this would suffice that need. (I was coming very close to the 65k method limit imposed by dex and so I was trying to save methods and effort.)

The problem seems to be that an infinite loop in the
read()
method would, obviously, prevent the Generic Response from being returned.
Thus the thread that the job was being executed on never ended and the next job was never executed.

And so everything stopped uploading.

I know this may seem obvious to some but it took me almost two days to realise this is what was happening and to fix it. The fix will depend on your implementation.

I like to get my stupid errors out there...cause then I don't make them again.

Hope this helps someone!