Backendless Support
 
Answered

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?

Leave a Comment

Comments (15)

photo
1

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

photo
1

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

http://prntscr.com/hjgjn1

Above screenshot is my table structure.

photo
1

http://prntscr.com/hjh3id

please check this error.

photo
1

Hello, Oleg

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

photo
1

Hello Andrei,

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

  1. let queryBuilder = DataQueryBuilder()!
  2. queryBuilder.setWhereClause("userOneId = 'current User objectId here'")
  3. let dataStore = backendless.data.of(Friend.ofClass())!
  4. dataStore.find(queryBuilder,
  5. response: { friends_ in
  6. if friends_ != nil {
  7. let friends = friends_! as! [Friend]
  8. for friend in friends {
  9. print("friends user is \(friend.objectId!)")
  10. print("friends user is \(friend.userOneId!)")
  11. print("friends user is \(friend.userTwo!)")
  12. print("******************")
  13. let userTwo = friend.userTwo!
  14. //friends.append(friend.userTwo!) /// Getting error in here
  15. }
  16. }
  17. },
  18. error: { fault in
  19. print("Error: \(fault!)")
  20. })

photo
1

Thanks for your responsing!

This is my friend table.

http://prntscr.com/hjqsjl

and also this is all user table.

http://prntscr.com/hjqt6h

photo
1

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

photo
1

yes

This is the definition of "Friend" class

http://prntscr.com/hjra0w

photo
1

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

  1. let queryBuilder = DataQueryBuilder()!
  2. queryBuilder.setWhereClause("userOneId = 'current User objectId here'")
  3. let dataStore = backendless.data.of(Friend.ofClass())!
  4. dataStore.find(queryBuilder,
  5. response: { friends_ in
  6. if friends_ != nil {
  7. let friends = friends_! as! [Friend]
  8. for friend in friends {
  9. print("friends user is \(friend.objectId!)")
  10. print("friends user is \(friend.userOneId!)")
  11. print("friends user is \(friend.userTwo!)")
  12. print("******************")
  13. let userTwo = friend.userTwo!
  14. //friends.append(friend.userTwo!) /// Getting error in here
  15. }
  16. }
  17. },
  18. error: { fault in
  19. print("Error: \(fault!)")
  20. })

Regards, Olga

photo
1

Thanks, Olga

photo
1

Above code is same mine.

In above code, I can't get "userTwo" item.

"userTwo" parameter is inherited by BackendlessUser

photo
1

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

photo
1

ok

thanks for your helping!

regards

andrei

photo
1

Is your problem solved?

Regards, Olga

photo
1

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