对于含糊不清的标题感到抱歉。为了让我保存一个特定的对象,我需要查询它,它返回一个集合。我编辑后如何保存该集合?
任何帮助表示赞赏。
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
for joiningroup in dataStore.data as! [Groups] {
joiningroup.UsersInGroup.append(user)
}
以下是保存对象的方法
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)")
}
}
答案 0 :(得分:0)
您可以保存父对象(前提是您已经加载了子集合),并且子集合将自动保存并添加新对象。
答案 1 :(得分:0)
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)")
})
}