简体   繁体   中英

Swift - load from Parse table based on array values

I have the following flow in my app:

  • User selects to view his contacts list
  • I go and load from the FriendsConnection table the array of friends (usernames) that match the current user logged (basically friends of current user). Store in array
  • Another database query to the Users class in Parse to get the information of related to each username in the array stored locally. Store this locally
    • Populate Table View with contact list and once clicked show details.

For step 2 above, I have:

var friendsUsernamesListArray: [String] = []
func loadFriendsUsernames()
    {
        let query = PFQuery(className:"FriendsConnections")

        query.whereKey("appUsername", equalTo:PFUser.currentUser()!["appUsername"])
        query.findObjectsInBackgroundWithBlock {
            (objects: [PFObject]?, error: NSError?) -> Void in

            if error == nil{
                //print("Successful")
                //print(objects)

                self.friendsUsernamesListArray = objects![0]["friendsList"] as! NSArray as! [String]


            }else{
                print("Failure")
            }

        }
    }

and now the values of friendsUsernamesListArray are:

["username1", "username2", "username3"]

I am not sure how to do the third part to get one major object in return with one request (to avoid a single request for each username in the array above). What I want is probably to get something like this:

User1 -> [name: XXXXX, age: 20, number:019282992] User2 -> [name: YYYY, age: 22, number:045527229]

Thanks for your help in advance

Found the answer and sharing it for future reference for similar issues:

The solution is to use containedIn keyword as provided by Parse PFQuery.

func loadFriendsInfo()
    {
        let query : PFQuery = PFUser.query()!

        query.whereKey("appUsername", containedIn: self.friendsUsernamesListArray)
        query.findObjectsInBackgroundWithBlock {
            (objects: [PFObject]?, error: NSError?) -> Void in

            if error == nil{
                print(objects)
            }else{
                print("Getting Usernames Info Failure")
                print(error)
            }

        }
    }

The above code works and happy to answer any questions related to it.

You could also try this (slightly similar to the previous answer)

let query = PFQuery(className: "Users")
 query.findObjectsInBackgroundWithBlock{(objects, error) -> Void in 
    if objects.count > 0 {
        for object in objects {
            for friend in self.friendsUsernamesListArray {
                   if object["username"] == friend {
                        //get objects info and add it to other things. E. g. object["email"]
                   }
              }
         }
    }
}

Let me know if this works. You may need to add some question/ exclamation marks for optionals. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM