How to get all friends object from backendless in iOS swift???

I am making chatting app using backendless and firebase.

I have one problem now.

following func is one that loads all friends object from backendless.

func loadFriends() {

    cleanup()
    
    let whereClause = "userOneId = '\(backendless!.userService.currentUser.objectId!)'"
    
    let dataQuery = DataQueryBuilder()
    dataQuery?.setWhereClause(whereClause)
    
    dataStore!.find(dataQuery, response: { (friends_) in
        
        if friends_ != nil {
            
            
            let friends = friends_! as! [Friend]
            
            self.friendObjects = friends
            
            for friend in friends {
                print("Current user is \(String(describing: backendless!.userService.currentUser.objectId!))")
                print("friends user is \(String(describing: friend.objectId))")
                print("friends user is \(String(describing: friend.userOneId))")
                self.friends.append(friend.userTwo!)                                          /// Getting error in here
                self.friendId.append(friend.userTwo!.objectId as String)
            }
            
            self.tableView.reloadData()
            
            if self.friends.count == 0 {
                
                ProgressHUD.showError("Currently you have no friends")
            }
            
        }
        
        
        }) { (fault) in
            
            print("Couldnt load friends: \(fault!.detail)")
    }
}

next func is one that saves selected user.

func saveFriend(selectedFriend: BackendlessUser) {

    if friendId.contains(selectedFriend.objectId as String) {
        
        return
    }
    
    let friend = Friend()
    friend.userOneId = backendless!.userService.currentUser.objectId as String
    friend.userTwo = selectedFriend
    
    dataStore!.save(friend, response: { (result) in
        
            self.loadFriends()
        
        }) { (fault) in
            
            ProgressHUD.showError("Error saving friend:\(fault!.detail)")
    }
}

Could you please help me?

Hi, Andrey.
To help you right, i need to know the data structure of you tables. Please, shortly describe it here and provide appId.
You may also investigate this article from our documentation: https://backendless.com/docs/ios/doc.html#single-step-retrieval

application id : 3640EA40-3CAA-D6B6-FF26-2F28DABE6C00

Above screenshot is my table structure.

please check this error.

Hello, Oleg
Could you please let me know whether to solve this problem or not?

Hello Andrei,

Please provide the description of the self.friends property. Also I’ve checked this part of code and it works fine.

let queryBuilder = DataQueryBuilder()!
 queryBuilder.setWhereClause("userOneId = 'current User objectId here'")
 
 let dataStore = backendless.data.of(Friend.ofClass())!
 dataStore.find(queryBuilder,
 response: { friends_ in
 if friends_ != nil {
 let friends = friends_! as! [Friend]
 for friend in friends {
 print("friends user is \(friend.objectId!)")
 print("friends user is \(friend.userOneId!)")
 print("friends user is \(friend.userTwo!)")
 print("******************")
 let userTwo = friend.userTwo!
 
 //friends.append(friend.userTwo!) /// Getting error in here
 }
 }
 },
 error: { fault in
 print("Error: \(fault!)")
 })

Screen Shot 2017-12-06 at 10.24.10.png

Thanks for your responsing!

This is my friend table.

and also this is all user table.

I’ve tested my code on your application, as you can see it works fine for me.
Could you please archive the simple project that reproduces your error and send it to the support@backendless.com. Or you can pin it here.

Regards, Olga

yes

This is the definition of “Friend” class

http://prntscr.com/hjra0w

Haven’t receive your project yet. Could you please try my code? Is it working fine for you?

let queryBuilder = DataQueryBuilder()!
queryBuilder.setWhereClause("userOneId = 'current User objectId here'")
let dataStore = backendless.data.of(Friend.ofClass())!
dataStore.find(queryBuilder,
response: { friends_ in
if friends_ != nil {
let friends = friends_! as! [Friend]
for friend in friends {
print("friends user is \(friend.objectId!)")
print("friends user is \(friend.userOneId!)")
print("friends user is \(friend.userTwo!)")
print("******************")
let userTwo = friend.userTwo!
//friends.append(friend.userTwo!) /// Getting error in here
}
}
},
error: { fault in
print("Error: \(fault!)")
})

Regards, Olga

Thanks, Olga

Above code is same mine.

In above code, I can’t get “userTwo” item.

“userTwo” parameter is inherited by BackendlessUser

What version of Xcode and Swift do you use?
If you’re using Xcode 9 + Swift 4 please notice, that classes declared in Swift 4 must have the @objcMembers attribute (more details here). If it won’t help please share the simple example project that reproduces the issue because we can’t reproduce it.

Regards, Olga

ok

thanks for your helping!

regards
andrei

Is your problem solved?

Regards, Olga

yes.
I have solved this problem with the other solution.

Thanks for your helping.

If I have any issue during developing, I will ask to you soon.

Best regards

Andrei Opanasenko