jmartinesp / SwissKnife

A multi-purpose library containing view injection and threading for Android using annotations
Apache License 2.0
254 stars 24 forks source link

Errors using GAsyncTask #21

Closed tgirard12 closed 9 years ago

tgirard12 commented 9 years ago

Hi,

I have try to use the GAsyncTask but I have trouble when i use it. First I try the dsl but none of after, before or error method is know (at compile or at runtime) :

    async {
        return "my result"
    } after {
        uiThreadAction()
    }

In secondary place, I tried to use the GAsyncTask directly like this :

    def task = new GAsyncTask<String>({
        return "my result"
    })
    task.before {
        log.error("before")
    }
    task.after {
        log.error("after = $it")
    }
    task.error { ex ->
        log.error("error()", ex)
    }
    task.execute()

The before task is executed and the error give me that stackTrace for the doInBackground task :

groovy.lang.MissingMethodException: No signature of method: MY_FRAGMENT$_getBackgroundNewEvent_background_closure3.doCall() is applicable for  argument types: (null, com.arasthel.swissknife.dsl.components.GAsyncTask) values: [null, com.arasthel.swissknife.dsl.components.GAsyncTask@52d5d5b8]
Possible solutions: doCall(), doCall(java.lang.Object), findAll(), findAll()
        at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:284) ~[na:0.0]
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016) ~[na:0.0]
        at groovy.lang.Closure.call(Closure.java:423) ~[na:0.0]
        at com.arasthel.swissknife.dsl.components.GAsyncTask.doInBackground(GAsyncTask.groovy:56) ~[na:0.0]
        at android.os.AsyncTask$2.call(AsyncTask.java:288) ~[na:0.0]
        at java.util.concurrent.FutureTask.run(FutureTask.java:237) ~[na:0.0]
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) ~[na:0.0]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) ~[na:0.0]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) ~[na:0.0]
        at java.lang.Thread.run(Thread.java:841) ~[na:0.0]

Thank you guys for this library Thomas

jmartinesp commented 9 years ago

Both bugs are due to poor GAsyncTask and async() method documentation, @eugene-kamenev was trying to fix both docs and code to have a better behavior.

eugene-kamenev commented 9 years ago

@Arasthel why it is a bug? :) @tgirard12 Thanks for feedback, I am sorry, yes I forgot to explain the right usage of it. You can try to use it like this, sorry I just copy-pasted it "as is":

final Closure asyncTask = { context, GAsyncTask task ->
        task.error {
            // here we catch the error
            this.progressBar.visible false
            this.refreshLayout.refreshing = false
        }
        task.after {
            // here we catch after event
            if (this.tempApartments) {
                this.offset += 1
                def recyclerAdapter = ((AnimationRecyclerViewAdapter) this.recyclerView.adapter)
                ((ApartmentAdapter) recyclerAdapter.decoratedAdapter).add this.tempApartments
            }
            this.recyclerView.visible true
            this.progressBar.visible false
            this.refreshLayout.refreshing = false
        }
        // and here you can place the actual long running task
        this.tempApartments = DatabaseService.instance.getApartments(this.offset).apartments
    }
...
// then you can call a closure as a param of extension async method
async(asyncTask) 
eugene-kamenev commented 9 years ago

@Arasthel @tgirard12 I updated docs: https://github.com/Arasthel/SwissKnife/blob/master/docs/dsl-methods.adoc#objectasyncclosure-closure

Thanks for feedback.

tgirard12 commented 9 years ago

Thanks for your response. I have tried the @eugene-kamenev right usage but the before Closure is never called, it is null in the onPreExecute function.

The other Closure work fine.

eugene-kamenev commented 9 years ago

@tgirard12 Ok, I see, yeap it will never work, is there a really need for "before" closure? I can delete it

tgirard12 commented 9 years ago

You're right, there is no really need for before. If there is no possible quick fix , delete it could be the best solution.