It worked just fine for me and all the records were deleted. We normally do not write client-side code beyond just showing the usage of the APIs. You are welcome to debug the code and see if there is a problem somewhere (but again, I was able to run it and saw that all Restaurant objects were deleted).
I will debug more to it. Thank you Mark.
Key,
here’s an alternative version of the code with CountDownLatch used for thread locking instead of Semaphore. I think it is a bit easier to understand:
public class RemoveAll
{
public static void test() throws Exception
{
Backendless.initApp( APP_ID, SECRET_KEY, "v1" );
new RemoveAll().removeAll();
System.out.println( "Done" );
}
boolean done = false;
CountDownLatch latch;
int objectsToDelete;
AsyncCallback<Long> removeCallback = new AsyncCallback<Long>()
{
@Override
public void handleResponse( Long response )
{
checkAndReleaseMutex();
}
@Override
public void handleFault( BackendlessFault fault )
{
checkAndReleaseMutex();
}
};
AsyncCallback<BackendlessCollection<Restaurant>> findCallback = new AsyncCallback<BackendlessCollection<Restaurant>>()
{
@Override
public void handleResponse( BackendlessCollection<Restaurant> response )
{
try
{
objectsToDelete = response.getCurrentPage().size();
if( objectsToDelete == 0 )
{
done = true;
latch.countDown();
}
for( Restaurant restaurant : response.getCurrentPage() )
Backendless.Persistence.of( Restaurant.class ).remove( restaurant, removeCallback );
}
catch( Exception e )
{
}
}
@Override
public void handleFault( BackendlessFault fault )
{
System.out.println( fault.toString() );
}
};
private void checkAndReleaseMutex()
{
objectsToDelete--;
if( objectsToDelete == 0 )
latch.countDown();
}
public void removeAll() throws Exception
{
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setPageSize( 10 );
while( !done )
{
latch = new CountDownLatch( 1 );
Backendless.Persistence.of( Restaurant.class ).find( dataQuery, findCallback );
latch.await();
if( done )
break;
}
}
}
Mark,
I got the same WAIT behavior for Semaphore and CountDownLatch on my Android and no row removed. I may miss some configuration on my Android Studio to allow Semaphore and CountDownLatch to work.
I appreciate these samples. For now, I have the workaround for it as I mentioned before. I will move on to other features. I’ll definitely get back to this again and will update you.
Best.