Backendless 4.0 // iOS // BackendlessUser // create Relations (friends)

Hello Backendless-Team,

I am trying to create a User 1:N relations.
It is no problem to create the relation in the Backendless Database.

The problem is in updating the currentUser object without logging out and logging in again.

I have ceated two objects.

  • object 1: friendObject
  • object 2: currentUser

I modify the currentUser object with the new friends object. The print looks good (see attachment). But after I perform the update(currentUser) method, I retrieve nil and the compiler throws an error.

This is my configuration:

  • Backendless SDK 4.0b from Podfile
  • Backendless Cloud 4

I tried for several hours and now I don´t now if there is a mistake in my source code.

I simply need a way to update my currentUser Object without logging out and in.
Is there an easier way?

Thank you very much.

Very best regards
Michael

Types.tryblock({ () -> Void in
    
    // load object of friend of type BackendlessUser >> friendObject
    var queryBuilder = DataQueryBuilder()
    queryBuilder!.setRelationsDepth(2)
    queryBuilder = queryBuilder?.setWhereClause("email = '\(self.friendEmail)'")
    let dataStore = self.backendless?.data.of(Users().ofClass())
    let result = dataStore?.find(queryBuilder) as! [BackendlessUser]
    let friendObject = result
    
    
    // create object of current logged in user of type BackendlessUser >> currentUser
    var currentUser = self.backendless?.userService.currentUser
    
    // MARK 1: this print is working very good
    print(currentUser?.getProperty("friends")  as! NSMutableArray)
    
    var currentFriends = NSMutableArray()
    currentFriends = currentUser?.getProperty("friends")  as! NSMutableArray
    currentFriends.add(friendObject.first!)
    print(currentFriends)
    
    // Set property
    currentUser?.setProperty("friends", object:currentFriends)
    currentUser = (self.backendless?.userService.update(currentUser))
    
    // MARK 2: Here is the problem: "fatal error: unexpectedly found nil while unwrapping an Optional value"
    print(currentUser?.getProperty("friends") as! NSMutableArray )


},
    catchblock: { (exception) -> Void in
    print("Server reported an error: \(exception as! Fault)")

































})

Hi Michael,

setting/updating relationships between objects works differently in 4.0. Instead of adding “friends” to the user object and then saving that object, you’d need to make a call to set/add relationship. Please see the documentation:
https://backendless.com/docs/ios/doc.html#data_relations_api_set_add_ios

Regards,
Mark

Dear Mark,

thank you so much for your quick answer. It works now.
I´ve posted my Code below.

Very best regards, Michael

var friendsObjects = [BackendlessUser]()
var currentUser = self.backendless?.userService.currentUser


var queryBuilder = DataQueryBuilder()
queryBuilder!.setRelationsDepth(2)
queryBuilder = queryBuilder?.setWhereClause("email = '\(currentUser.email as! String)'")
let dataStore = self.backendless?.data.of(Users().ofClass())
let result = dataStore?.find(queryBuilder) as! [BackendlessUser]
print(result)


for i in result as! [BackendlessUser] {
    print(i.getProperty("friends"))
    self.friendsObjects = i.getProperty("friends") as! [BackendlessUser]














}