I have a table which stores who is following who and who is being followed. I am trying to have checkmarks show up when the user is being followed. (I am using a table view). I’m not sure how to use the data from my table to run a check to see who the current user is following. My code is below:
func fetchingUserDataAsync() {
self.userids.removeAll(keepCapacity: true)
self.usernames.removeAll(keepCapacity: true)
self.isFollowing.removeAll(keepCapacity: true)
print("\n============ Fetching user data using the ASYNC API ============")
let query = BackendlessDataQuery()
backendless.persistenceService.of(BackendlessUser.ofClass()).find(
query,
response: { ( name : BackendlessCollection!) -> () in
let currentPage = name.getCurrentPage()
for names in currentPage as! [BackendlessUser] {
//print("User name = \(names.name)")
if(names.name != self.backendless.userService.currentUser.name){
self.usernames.append(names.name)
self.backendless.persistenceService.of(BackendlessUser.ofClass()).find(
query,
response: { ( objectId : BackendlessCollection!) -> () in
let currentPage = objectId.getCurrentPage()
for objectIds in currentPage as! [BackendlessUser] {
print("User id = \(objectIds.objectId)")
if(objectIds.objectId != self.backendless.userService.currentUser.objectId){
self.userids.append(objectIds.objectId)
self.backendless.persistenceService.of(Follow.ofClass()).find(
query,
response: { follow -> () in
if let follow = follow{
self.isFollowing.append(true)
} else{
self.isFollowing.append(false)
}
if self.isFollowing.count == self.usernames.count{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
}
},
error: { ( fault : Fault!) -> () in
print("Server reported an error: \(fault)")
}
)
}
}
},
error: { ( fault : Fault!) -> () in
print("Server reported an error: \(fault)")
}
)
}
}
},
error: { ( fault : Fault!) -> () in
print("Server reported an error: \(fault)")
}
)
//////////////////////////////////////////////////////////////////////////////////
}
Please clarify your use case, because I didn’t understand it from your code (in particular, why did you retrieve all users twice).
What do you mean “I have a table which stores who is following who and who is being followed”? Who are these followers and where they have been stored: maybe in some BackendlessUser property as 1:N relation?
They are stored in an app table as in the screenshot below. The table has the properties of follower, following, objectId, etc. The usernames and userIds are stored in two separate arrays in my code after they are retrieved from back end less. I retrieve the users twice, because their user names will be displayed in a table, and then I retrieve the object ids of the users so that I can still track who they are following and who is following them if they happen to change their username. When a user taps on the table view I have on the app it will follow that user who is being tapped on. I have not set up the unfollow mechanism yet. I am trying to run a check to see what users the current user is following and then only append the isTrue array so that it only displays a checkmark next to who the current user is following. I hope that made it clearer. I did not set it up as a relational table. http://support.backendless.com/public/attachments/f9dba4c1b51d7af877de3431d3ddfab3.png</img>
I tried your second suggestion. I did thee query like in the code below, but it keeps returning nil and since it is nil it returns false in my if statement. I may just not understand how your queries work, which is not your fault you have lots of documentation I am just unfamiliar.
self.backendless.persistenceService.of(Follow.ofClass()).find(
query,
response: { follower -> () in
print ("This is the follower = '\(Follow().follower)'")
if follower == self.backendless.userService.currentUser.objectId {
self.isFollowing.append(true)
} else{
self.isFollowing.append(false)
}
if self.isFollowing.count == self.usernames.count{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
}
},
error: { ( fault : Fault!) -> () in
print("Server reported an error: \(fault)")
}
)
What does your query look like?
I think this is what you mean correct?
let query = BackendlessDataQuery()
Also thank you for all your help thus far, I am truly thankful
Yes, but that code loads only one page of data. How do you load the related objects? Do you use auto-load?
I actually don’t know. All the code I used to make my queries are in my first post. I am still learning your api’s so I am not sure if I auto-loaded or not.