Selection columns to return in query?

Hi!

Just a quick question.

In swift, is there a way to select which columns you want returned or do you always have to fetch the whole object from the database?

Thanks,
Tobias

Hi Tobias,

Yes, you can fetch only whole object from class table on server (directly or with some search condition):
https://backendless.com/documentation/data/ios/data_basic_search.htm
https://backendless.com/documentation/data/ios/data_search_and_query.htm
https://backendless.com/documentation/data/ios/data_search_with_dates.htm

But if a class has 1:1 or 1:N relations you can determine which of them you would like to load:

 func singleStepRetrieval() {
 
 let dataQuery = BackendlessDataQuery()
 let queryOptions = QueryOptions()
 queryOptions.related = ["items", "items.manufacturer"];
 dataQuery.queryOptions = queryOptions
 
 var error: Fault?
 let bc = backendless.data.of(Order.ofClass()).find(dataQuery, fault: &error)
 if error == nil {
 print("Contacts have been found: \(bc.data)")
 }
 else {
 print("Server reported an error: \(error)")
 }

 }

or which depth relation level you need to retrieve:

 
 func findWithRelationDepth() {
 
 let dataQuery = BackendlessDataQuery()
 let queryOptions = QueryOptions()
 queryOptions.relationsDepth = 2
 dataQuery.queryOptions = queryOptions
 
 var error: Fault?
 let bc = backendless.data.of(PhoneBook.ofClass()).find(dataQuery, fault: &error)
 if error == nil {
 print("Contacts have been found: \(bc.data)")
 }
 else {
 print("Server reported an error: \(error)")
 }
 }

Regards,
Slava

Okay!

Thanks for the fast response! :slight_smile:

Hi Tobias,

Does Slava’s response answer your question? The way I understood it is you wanted to identify specific columns/properties (non-relations) which should be fetched when you retrieve data from the server.

Regards,
Mark

Hi Mark,

Yeah you’re absolutely correct. Say for example if I have a table with 5 columns/properties like firstName, lastName, age, height and weight. On some views I only want to do a query and fetch firstName and lastName for instance.

I interpreted Slava’s answer with “Yes, you can fetch only whole object from class table” that this what not possible unless it was a relational object I wanted to fetch including the whole object in the “main” table.

Thanks,
Tobias

Woops… Made the misstake to make my comment in the main thread and not as a reply to you! See below :slight_smile:

Hi Tobias,

Yes, you can do what you’re asking. Take a look at the docs here:
https://backendless.com/documentation/data/ios/data_search_and_query.htm

Specifically check out the “properties” property in the BackendlessDataQuery object. You can specify what properties should be returned. You will still get back strongly-typed objects in the returned collection, however, only the properties you identify in the query will be populated in the objects you get back:
http://support.backendless.com/public/attachments/d591d6cad3ac4ffbffa69401003946e6.png</img>

Hi Mark,

Is it also possible to select specific columns of a related object that is brought back along with the queried object?

Thanks!