Skip to content

Updating Single Object

Updating an object in a data table must be done with 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 updating data objects in Backendless:

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

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

where:

Argument                Description
entity object to update in the database.
responseBlock a block (closure) to handle successful result of an asynchronous call.
errorBlock a block (closure) to handle fault result of an asynchronous call.

Return Value

The API returns the updated 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 4 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 4 programming guide.

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

Contact *existingContact = // some existing contact
existingContact.age = @34;

DataStoreFactory *dataStore = [Backendless.shared.data of:[Contact class]];
[dataStore updateWithEntity:existingContact responseHandler:^(Contact *updatedObject) {
    NSLog(@"Object has been updated: %@", updatedObject);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let existingContact: Contact = // some existing contact
existingContact.age = 34

let dataStore = Backendless.shared.data.of(Contact.self)
dataStore.update(entity: existingContact, responseHandler: { updatedObject in
    print("Object has been updated: \(updatedObject)")
}, 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):

NSMutableDictionary *existingContact = // some existing contact
existingContact[@"age"] = @34;

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Contact"];
[dataStore updateWithEntity:existingContact responseHandler:^(NSDictionary *updatedObject) {
    NSLog(@"Object has been updated: %@", updatedObject);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
var existingContact: [String: Any] = // some existing contact
existingContact["age"] = 34

let dataStore = Backendless.shared.data.ofTable("Contact")
dataStore.update(entity: existingContact, responseHandler: { updatedObject in
    print("Object has been updated: \(updatedObject)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})