I’m trying to add a user (the current user) to a one to many relationship from groups to users. I tried using the method for updating an object specified as suggested in the api, involving using the same API as for creating the object initially. When I run it, the object is left unchanged.
As you can see, the objectId is of the object I’m trying to update.
Thank you.
SWIFT/IOS
func joinGroup() {
let dataStore = Backendless.sharedInstance().data.of(Groups.ofClass())
backendless.initApp(APP_ID, secret:SECRET_KEY, version:VERSION_NUM)
self.backendless.userService.getPersistentUser()
let user = self.backendless.userService.currentUser
var JoinGroup = Groups()
JoinGroup.objectId = "04E31189-9242-62F5-FF85-B9D22D290100"
JoinGroup.UsersInGroup.append(user)
var error: Fault?
JoinGroup = dataStore.save(JoinGroup) as! Groups
if error == nil {
print("Order has been saved: \(JoinGroup.groupName)")
}
else {
print("Server reported an error: \(error)")
}
}
createGroup and joinGroup functions
func createGroup() {
let groups = self.backendless.persistenceService.of(Groups().ofClass())
backendless.initApp(APP_ID, secret:SECRET_KEY, version:VERSION_NUM)
self.backendless.userService.getPersistentUser()
let user = self.backendless.userService.currentUser
var newGroup = Groups()
newGroup.groupName = "FromClient"
newGroup.ownerId = user.objectId
newGroup.UsersInGroup.append(user)
var error: Fault?
newGroup = groups.save(newGroup) as! Groups
if error == nil {
print("Created group: \(newGroup.groupName)")
}
else {
print("Server reported an error: \(error)")
}
}
func joinGroup() {
let dataStore = Backendless.sharedInstance().data.of(Groups.ofClass())
backendless.initApp(APP_ID, secret:SECRET_KEY, version:VERSION_NUM)
self.backendless.userService.getPersistentUser()
let user = self.backendless.userService.currentUser
var JoinGroup = Groups()
JoinGroup.objectId = "12CD60CC-75D3-7C97-FF02-E46100FEA300"
JoinGroup.UsersInGroup.append(user)
var error: Fault?
JoinGroup = dataStore.save(JoinGroup) as! Groups
if error == nil {
print("Joined group: \(JoinGroup.groupName)")
}
else {
print("Server reported an error: \(error)")
}
}
Does the createGroup() method work for you? is the group created?
Modifying a group by providing it’s objectId is not a good idea. The way you save it, it will overwrite any other properties on the server which might have had value. A better approach would be to lookup the group object from the server by it’s id.
Also, when you update the group, could you check if the “user” object is not null?
Mark
Hello Mark,
Thank you for your response.
Yes, that createGroup()works. I changed the joinGroup method, and it creates a new object each time it runs instead of reporting nil. I tried using queries with whereClauses to equal the datastore, but that causes compiler errors. If you have an example of updating a specified object that would be extremely helpful.
func joinGroup() {
var group = Groups()
backendless.initApp(APP_ID, secret:SECRET_KEY, version:VERSION_NUM)
self.backendless.userService.getPersistentUser()
let user = self.backendless.userService.currentUser
let dataStore = Backendless.sharedInstance().data.of(Groups.ofClass())
print(dataStore)
// update object asynchronously
group.UsersInGroup.append(user)
dataStore.save(
group,
response: { (result: AnyObject!) -> Void in
let joinedGroup = result as! Groups
print("Contact has been updated: \(joinedGroup.objectId)")
},
error: { (fault: Fault!) -> Void in
print("Server reported an error (2): \(fault)")
})
}
In this code you’re creating a brand new group object and then save it with “dataStore.save”. To update an object all you need to do is retrieve an existing object from the server side.
Could you show the code that causes the compile errors?
"Value of type ‘BackendlessCollection’ has no member ‘save’. "
I’m tying to find a way that I could query a specific value, then save it.
func joinGroup() {
var group = Groups()
backendless.initApp(APP_ID, secret:SECRET_KEY, version:VERSION_NUM)
self.backendless.userService.getPersistentUser()
let user = self.backendless.userService.currentUser
let whereClause = "UsersInGroup.objectId = '\(user.objectId)'"
let dataQuery = BackendlessDataQuery()
dataQuery.whereClause = whereClause
var error: Fault?
var dataStore = self.backendless.persistenceService.find(Groups.ofClass(),
dataQuery:dataQuery) as BackendlessCollection
group.UsersInGroup.append(user)
dataStore.save(
group,
response: { (result: AnyObject!) -> Void in
let updatedContact = result as! Contact
print("Contact has been updated: \(updatedContact.objectId)")
},
error: { (fault: Fault!) -> Void in
print("Server reported an error (2): \(fault)")
})
}
BackendlessCollection has no “save”. And that is not what I suggested. I wrote to use the “save” method on the dataStore object. the same “dataStore” you already declare in your program.
When you run the find() operation, it returns BackendlessCollection. You need to get your object OUT OF the collection. Then update THAT object and save it.
Bam, finally got it to work. Sorry for asking a bad question.
func joinGroup() {
self.backendless.userService.getPersistentUser()
let user = self.backendless.userService.currentUser
let dataStore = backendless.data.of(Groups.ofClass())
dataStore.findID(
"A772D4AB-BCB6-1C2E-FFE9-C436C65E3200",
response: { (result: AnyObject!) -> Void in
let foundContact = result as! Groups
let groupJoining = foundContact
groupJoining.UsersInGroup.append(user)
dataStore.save(
groupJoining,
response: { (result: AnyObject!) -> Void in
let savedContact = result as! Groups
print("Contact has been saved: \(savedContact.objectId)")
},
error: { (fault: Fault!) -> Void in
print("Server reported an error (1): \(fault)")
})
print("Contact has been found: \(foundContact.objectId)")
},
error: { (fault: Fault!) -> Void in
print("Server reported an error (2): \(fault)")
})
}