Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Backendless sorting relationship objects

I have this Backendless tables: Posts and Comments

Posts has a column comments which is has a one-to-many relationship to Comments table.

I'm try to get the comments in a latestOrder first behavior.

Currently, I have this query:

  let query = BackendlessDataQuery()
  let queryOptions = QueryOptions()
  queryOptions.pageSize = size
  queryOptions.related = ["comments", "comments.user", "user", "media"]
  query.queryOptions = queryOptions
  // Used PostObject since it is already mapped using `mapTableToClass`
  backendlessInstance.persistenceService.of(PostObject.ofClass()).find(
    query,
    response: { backendlessPostsList in
      let backendlessPostsListOfOffset = backendlessPostsList.getPage(offset)
      guard let postObjects = backendlessPostsListOfOffset.getCurrentPage() as? [PostObject] else {
        reject(BackendlessError.InvalidTypeForObject(name: "Post"))
        return
      }
      return postObjects
    },
    error: { fault in
      // TODO Find a way to convert a Fault to ErrorType
      print("Server reported an error (1): \(fault)")
    }
  )

What I'm doing currently to sort Post.comments in the view model is reversing it. Post.comments.reverse().

Is there a way to explicitly sort the comments in Backendless level?

like image 348
jhnferraris Avatar asked Feb 21 '26 00:02

jhnferraris


1 Answers

You don't have options when using related, and you should really just use it for to-one relationships, not to-many because of this. It would work ok if you have a one-to-few relationship.

So, you should make a separate request for the comments so you can specify a specific page size and sort order. Note that when you specify the variable to sort by (created) you can also add asc or desc to specify the direction of the sort (so created desc).

like image 160
Wain Avatar answered Feb 22 '26 16:02

Wain