Skip to content

Basic Object Retrieval

Backendless supports multiple data search and retrieval operations. These include finding an object by its objectId, finding first or last object in the collection or retrieving the entire persisted collection.

Retrieving Data Objects

Retrieving object count for a table/query can 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("YOUR-TABLE")

// 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" 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 basic data retrieval from Backendless:

Basic retrieval of data objects

Paging is built-in and the server returns up to 100 objects at a time.

- (void)findWithResponseHandler:^(NSArray * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func find(responseHandler: (([Any]) -> Void)!, errorHandler: ((Fault) -> Void)!)

Find first data object

The first data object is the first one saved in the data store:

- (void)findFirstWithResponseHandler:^(id _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func findFirst(responseHandler: ((Any) -> Void)!, errorHandler: ((Fault) -> Void)!)

Find last data object

The last data object is the last one saved in the data store:

- (void)findLastWithResponseHandler:^(id _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func findLast(responseHandler: ((Any) -> Void)!, errorHandler: ((Fault) -> Void)!)

Find a data object by object  ID

- (void)findByIdWithObjectId:(NSString * _Nonnull)objectId responseHandler:^(id _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func findById(objectId: String, responseHandler: ((Any) -> Void)!, errorHandler: ((Fault) -> Void)!)

Basic retrieval of data objects

Paging is built-in and the server returns up to 100 objects at a time.

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

Find first data object

The first data object is the first one saved in the data store:

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

Find last data object

The last data object is the last one saved in the data store:

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

Find a data object by object  ID

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

where:

Argument                Description
objectId object ID of the object to find. For the details about objectId, see the Data Object section of the documentation.
responseHandler handles successful result of an asynchronous call.
errorHandler handles fault result of an asynchronous call.

Return Value

Most methods return a single object (findFirst, findList, findById), the find method returns an array of objects. The array contains objects of the specified class with the "Custom class approach" or dictionaries/maps with the "Dictionary approach".

Example

Consider the following class:

@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

The following code demonstrates various search queries:

Example Load objects from the "Contact" table:

DataStoreFactory *dataStore = [Backendless.shared.data of:[Contact class]];
[dataStore findWithResponseHandler:^(NSArray *foundObjects) {
    NSLog(@"Found objects: %@", foundObjects);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];

Example Find the first object in the "Contact" table:

DataStoreFactory *dataStore = [Backendless.shared.data of:[Contact class]];
[dataStore findFirstWithResponseHandler:^(Contact *first) {
    NSLog(@"Found first: %@", first);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];

Example Find the last object in the "Contact" table:

DataStoreFactory *dataStore = [Backendless.shared.data of:[Contact class]];
[dataStore findLastWithResponseHandler:^(Contact *last) {
    NSLog(@"Found last: %@", last);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];

Example Find object by object ID:

DataStoreFactory *dataStore = [Backendless.shared.data of:[Contact class]];
[dataStore findByIdWithObjectId:@"862D8AB3-02F1-4932-FF70-527C3638B200" responseHandler:^(Contact *foundObject) {
    NSLog(@"Found by id: %@", foundObject);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];

Consider the following class:

@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 demonstrates various search queries:

Example Load objects from the "Contact" table:

let dataStore = Backendless.shared.data.of(Contact.self)
dataStore.find(responseHandler: { foundObjects in
    print("Found objects: \(foundObjects as! [Contact])")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Example Find the first object in the "Contact" table:

let dataStore = Backendless.shared.data.of(Contact.self)
dataStore.findFirst(responseHandler: { first in
    print("Found first: \(first as! Contact)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Example Find the last object in the "Contact" table:

let dataStore = Backendless.shared.data.of(Contact.self)
dataStore.findLast(responseHandler: { last in
    print("Found last: \(last as! Contact)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Example Find object by object ID:

let dataStore = Backendless.shared.data.of(Contact.self)
dataStore.findById(objectId: "862D8AB3-02F1-4932-FF70-527C3638B200", responseHandler: { foundObject in
    print("Found object: \(foundObject as! Contact)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Dictionary-driven approach does not require a class definition for objects stored in Backendless. The following code demonstrates various search queries:

Example Load objects from the "Contact" table:

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Contact"];
[dataStore findWithResponseHandler:^(NSArray *foundObjects) {
    NSLog(@"Found objects: %@", foundObjects);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];

Example Find the first object in the "Contact" table:

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Contact"];
[dataStore findFirstWithResponseHandler:^(NSDictionary *first) {
    NSLog(@"Found first: %@", first);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];

Example Find the last object in the "Contact" table:

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Contact"];
[dataStore findLastWithResponseHandler:^(NSDictionary *last) {
    NSLog(@"Found last: %@", last);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];

Example Find object by object ID:

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Contact"];
[dataStore findByIdWithObjectId:@"862D8AB3-02F1-4932-FF70-527C3638B200" responseHandler:^(NSDictionary *foundObject) {
    NSLog(@"Found by id: %@", foundObject);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];

Dictionary-driven approach does not require a class definition for objects stored in Backendless. The following code demonstrates various search queries:

Example Load objects from the "Contact" table:

let dataStore = Backendless.shared.data.ofTable("Contact")
dataStore.find(responseHandler: { foundObjects in
    print("Found objects: \(foundObjects as! [[String: Any]])")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Example Find the first object in the "Contact" table:

let dataStore = Backendless.shared.data.ofTable("Contact")
dataStore.findFirst(responseHandler: { first in
    print("Found first: \(first as! [String: Any])")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Example Find the last object in the "Contact" table:

let dataStore = Backendless.shared.data.ofTable("Contact")
dataStore.findLast(responseHandler: { last in
    print("Found last: \(last as! [String: Any])")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Example Find object by object ID:

let dataStore = Backendless.shared.data.ofTable("Contact")
dataStore.findById(objectId: "862D8AB3-02F1-4932-FF70-527C3638B200", responseHandler: { foundObject in
    print("Found object: \(foundObject as! [String: Any])")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})