I have cloud code to search for the users relations with other users when they go to their friends page.
Problem I am having is populating the array of PFObjects I have that stores the info of the users friends so it can be displayed in the tableview.
Here is my cloud code:
Parse.Cloud.define("getFriends", function(request, response) {
var userId = request.params.UserKey;
var query = new Parse.Query(Parse.User);
query.get(userId, {
success: function(foundCurrentUser) {
var currentUser = foundCurrentUser;
var relation = currentUser.relation("Friends");
var getRelationQuery = relation.query();
getRelationQuery.find().then(function(results){
response.success(results);
});
},
error: function(error) {
response.error(error);
}
});
});
I check my logs and get the correct response. It contains the correct data in JSON.
Now here is the problem. My cloud query in the iOS app is what is causing me grief.
let currentUser: PFUser = PFUser.currentUser()!
let params = NSMutableDictionary()
params.setObject(PFUser.currentUser()!.objectId!, forKey: "UserKey")
PFCloud.callFunctionInBackground("getFriends", withParameters: params as [NSObject : AnyObject], block: {
(object:AnyObject?, error: NSError?) -> Void in
if error == nil {
self.currentUserFriendList.append(object as! PFObject)
}
else
{
println("didnt retrieve the carpoolers")
}
})
On the line of code I get an issue is self.currentUserFriendList.append(object as!! PFObject)
with the error Could not cast value of type '__NSArrayM' (0x111cd8448) to 'PFObject' (0x10fd8a818).
I want to use a
for object in objects {
self.currentUserFriendList.append(object as! PFObject)
}
But the block isn't an object:[AnyObject]
When I print the object I get the correct relations wrapped in an Optional like so
Optional((
"<PFUser: 0x7fa5db4a6de0, objectId: 1FLleawAQz, localId: (null)> {\n Friends = \"<PFRelation: 0x7fa5db47b1a0, 0x0.(null) -> _User>\";\n email = \"[email protected]\";\n fullName = \"Tester \";\n profilePicture = \"<PFFile: 0x7fa5db4a0140>\";\n username = \"[email protected]\";\n}",
"<PFUser: 0x7fa5db47d5f0, objectId: nqFBQYIVjD, localId: (null)> {\n Friends = \"<PFRelation: 0x7fa5db5c97b0, 0x0.(null) -> _User>\";\n email = \"[email protected]\";\n fullName = test2;\n profilePicture = \"<PFFile: 0x7fa5db554400>\";\n username = \"[email protected]\";\n}"
))
I was able to get the response to output exactly what I wanted and append it to my array of PFObjects like this.
if error == nil {
let objects = response as! [PFObject]
for object in objects as [PFObject] {
self.currentUserFriendList.append(object as PFObject)
println("\(object)")
}