Blog

How to load the last object from a data table using API

by on April 11, 2015

In another article, we wrote how to load the first object from a data table using API. For the purpose of symmetry (and out of common sense), there is also an API to load the last object from a data table in Backendless. The last object is determined by the time when it is saved in the data storage. The most recently created (notice it is not the updated one) object is what the API returns.

The sample code below works with the same table as the example for finding first object.

    Backendless.Data.of( Person.class ).findLast( new AsyncCallback<Person>()
    {
        @Override
        public void handleResponse( Person firstPerson )
        {
            System.out.println( "Name - " + firstPerson.name );
            System.out.println( "Age - " + firstPerson.age );
            System.out.println( "Birthdate - " + firstPerson.birthdate );
        }
        @Override
        public void handleFault( BackendlessFault backendlessFault )
        {
            System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
        }
    } );

    Synchronous API sample (Plain Java only):

    Person firstPerson = Backendless.Data.of( Person.class ).findLast();
    System.out.println( "Name - " + firstPerson.name );
    System.out.println( "Age - " + firstPerson.age );
    System.out.println( "Birthdate - " + firstPerson.birthdate );

    [[Backendless.shared.data of:[Person class]] findLastWithResponseHandler:^(Person *lastPerson) {
        NSLog(@"Name - %@", lastPerson.name);
        NSLog(@"Age - %li", (long)lastPerson.age);
        NSLog(@"Birthdate - %@", lastPerson.birthdate);
    } errorHandler:^(Fault *fault) {
        NSLog(@"Error: %@", fault.message);
    }];

    Backendless.shared.data.of(Person.self).findLast(responseHandler: { lastPerson in
        if let lastPerson = lastPerson as? Person {
            print("Name - \(lastPerson.name ?? "")")
            print("Age - \(lastPerson.age)")
            print("Birthdate - \(lastPerson.birthdate)")
        }
    }, errorHandler: { fault in
        print("Error: \(fault.message ?? "")")
    })

    const Backendless = require('backendless')
    /*
     Or use `import Backendless from 'backendless'` for the client side.
     If you don't use npm or yarn to install modules, you can add the following line
     <script src="//api.backendless.com/sdk/js/latest/backendless.min.js"></script>
     to your index.html file and use the global Backendless variable.
    */
    
    Backendless.initApp('YOUR_APP_ID', 'YOUR_JS_API_KEY')
    
    const onSuccess = firstPerson => {
      console.log(`Name - ${ firstPerson.name }`)
      console.log(`Age - ${ firstPerson.age }`)
      console.log(`Birthdate - ${ firstPerson.birthdate }`)
    }
    
    const onError = error => {
      console.error('Server reported an error: ', error.message)
      console.error('error code: ', error.code)
      console.error('http status: ', error.status)
    }
    
    Backendless.Data.of('Person').findLast()
      .then(onSuccess)
      .catch(onError)