Tutorials

Backendless 4 friend function fix for QuickChat 2.0

Since Backendless Version 4 is different then the Version 3, some of the students were having issues adding friends to the user.

Here is a fix how to change the code to work with Backendless version 4. We have to change 4 files in the App. The change is only for load function, also we are adding 1 extra function to each file.

you have to update 4 files to make it work, below is each file with its code updates, the rest of the functions stay as they are

Friend.swift file

import Foundation
class Friend: NSObject {
    
    var objectId: String?
    var userOneId: String?
//    var userTwo: BackendlessUser? <---Old Code
    var userTwo: String? //<-----New Code
}

FriendsViewController.swift filr

    //Replace old load with this
    func loadFriends() {
        
        cleanup()
        
        let whereClause = "userOneId = '\(backendless!.userService.currentUser.objectId!)'"
        let queryBuilder = DataQueryBuilder()
        queryBuilder!.setWhereClause(whereClause)
        dataStore?.find(queryBuilder, response: {
            (friends_) -> () in
            
            if friends_ != nil {
                print("have friends")
                let friends = friends_! as! [Friend]
                
                self.friendObjects = friends
                
                for friend in friends {
                    self.friendId.append(friend.userTwo as! String)
                }
                
                //get friends from thier Ids
                self.fetchFriends(withIds: self.friendId)
                
                self.tableView.reloadData()
                
            }
        }, error: {
            (fault : Fault?) -> () in
            print("Couldnt load friends: \(fault!.detail)")
        })
        
    }
    
    //new function
    func fetchFriends(withIds: [String]) {
        
        let string = "'" + withIds.joined(separator: "', '") + "'"
        let whereClause = "objectId IN (\(string))"
        let queryBuilder = DataQueryBuilder()
        queryBuilder!.setWhereClause(whereClause)
        
        let dataStore = backendless!.persistenceService.of(BackendlessUser.ofClass())
        dataStore?.find(queryBuilder, response: {
            (allUsers) -> () in
            
            if allUsers != nil {
                
                for friendUser in allUsers as! [BackendlessUser] {
                    self.friends.append(friendUser)
                }
                self.tableView.reloadData()
            }
            
        }, error: {
            (fault : Fault?) -> () in
            print("Couldnt load all friends: \(fault!.detail)")
        })
    }

EditGroupViewController.swift

//new load function
func loadFriend() {
        
        cleanup()
        
        var friendIds = [String]()
        
        let whereClause = "userOneId = '\(backendless!.userService.currentUser.objectId!)'"
        let queryBuilder = DataQueryBuilder()
        queryBuilder!.setWhereClause(whereClause)
        
        dataStore?.find(queryBuilder, response: {
            (friendsFromBackendless) -> () in
            
            
            if friendsFromBackendless != nil {
                
                let localFriends = friendsFromBackendless! as! [Friend]
                
                
                for friend in localFriends {
                    
                    if !(self.group![kMEMBERS] as? [String])!.contains((friend.userTwo)! as String) {
                        
                        friendIds.append(friend.userTwo!)
                    }
                    
                }
                
                //get friends from thier Ids
                self.fetchFriends(withIds: friendIds)
                self.tableView.reloadData()
            }
            
        }, error: {
            (fault : Fault?) -> () in
            ProgressHUD.showError("Couldnt load friends: \(fault!.detail!)")
        })
        
        
    }
    
    //new function
    func fetchFriends(withIds: [String]) {
        
        let string = "'" + withIds.joined(separator: "', '") + "'"
        let whereClause = "objectId IN (\(string))"
        let queryBuilder = DataQueryBuilder()
        queryBuilder!.setWhereClause(whereClause)
        
        let dataStore = backendless!.persistenceService.of(BackendlessUser.ofClass())
        
        dataStore?.find(queryBuilder, response: {
            (allUsers) -> () in
            
            if allUsers != nil {
                
                for friendUser in allUsers as! [BackendlessUser] {
                    self.friends.append(friendUser)
                }
                self.tableView.reloadData()
            }
            
        }, error: {
            (fault : Fault?) -> () in
            print("Couldnt load all friends: \(fault!.detail)")
        })
        
        
    }

AddGroupViewController.swift

//new load function
    func loadFriends() {
    
        cleanup()
        
        var friendIds = [String]()
        
        let whereClause = "userOneId = '\(backendless!.userService.currentUser.objectId!)'"
        let queryBuilder = DataQueryBuilder()
        queryBuilder!.setWhereClause(whereClause)
        
        dataStore?.find(queryBuilder, response: {
            (allFriends) -> () in
            
            if allFriends != nil {
                let friends = allFriends! as! [Friend]
                
                for friend in friends {
                    friendIds.append(friend.userTwo!)
                }
                
            }
            //get friends from thier Ids
            self.fetchFriends(withIds: friendIds)
            
            self.tableView.reloadData()
            
        }, error: {
            (fault : Fault?) -> () in
            ProgressHUD.showError("Couldnt load friends \(fault!.detail)")
        })
        
    }
    
    //new function
    func fetchFriends(withIds: [String]) {
        
        let string = "'" + withIds.joined(separator: "', '") + "'"
        let whereClause = "objectId IN (\(string))"
        
        let queryBuilder = DataQueryBuilder()
        queryBuilder!.setWhereClause(whereClause)
        
        let dataStore = backendless!.persistenceService.of(BackendlessUser.ofClass())
        
        dataStore?.find(queryBuilder, response: {
            (allUsers) -> () in
            
            if allUsers != nil {
                let friends = allUsers as! [BackendlessUser]
                
                for friendUser in friends {
                    self.friends.append(friendUser)
                }
                
                self.tableView.reloadData()
                
                if self.friends.count == 0 {
                    ProgressHUD.showError("Currently you have no friends, please add some")
                }
            }
            
        }, error: {
            (fault : Fault?) -> () in
            print("Couldnt load all friends: \(fault!.detail)")
        })
        
        
    }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.