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)")
})