When the tableview loads I retrieve the Parse Object "User", and load it into a userArray.
func loadUserData(){
var query = PFUser.query()
query?.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error != nil {
println(error)
} else if let objects = objects as? [PFObject] {
println(objects)
// loop through results. print(results)
for object in objects {
self.userArray.addObject(object)
// code to download an image
let imageFile = object["image"] as! PFFile
imageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error != nil {
println(error)
} else {
if let data = imageData {
self.images.append(UIImage(data: data)!)
self.tableView.reloadData()
}
}
}
}
self.tableView.reloadData()
}
}
}
When a user selects a tableView cell, that PFUser.objectId is sent into an array of [selectedFriends], which is created at the top of the class.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.cellForRowAtIndexPath(indexPath)
let row = indexPath.row
let cellDataParse : PFObject = self.userArray.objectAtIndex(row) as! PFObject
var user = userArray[row] as! PFUser
var iD = user.objectId
var selectedFriend = iD
if cell?.selected == true {
cell!.accessoryType = .Checkmark
// change made here ----????
selectedFriends.append(selectedFriend!)
println(selectedFriends)
}
if cell?.selected == false && selectedFriends.count == 2 {
selectedFriends.removeAtIndex(1)
}
if self.selectedFriends.count > 5 {
var alert = UIAlertController(title: "Team Maximum", message: "Max team size is 5 friends\n You can only choose maximum 4 friends for your team.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: { action in
self.selectedFriends.removeLast()
cell?.accessoryType = .None
cell?.selected = false
})
}
}
At this point I can successfully select a indexPath.row, and add the objectId of the user into the array of selectedFriends (Which I declared at the top of my class).
My goal is to take all of the objectId's added into the array, and send them to a Parse array called "team".
Here is the code for this:
func createNewTeam(){
var user = PFUser.currentUser()
if selectedFriends.count > 0 {
user!.addObjectsFromArray([selectedFriends], forKey: "team")
user!.saveInBackground()
}
tableView.reloadData()
selectedFriends.removeAll(keepCapacity: true)
println("something should have happend")
}
I then added this function to an action Button. When I pressed the button I got this error:
2015-09-09 12:52:56.651 Storm[11039:1498653] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Operation is invalid after previous operation.' * First throw call stack: ( 0 CoreFoundation 0x00000001023b3c65 exceptionPreprocess + 165 1 libobjc.A.dylib 0x00000001040cabb7 objc_exception_throw + 45 2 Storm 0x0000000100dc2ae3 -[PFAddOperation applyToValue:forKey:] + 0 3 Storm 0x0000000100dd73bd -[PFObject(Private) performOperation:forKey:] + 206 4 Storm 0x0000000100ddea53 -[PFObject addObjectsFromArray:forKey:] + 126 5 Storm 0x0000000100d64596 _TFC5Storm29SearchTeamTableViewController13createNewTeamfS0_FT_T_ + 486 6 Storm 0x0000000100d5ebe4 _TFC5Storm29SearchTeamTableViewController18nextButtonDidPressfS0_FPSs9AnyObject_T_ + 68 7 Storm 0x0000000100d5ecd6 _TToFC5Storm29SearchTeamTableViewController18nextButtonDidPressfS0_FPSs9AnyObject_T_ + 54 8 UIKit 0x0000000102dffd62 -[UIApplication sendAction:to:from:forEvent:] + 75 9 UIKit 0x0000000102dffd62 -[UIApplication sendAction:to:from:forEvent:] + 75 10 UIKit 0x0000000102f1150a -[UIControl _sendActionsForEvents:withEvent:] + 467 11 UIKit 0x0000000102f108d9 -[UIControl touchesEnded:withEvent:] + 522 12 UIKit 0x0000000102e4c958 -[UIWindow _sendTouchesForEvent:] + 735 13 UIKit 0x0000000102e4d282 -[UIWindow sendEvent:] + 682 14 UIKit 0x0000000102e13541 -[UIApplication sendEvent:] + 246 15 UIKit 0x0000000102e20cdc _UIApplicationHandleEventFromQueueEvent + 18265 16 UIKit 0x0000000102dfb59c _UIApplicationHandleEventQueue + 2066 17 CoreFoundation 0x00000001022e7431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 18 CoreFoundation 0x00000001022dd2fd __CFRunLoopDoSources0 + 269 19 CoreFoundation 0x00000001022dc934 __CFRunLoopRun + 868 20 CoreFoundation 0x00000001022dc366 CFRunLoopRunSpecific + 470 21 GraphicsServices 0x000000010647ea3e GSEventRunModal + 161 22 UIKit 0x0000000102dfe8c0 UIApplicationMain + 1282 23 Storm 0x0000000100d4c117 main + 135 24 libdyld.dylib 0x0000000104800145 start + 1 25 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException
I apologize. This is a lot to read. This is also my first post on stack overflow. Could someone help???