Skip to content

Instantly share code, notes, and snippets.

@mosssdesign
Created August 25, 2015 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosssdesign/e4b412ac3fd01dadb455 to your computer and use it in GitHub Desktop.
Save mosssdesign/e4b412ac3fd01dadb455 to your computer and use it in GitHub Desktop.
Get profile for user
func getProfilesByPoints(completionHandler: (users:NSMutableArray) -> ()) {
var users = NSMutableArray()
var query = PFQuery(className:"points")
query.addDescendingOrder("points")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// print("Successfully retrieved \(objects.count) user(s)")
for object in objects {
var userId = object.valueForKey("userId") as! String?
var Username = ""
var UserImageFile: PFFile!
var Verified = false
var userPoints = 0
if let points = object.valueForKey("points") as! Int?{
userPoints = points
}
println("opId:\(userId)")
self.getProfileForTopUser(userId!, completionHandler: { (username, profileImageFile) -> () in
Username = username
UserImageFile = profileImageFile
users.addObject([userId!, Username, UserImageFile, userPoints, Verified])
println("newusersss:\(users)")
})
// It shouldn't fetch username from point table but the user table
// if let usern = object.valueForKey("username") as! String?{
// Username = usern
// }else
// {
// self.getProfileForUser(userId!, completionHandler: { (username, profileInfo, email, profileImage) -> () in
// // Fetch User Name
// Username = username
//
// })
// }
// if let authorImageFile = object["profileImage"] as! PFFile?
// {
// users.addObject([userId!, Username, authorImageFile, userPoints, Verified])
// }
// else
// {
// var imageFile = PFFile(data: NSData(data: UIImagePNGRepresentation(UIImage(named: "profileAvatar")!)))
// users.addObject([userId!, Username, imageFile, userPoints])
//
// }
}
if users.count == objects.count {
completionHandler(users: users)
}
} else {
print("Error: %@ %@", error, error.userInfo!)
}
}
}
func getProfileForTopUser(userID:String, completionHandler: (username: String, profileImageFile: PFFile) -> ()) {
var query = PFQuery(className:"_User")
query.whereKey("objectId", equalTo:userID)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
var name: String!
var file: PFFile!
for object in objects {
name = object["username"] as! String!
if let imageFile = object["profileImage"] as? PFFile {
file = imageFile
} else {
file = PFFile(data: NSData(data: UIImagePNGRepresentation(UIImage(named: "profileAvatar")!)))
}
}
completionHandler(username: name, profileImageFile: file)
} else {
print("Error: %@ %@", error, error.userInfo!)
}
}
}
@alucheri
Copy link

alucheri commented Jan 1, 2018

YOU ARE GREET TO ASS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment