Skip to content

Retrieving objects

This operation retrieves data objects from a database table. The retrieval is performed according to the provided options, which allow you to:

  • Retrieve objects with a query
  • Using paging
  • Using sorting
  • With related objects
-(OpResult *)[unitOfWorkInstance findWithTableName:(NSString * _Nonnull) queryBuilder:(DataQueryBuilder * _Nullable)];
unitOfWorkInstance.find(tableName: String, queryBuilder: DataQueryBuilder?) -> OpResult

The tableName argument specified the table where the objects are retrieved from. The queryBuilder argument contains the data retrieval options. For more information about available options see the Advanced Object Retrieval API section of this guide.

Important

You can retrieve a single object by setting the pageSize option to 1. However, remember the method still returns a collection of objects, therefore if you need to get only one object, you need to use the OpResult API to resolve it to a OpResultValueReference.

Return Value

The operation returns an OpResult object which represents the result of this operation - a collection of retrieved objects. The OpResult object can be used as an argument in other operations in the same transaction (same UnitOfWork instance). There are two ways to reference a result from a previous operation - using the OpResult class or using the OpResultValueReference. You can learn about these classes in detail from the Operation Result chapter of this guide, but in short the OpResult objects represent the entire result of a transaction operation, while the OpResultValueReference objects represent a specific object.

Example

The example below demonstrates retrieval of an object and updating its property in a transaction:

UnitOfWork *unitOfWork = [UnitOfWork new];

// Create a query builder
DataQueryBuilder *queryBuilder = [DataQueryBuilder new];

// set the where clause
queryBuilder.whereClause = @"name = 'India'";

// we're asking the server to return only one object
queryBuilder.pageSize = 1;

// run the query in the transaction
OpResult *findCountryResult = [unitOfWork findWithTableName:@"Country" queryBuilder:queryBuilder];

// The entire findCountryResult is a collection, so we need to
// get the first object from the response of the previous operation
OpResultValueReference *india = [findCountryResult resolveToResultIndex:@0];

// update the object and set a value for the "population" property
[unitOfWork updateWithValueReference:india propertyName:@"population" propertyValue:@1339000000];

// now run the transaction
[unitOfWork executeWithResponseHandler:^(UnitOfWorkResult *response) {
    // transaction is complete. Use uowResult to check the result
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let unitOfWork = UnitOfWork()

// Create a query builder
let queryBuilder = DataQueryBuilder()

// set the where clause
queryBuilder.whereClause = "name = 'India'"

// we're asking the server to return only one object
queryBuilder.pageSize = 1

// run the query in the transaction
let findCountryResult = unitOfWork.find(tableName: "Country", queryBuilder: queryBuilder)

// The entire findCountryResult is a collection, so we need to
// get the first object from the response of the previous operation
let india = findCountryResult.resolveTo(resultIndex: 0)

// update the object and set a value for the "population" property
let _ = unitOfWork.update(valueReference: india, propertyName: "population", propertyValue: 1339000000)

// now run the transaction
unitOfWork.execute(responseHandler: { uowResult in
    // transaction is complete. Use uowResult to check the result
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})