#UiThread pre/post Execute - android-annotations

With android-annotations I can replace AsyncTask<Param, Progress, Result> with #UiThread.
But is possible to handle something like onPreExcecute / onPostExecute of AsynkTask?

There is no callback methods to handle this. But you can have the same result by calling methods at the beginning and at the end of your #Background annotated method (you said #UIThread but I hope this was a mistake :)).
Example :
#Background
void longRunningProcess() {
longRunningProcessStarted();
//...
longRunningProcessEnded();
}
#UiThread
void longRunningProcessStarted() {
}
#UiThread
void longRunningProcessEnded() {
}
We also wrote a cookbook about this.

Related

Elegant way to execute code on function exit in Dart

Suppose we need to execute some code when a function finishes, no matter how.
Example:
void myFunc() async {
await myLock.acquire();
if(...) {
myLock.release();
return;
}
...
myLock.release();
}
Many languages have features that allow to achieve this in a more elegant way than just manually calling myLock.release() before every return statement (For example defer in Go). Is something like that also possible in Dart?
Dart does not have RAII. You instead would need to use try-finally.
(Dart did recently (in 2.17) add Finalizers, but those would fire when objects are garbage collected, which might happen at some non-deterministic time, if ever.)
And just for the record, an example of using try/finally:
void myFunc() async {
await myLock.acquire();
try {
if(...) {
return;
}
...
} finally {
myLock.release();
}
}
You'd want to start the try after allocating the resource, so that you don't try to release if allocation throws.

Anonymous inner Class in Swift

In Java I can do the following:
interface SomeCallback {
void onDone();
}
then I can create a function like this:
void test(SomeCallback callback) {
...
}
To call this function I do:
test(new SomeCallback() {
#Override
void done() {
...
}
});
I want to do something similar in Swift. I could create a protocol
protocol SomeCallback : class {
func done()
}
and a function like this
func test(callback: SomeCallback) {
...
}
I am still struggling with the call of this function.
Edit: Since I use an external API which requires a delegate I cannot use a Closure.
Is it possible to create some kind of anonymous inner class like I did it in the Java example to call test()?
Update: If you can't use a closure/function, the direct answer is: no, there are no anonymous inner classes. However, as Mike M points out, you'll have to use a class, and this class may be nested/inner to prevent polluting the global namespace. This class may well have a closure for every method it needs to implement and just call through to those closures.
The Swift-y way of doing this as long as you just need one method is to just use a lambda/closure.
For example, see NSComparator, which is typealiased since it is used all over the place and you are meant to recognize it.
In your example, specifying a function type inline will do fine. So for example:
func test(callback: () -> Void) {
...
callback()
}
// called as:
test({ in
...
})
// or even (since it's the last parameter)
test { in
...
}
Just to clarify the syntax because what you wrote is a little confusing.
#objc protocol SomeCallback {
func done() -> Void
}
No need to inherit from class as you wrote. Also, don't forget the #objc even if you do not want to bridge the protocol to that language. It helps with compiler complaints later on (might be a bug at the moment)
You cannot instantiate a protocol in Swift. You can however have an internal class that inherits from NSObject (root object) and implements this.
class External: NSObject {
class Internal : SomeCallback {
func done() {
// does something
}
}
let int = Internal()
func test(callback : SomeCallback) {
// additional work
callback.done()
}
}

In Dart, how do I get code completion when using noSuchMethod?

I'd like to use noSuchMethod to reduce boilerplate when writing a lot of similar methods. But then the API is worse for my library's users because code completion no longer works. I tried this but I get warnings about unimplemented methods:
class ThingMixin {
foo();
bar();
noSuchMethod(Invocation inv) {
...
}
}
Is there a workaround?
The trick is to define the API in a separate class:
abstract class ThingApi {
foo();
bar();
}
class ThingMixin implements ThingApi {
noSuchMethod(Invocation inv) {
...
}
}
(Note that noSuchMethod cannot call super if you want it to work as a mixin.)

Razor void function

I have Razor function which outputs some data and as result does not return anything (that's a long story why it is done this way):
#functions
{
public static void SampleHelperMethod()
{
//...
}
}
How can I call it in view now? I tried #MyFunctions.SampleHelperMethod() but it doesn't work for void functions.
Declaration
#functions
{
public static void TestFunction()
{
}
}
Use in View
#{ TestFunction(); }
Because this is a function that does not return anything, you need to wrap it in the braces like you would and if/for statement. However, like Erik said, it is really unclear why this logic would be declared in the view...you may consider creating a helpers class that your views can include. This will allow for reuse and better separations of concerns.

GroovyInterceptable (AOP) and closures

I've got a grails app with Service classes that inherit from Groovy's GroovyInterceptable:
class customerSerrvice implements GroovyInterceptable {
private List<Customer> customers
def invokeMethod(String name, args) {
log.debug "=======>INVOKING method [$name] with args:$args"
}
void foo() {
customers.each { doSomething(it) }
}
void doSomething(Customer cust) { log.debug "doSomething invoked with $cust" }
}
The above is a greatly simplified representation, but it gives you the idea. If I call foo() or doSomething() directly from another class, the invokeMethod gets called like it is supposed to. However, when foo() calls doSomething(), that call is not intercepted in invokeMethod.
If I change from
customers.each { doSomething(it) }
to
for(Customer cust: customers) { doSomething(cust) }
then the invokeMethod gets called just fine.
So is there something about closures and GroovyInterceptable that don't go together? Is there any way to get the invokeMethod to work with closures short of changing them all out?
Thanks
Confirmed as a bug, old link:
http://jira.codehaus.org/browse/GROOVY-4610, new link:
https://issues.apache.org/jira/browse/GROOVY-4610

Resources