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.

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](https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html#//apple_ref/doc/uid/TP40014216-CH5-ID86)        .

The following code saves an instance of the Contact class 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 ?? "")")
})

Codeless Reference

data_service_saving_object

where:

Argument                Description
table name Name of the data table where a new record must be saved.
object An object to save in the database. Object properties must match the names of the table columns. The object must not have the objectId property.
return result Optional parameter. When this box is checked, the operation returns the saved object with the objectId property.

Returns the saved object with the objectId property assigned by Backendless

Consider the following structure of the data table called employees:
data_service_example_saving_object_1

For demonstration purposes, the data table presented above has three custom columns: name, position, and phoneNumber. The objectId is a system column that contains unique identifiers of the records in the table. When a new record is saved to the table, the system assigns a unique objectId to it. The objectid is used in various operations as an access point to a specific record.

The example below saves a new object to the employees data table:

data_service_example_saving_object_2

The result of this operation will look as shown below after the Codeless logic runs:

data_service_example_saving_object_3

The operation described above has returned the newly saved object:

data_service_example_saving_object_4