Getting index of an object returns nil

Hello,

I’m trying to get the index of an object (user), in a one to many relationship. It returns nil.









    func leaveHouse() {

        

        self.backendless.userService.getPersistentUser()

        let user = self.backendless.userService.currentUser

        

        var dataStore = backendless.data.of(House.ofClass())

        dataStore.findID(

            "B52F6BEA-79F8-A58B-FF15-AF840BCB2A00",

            response: { (result: AnyObject!) -> Void in

                var LookingForHouse = result as! House

                let index = LookingForHouse.UsersInHouse.indexOf(user)

                print(index)


                dataStore.save(

                    LookingForHouse,

                    response: { (result: AnyObject!) -> Void in

                        let HouseJoining = result as! House

                        print("House has been saved: \(HouseJoining.houseName)")

                        

                    },

                    error: { (fault: Fault!) -> Void in

                        print("Server reported an error (1): \(fault)")

                })

                

                

                print("House has been found: \(LookingForHouse.objectId)")

            },

            error: { (fault: Fault!) -> Void in

                print("Server reported an error (2): \(fault)")

        })

    }









import Foundation




class House : NSObject{




        var houseName: String?

        var UsersInHouse: [BackendlessUser] = []

        var ownerId: String?

        var objectId : String?

    

}

Hi Lekan,

Please keep in mind that related objects are not returned by default. This is done to minimize the payload of the data. You can request additional relations to be loaded by using the API described here:
https://backendless.com/documentation/data/ios/data_relations_retrieve.htm

Regards,
Mark

Thanks Mark.

I created a workaround before I read this.


    func leavingGroup() {

        

        self.backendless.userService.getPersistentUser()

        let user = self.backendless.userService.currentUser

        

        var dataStore = backendless.data.of(Groups.ofClass())

        dataStore.findID(

            "B52F6BEA-79F8-A58B-FF15-AF840BCB2A00",

            response: { (result: AnyObject!) -> Void in

                var LookingForGroup = result as! Groups

                

                var groupUserData = LookingForGroup.UsersInGroup.description

                // “Cleaned” groupUserData of unnecessary data and created groupDataCleaned

                var groupDataCleaned: String = groupUserData.stringByReplacingOccurrencesOfString("\"", withString: "").stringByReplacingOccurrencesOfString("<BackendlessUser> {", withString: "").stringByReplacingOccurrencesOfString(" ", withString: "").stringByReplacingOccurrencesOfString("___class=Users;", withString: "").stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).stringByReplacingOccurrencesOfString("},", withString: "").stringByReplacingOccurrencesOfString("]", withString: "").stringByReplacingOccurrencesOfString("}", withString: "").stringByReplacingOccurrencesOfString("[", withString: "").stringByReplacingOccurrencesOfString("created=", withString: "").stringByReplacingOccurrencesOfString("email=", withString: "").stringByReplacingOccurrencesOfString("name=", withString: "").stringByReplacingOccurrencesOfString("objectId=", withString: "").stringByReplacingOccurrencesOfString("ownerId=", withString: "").stringByReplacingOccurrencesOfString("password=", withString: "").stringByReplacingOccurrencesOfString("updated=", withString:"").stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

                

                

                

                // Converting groupDataCleaned from a string to an array called arrayofUsers, where the arrays values are anything between whitespace, so the string cookie = “chocolate cake hottea” would be an array [“chocolate”, “cake”, “hottea"]

                

                let arrayofUsers: Array = groupDataCleaned.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

                

                // Converting arrayofUsers into string, and replacing some characters. I did the filter here instead of groupList because it produced different results, and I preferred these results here.

                var groupListClean2: String = arrayofUsers.joinWithSeparator("").stringByReplacingOccurrencesOfString(";", withString: ",")

                

                

                // Once I’ve cleaned the string completely, I’m turning it into an array again called arrayofUsers2. I’m separating everything with a coma.

                var arrayofUsersData: Array = groupListClean2.componentsSeparatedByString(",")

                

                // I’m getting the index number of the current user's objectId in the data. It pulls out the index of one property belonging to one user, in a list of properties belonging to many users.

                var indexnumber = arrayofUsersData.indexOf(user.objectId)

                var indexnumber2 = indexnumber

                var index = 0

                

                // Since objectIds are always in the same location (7 away from the next in our case, if a new column is added, then that away increments), if its less than really 6 (I set 3 to be safe), then its the users index is 0. If its not, then I’ll keep subtracting by 7, and incrementing the index value each time I subtract. Each user can be identified by jumping 7 indexes, this shows me how many times I have to do that jump, the amount of times is where the user is.

                

                if indexnumber2 <= 3 {

                    index = 0

                } else {

                    repeat {

                        

                        indexnumber2 = indexnumber2! - 7

                        index = index + 1

                    } while indexnumber2 > 3

                }

                

                

                LookingForGroup.UsersInGroup.removeAtIndex(index)

                

                dataStore.save(

                    LookingForGroup,

                    response: { (result: AnyObject!) -> Void in

                        let GroupJoining = result as! Groups

                        print("Group has been saved: \(GroupJoining.groupName)")

                        

                    },

                    error: { (fault: Fault!) -> Void in

                        print("Server reported an error (1): \(fault)")

                })

                

                

                print("Group has been found: \(LookingForGroup.objectId)")

            },

            error: { (fault: Fault!) -> Void in

                print("Server reported an error (2): \(fault)")

        })

    }

Hi Lekan,

Sounds like you have resolved the problem. I will mark this topic as “Solved”.

Regards,
Mark