ServerSide Code related Question

so i added event handler for updating of my table but why return value is not as desired ??

@Async
@Override
public void afterUpdate( RunnerContext context, PEvents pevents, ExecutionResult<PEvents> result ) throws Exception
{
// add your code here

System.out.println("afterUpdate  :"+pevents.getUpvotes().size() +" result : " +result ); // pevents.getUpvotes().size() always returns 1 

}

@Async
@Override
public void beforeUpdate( RunnerContext context, PEvents pevents ) throws Exception
{
// add your code here
System.out.println(“beforeUpdate :”+pevents.getUpvotes().size()); // pevents.getUpvotes().size() always returns 1
}

}
value of pevents.getUpvotes().size() suppose to increase but it always returns 1 , i checked the value on server and there its correct

after selecting “AUTOLOAD” from row now its giving the correct size , still i have a problem here that objects returns same values for “pevents.getUpvotes().size()” even after and before update

like before update the value of "pevents.getUpvotes().size() " is 1 but after update it changes to 2 but both callbacks (before , after) are returning “2”

In the beforeUpdate handler you have the same object you’re sending, thus you already have 2 objects in it, but at this moment database still has one.

just created a new object with (0 object) and tried to update it here’s the console result :

System.out.println(“old :”+oldPevent.getUpvotes().size()+" NEW : "+pevents.getUpvotes().size()); // returned old :1 NEW : 1

old suppose to return 0 here

Please show how you load oldPevent

@Async
@Override
public void beforeUpdate( RunnerContext context, PEvents pevents ) throws Exception
{PEvents oldPevent = Backendless.Data.of( PEvents.class ).findById( pevents.getObjectId(), 1 );…
}

What if you remove the @Async annotation?

it was part of boilerplate code but removing it does the job now i’m getting desired result , thanks :slight_smile:

Glad it helped!
Anyway, thank you for reporting. The @Async annotation on this type of handlers seems strange, I think we’ll reconsider it’s behaviour in the future.

alright , thanks backendless guys for always being there (Y)

Just discussed it’s behaviour with a team: having an @Async annotation on a before handler guarantees only that it will be invoked before the actual update starts; but since it’s asynchronous there are no guarantees that the code inside the handler will be executed before the actual update.

For example, it may be useful to send some notification or invoke some action when an object is being updated. But if you need to rely on the old object’s state inside the handler’s code, then you shouldn’t mark the handler as @Async - which is exactly your case.

okay now it clear , i have a little question , we can update any user’s property from serveSide code right ?

Yes, of course.

alright :slight_smile: