Skip to content

Saving Single Object

Your application objects are stored in Backendless tables. Objects can be of custom classes defined in your application or represented as instances of NSDictionary in Objective-C or dictionaries with the type of [String:Any] in Swift. With the custom classes approach, instances of class A are stored in table "A" - Backendless creates tables automatically, however, if you prefer, you can create your tables and define the schema using Backendless console.

Each data table can be accessed using a 'data store' object. A reference to a data store can be obtained using the following code:

// obtaining DataStore for the NSDictionary approach
MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"TABLE-NAME"];

// obtaining DataStore for the custom-class approach
DataStoreFactory *dataStore = [Backendless.shared.data of:[YOUR-CLASS class]];
// obtaining DataStore for the NSDictionary approach
let dataStore = Backendless.shared.data.ofTable("TABLE-NAME")

// obtaining DataStore for the custom-class approach
let dataStore = Backendless.shared.data.of(YOUR-CLASS.self)

where

Argument                Description
YOUR-CLASS identifies a table on the server.
"TABLE-NAME" is the name of the table for which to get the data store. All subsequent data store operations will be performed in the specified table.

Important

All Data Service methods in Backendless must be accessed through data stores.

A data store object provides the following APIs for saving data objects in Backendless:

- (void)saveWithEntity:(id _Nonnull)entity responseHandler:^(id _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func save(entity: Any, responseHandler: ((Any) -> Void)!, errorHandler: ((Fault) -> Void)!)

- (void)saveWithEntity:(NSDictionary<NSString *,id> * _Nonnull)entity responseHandler:^(NSDictionary<NSString *,id> * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func save(entity: [String : Any], responseHandler: (([String : Any]) -> Void)!, errorHandler: ((Fault) -> Void)!)

where:

Argument                Description
entity object to save
responseHandler a block (closure) to handle successful result of an asynchronous call.
errorHandler a block (closure) to handle fault result of an asynchronous call.

Return Value

The API returns the saved object as a result of the request.

Example

Consider the following class which will be used in the example. The class will be used to create data objects persisted in Backendless:

@interface Contact : NSObject

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

@end
@objcMembers class Contact: NSObject {
    var objectId: String?
    var name: String?
    var phone: String?
    var title: String?    
    var age: NSNumber?
}

Important

SWIFT note: The @objcMembers attribute is important. It is required since the Swift object properties are accessed from the Objective-C runtime. For additional information, see the Swift programming guide.

The following code saves an instance of the Contactclass in Backendless:

Contact *contact = [Contact new];
contact.name = @"Dude";
contact.phone = @"555-111-111";
contact.title = @"Coding Chief";
contact.age = @33;             

DataStoreFactory *dataStore = [Backendless.shared.data of:[Contact class]];
[dataStore saveWithEntity:contact responseHandler:^(Contact *savedContact) {
    NSLog(@"Contact has been saved: %@", savedContact);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let contact = Contact()
contact.name = "Dude"
contact.phone = "555-111-111"
contact.title = "Coding Chief"
contact.age = 33

let dataStore = Backendless.shared.data.of(Contact.self)
dataStore.save(entity: contact, responseHandler: { savedContact in
   print("Contact has been saved: \(contact)")
}, errorHandler: { fault in
   print("Error: \(fault.message ?? "")")
})

With the dictionary/map-driven approach, objects sent to the backend are represented as plain NSDictionary (in Objective-C) or [String: Any] Dictionary (in Swift):

NSDictionary *contact = @{@"name": @"Dude", @"phone": @"555-111-111", @"title": @"Coding Chief", @"age": @(33)};

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Contact"];
[dataStore saveWithEntity:contact responseHandler:^(NSDictionary *savedContact) {
    NSLog(@"Contact has been saved: %@", savedContact);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let contact = ["name": "Dude", "phone": "555-111-111", "title": "Coding Chief", "age": 33] as [String : Any]

let dataStore = Backendless.shared.data.ofTable("Contact")
dataStore.save(entity: contact, responseHandler: { savedContact in
    print("Contact has been saved: \(contact)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})