-1

抱歉标题模棱两可。为了让我保存一个特定的对象,我需要查询它,它返回一个集合。编辑后如何保存该集合?

任何帮助表示赞赏。

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



}
4

2 回答 2

0

您可以只保存一个父对象(前提是您之前已经加载了子集合),并且添加了新对象的子集合将自动保存。

于 2016-03-16T20:43:43.680 回答
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)")
                    })




    }
于 2016-03-17T07:21:07.530 回答