Blog

How to create custom mapping between data tables and client classes

by on May 12, 2015

In another post, we described how data tables in Backendless map to the client-side classes whose instances contain persisted data objects. However, there are scenarios when the default mapping is undesirable. In that case, Backendless client libraries provide an API to override the mapping. For example, consider the following data table (Restaurant):

Suppose that for some reason the client-side class for the objects in the Restaurant table must be called PlaceToEat. The mapping between the Restaurant table and the PlaceToEat class can be established using the following API:

Backendless.Data.mapTableToClass( "Restaurant", PlaceToEat.class );

Where the PlaceToEat class has the following structure (class’s fields and properties must match the table columns):

    import com.backendless.BackendlessUser;
    public class PlaceToEat
    {
      private String name;
      private String objectId;
      private String cuisine;
      private java.util.List<Location> locations;
      private BackendlessUser owner;
      // skipping getters and setters for brevity
    }

    //
    //  PlaceToEat.m
    //  F123CustomClassToTableMapping
    #import "PlaceToEat.h"
    @implementation PlaceToEat
    @end

    import Foundation
    class PlaceToEat : NSObject {
        var name : String?
        var objectId : String?
        var cuisine : String?
        var locations : [Location]?
        var owner : BackendlessUser?
    }

    It is recommended that mapTableToClass method is called right after the initApp method, that is before any other API which fetches data from the server.