Reputation: 276
I'm making a like function for a social wall. When the currentUser does this it will add the objectId from the message to the an Array column in the User class:
func likeBtnClick(sender: AnyObject){
let senderbutton:UIButton = sender as UIButton
println("current row is = \(senderbutton.tag)")
let tempObject:PFObject = ImageTimeLineData.objectAtIndex(senderbutton.tag) as PFObject
println("\(tempObject.objectId)")
PFUser.currentUser().addUniqueObject(tempObject.objectId, forKey: "liked")
PFUser.currentUser().saveInBackground()
}
This is what you will get in the "liked" Array:["q6begjrFE4","s63ehjxFA1"]
This works all well.
Now I want to retrieve the amount of likes from a message in the cellForRowAtIndexPath
and present the amount of likes in the button:
.....
let message:PFObject = self.ImageTimeLineData.objectAtIndex(indexPath!.row) as PFObject
.....
var findLikes:PFQuery = PFUser.query()
findLikes.whereKey("liked", equalTo: message.objectForKey("objectId"))
findLikes.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]!, error:NSError!) -> Void in
if error == nil{
let liked:NSArray = objects as NSArray
println(liked)
println(liked.count)
cell.likedButton.setTitle("\(liked.count)", forState: UIControlState.Normal)
}
}
.....
cell.likedButton.tag = indexPath!.row
cell.likedButton.addTarget(self, action: "likeBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
This will crash the app says it cannot do a comparison query for type null. I've set the Exceptional breakpoint and came to this: +[PFInternalUtils assertValidClassForQuery:]
. It crashes on this: findLikes.whereKey("liked", equalTo: message.objectForKey("objectId"))
line.
#0 0x01833a6b in objc_exception_throw ()
#1 0x01bae86d in +[NSException raise:format:] ()
#2 0x00197f30 in +[PFInternalUtils assertValidClassForQuery:] at /Users/nlutsenko/src/parse/ios-client/Parse/Internal/PFInternalUtils.m:368
#3 0x001679d3 in -[PFQuery whereKey:equalTo:] at /Users/nlutsenko/src/parse/ios-client/Parse/PFQuery.m:195
#4 0x000cce00 in TongerenApp.ImageTimeLineTableViewController.tableView (TongerenApp.ImageTimeLineTableViewController)(Swift.Optional<ObjectiveC.UITableView>, cellForRowAtIndexPath : Swift.Optional<ObjectiveC.NSIndexPath>) -> ObjectiveC.UITableViewCell at /Users/Dax/Desktop/TongerenApp/TongerenApp/ImageTimeLineTableViewController.swift:130
#5 0x000cf143 in @objc TongerenApp.ImageTimeLineTableViewController.tableView (TongerenApp.ImageTimeLineTableViewController)(Swift.Optional<ObjectiveC.UITableView>, cellForRowAtIndexPath : Swift.Optional<ObjectiveC.NSIndexPath>) -> ObjectiveC.UITableViewCell ()
#6 0x022881bc in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] ()
#7 0x0228829e in -[UITableView _createPreparedCellForGlobalRow:willDisplay:] ()
#8 0x02261a6b in -[UITableView _updateVisibleCellsNow:isRecursive:] ()
#9 0x0227c3d1 in -[UITableView layoutSubviews] ()
#10 0x021f1dd1 in -[UIView(CALayerDelegate) layoutSublayersOfLayer:] ()
#11 0x01849771 in -[NSObject performSelector:withObject:] ()
#12 0x0074628f in -[CALayer layoutSublayers] ()
#13 0x0073a115 in CA::Layer::layout_if_needed(CA::Transaction*) ()
#14 0x00739f70 in CA::Layer::layout_and_display_if_needed(CA::Transaction*) ()
#15 0x006983c6 in CA::Context::commit_transaction(CA::Transaction*) ()
#16 0x0069978c in CA::Transaction::commit() ()
#17 0x00699e58 in CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) ()
#18 0x01ad19de in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#19 0x01ad1920 in __CFRunLoopDoObservers ()
#20 0x01ac735a in __CFRunLoopRun ()
#21 0x01ac6bcb in CFRunLoopRunSpecific ()
#22 0x01ac69fb in CFRunLoopRunInMode ()
#23 0x052da24f in GSEventRunModal ()
#24 0x052da08c in GSEventRun ()
#25 0x021668b6 in UIApplicationMain ()
#26 0x0010ff9e in top_level_code at /Users/Dax/Desktop/TongerenApp/TongerenApp/AppDelegate.swift:12
#27 0x0011008b in main ()
#28 0x03885ac9 in start ()
After searching I still don't quite understand what this means. Seems the query is wrong. How can I acces the "liked" array and do a comparison?
Upvotes: 1
Views: 697
Reputation: 12015
My first guess is, that the problem is:
message.objectForKey("objectId")
The syntax objectForKey works with those columns of the PFObjects, that YOU created, but objectId is created by Parse. So instead just use
message.objectId
, because objectId is stored as a property of the PFObject.
Upvotes: 3