Skip to content

Data Object

Backendless database stores objects in data tables. A persisted object is stored in a language-independent format. For iOS applications, there are two ways to represent data objects on the client side:

Dictionary-based Approach

Data objects can be stored/retrieved as instances of NSDictionary for Objective-C apps or [String : Any]Dictionary for Swift apps. For example, the following code saves an object with properties "name" and "age" in the "Person" table:

// create a data object
NSDictionary *person = @{@"name": @"Joe", @"age": @(25)};

// now save the object in the Backendless database
[[Backendless.shared.data ofTable:@"Person"] saveWithEntity:person responseHandler:^(NSDictionary *savedPerson) {
    NSLog(@"Object has been saved: %@", savedPerson);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
// create a data object
let person = ["name": "Joe", "age": 25] as [String : Any]

// now save the object in the Backendless database
Backendless.shared.data.ofTable("Person").save(entity: person, responseHandler: { savedPerson in
    print("Object has been saved: \(savedPerson)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})


or

Class-based Approach

Data objects can be represented as instances of custom classes which correspond to the data tables. Object's class does not need to implement any special Backendless interfaces or extend a class from the Backendless SDK. For example, the following code saves an object with properties "name" and "age" in the "Person" table:

// Person class declaration
// Person.h
@interface Person : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSNumber *age;

@end

// create a data object
Person *person = [Person new];
person.name = @"Joe";
person.age = @(25);

// now save the object in the Backendless database
[[Backendless.shared.data of:[Person class]] saveWithEntity:person responseHandler:^(Person *savedPerson) {
    NSLog(@"Object has been saved: %@", savedPerson);
} errorHandler:^(Fault *fault) {
    NSLog(@"%@", fault.message);
}];
// Person class declaration
// Person.swift
@objcMembers class Person: NSObject {
    var name: String?
    var age: NSNumber?
}

// create a data object
let person = Person()
person.name = "Joe"
person.age = 25

// now save the object in the Backendless database
Backendless.shared.data.of(Person.self).save(entity: person, responseHandler: { savedPerson in
    print("Object has been saved: \(savedPerson)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

There are a few requirements imposed on the classes of the persisted objects:

  • Classes declared in Swift must have the @objcMembers attribute. For additional details, see Swift programming guide.
  • Class implementation must be kept outside of ViewController or AppDelegate classes. It is recommended to keep an implementation of the data object class in a separate source file.
  • Class must contain the default, publicly accessible no-argument constructor.
  • Class must contain either at least one property.
  • Optional requirement. Backendless automatically assigns a unique ID to every persisted object. If the application needs to have access to the assigned ID, the interface must declare the following property:
@property (strong, nonatomic) NSString *objectId;
var objectId: String?
  • Optional requirement. In addition to objectId, Backendless maintains two additional properties for every persisted object - created and updated. The former contains the timestamp when the object was initially created in the Backendless data store. The latter is updated every time the object is updated. To get access to these properties, the class must implement the following methods:
@property (strong, nonatomic) NSDate *created;
@property (strong, nonatomic) NSDate *updated;
var created: Date?
var updated: Date?
  • For Swift data objects, the Bool, Int, Double and Float types must not be declared optional:
// not applicable to Objective-C
@objcMembers
class SampleDataObject : NSObject {

    var objectId : String?
    var counter: Int = 0
    var isPublished: Bool = true
}