Created
August 25, 2015 18:44
-
-
Save mosssdesign/e4b412ac3fd01dadb455 to your computer and use it in GitHub Desktop.
Get profile for user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
YOU ARE GREET TO ASS.