I would like to delete an object from Parse when I un-check the table row.
The issue occurs when trying to delete objects from Parse after having queried them.
this is my code:
if cell.accessoryType == UITableViewCellAccessoryType.Checkmark {
cell.accessoryType = UITableViewCellAccessoryType.None
var query = PFQuery(className:"Followers")
query.whereKey("follower", equalTo: "\(PFUser.currentUser()?.username)")
query.whereKey("following", equalTo: "\(cell.textLabel?.text)")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
for object in objects as! [PFUser] {
object.deleteInBackground()
}
} else {
println(error)
}
}
}
I think the issue is in your query.findObjectsInBackgroundWithBlock
i think its because you are defining objects as! [PFUser] instead of a [PFObject]
try this it should do the trick
query.findObjectsInBackground { (objects, error) in
if error == nil,
let objects = objects {
for object in objects {
object.deleteInBackground()
}
}
I want to delete objects from parse
Yes in the Parse iOS SDK to delete multiple objects in background at once on Parse server, you can use deleteAllInBackground
You can use it with 2 different ways:
PFObject.deleteAll(inBackground: [PFObject]?)
PFObject.deleteAll(inBackground: [PFObject]?, block: PFBooleanResultBlock?)
For example:
query.findObjectsInBackgroundWithBlock({ (objects : [PFObject]?, error: NSError?) -> Void in
PFObject.deleteAll(inBackground: objects)
})
You can also see this post
I hope my answer was helpful 😊
Related
I am trying to delete an object from the class UserRequests via swift only if the object belongs to the current user, and that requestResponded is not equal to true. However, I get an error at objects.deleteInBackground() and the function still doesn't work when I remove this line.
func deleteRequest(){
let check = PFQuery(className: "UserRequests")
check.whereKey("requestResponded", equalTo: "True")
let query = PFQuery(className: "UserRequests")
query.whereKey("username", equalTo: (PFUser.currentUser()?.objectForKey("username") as! String))
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if objects != nil && error == nil{
// Successfully retrieved the object
check.getFirstObjectInBackgroundWithBlock {
(object: PFObject?, error: NSError?) -> Void in
if error != nil || object == nil {
print("Not accepted.")
object!.deleteInBackground()
objects.deleteInBackground()
} else {
print("Successfully retrieved the object.")
}
}
}else{
self.performSegueWithIdentifier("requestAccepted", sender: self)
}
})
}
It is because objects is an list of object. You should only delete object 1 by 1.
For example:
for object in objects {
object.deleteInBackground()
}
Also, because two queries belong to same class. I would suggest using 1 query
UPDATE
func deleteRequest(){
let query = PFQuery(className: "UserRequests")
// the key "requestResponded" is not True
query.whereKey("requestResponded", equalTo: "False")
// for deleting the object is that it belongs to the current user
query.whereKey("username", equalTo (PFUser.currentUser()?.objectForKey("username") as! String))
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error != nil{
print(error)
}
// objects are those the key "requestResponded" is not True and belongs to the current user
for object in objects {
object.deleteInBackground()
}
// other case
if objects.count == 0 { // no match result found
}
})
}
I guess you still miss the condition of when to perform segue
I'm new in Parse. I have leagues class, which has name. I want to take all names from table and show them in table view.
I wrote something like this:
let query = PFQuery(className: "Leagues")
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) in
if( objects != nil && error == nil) {
for i in objects! {
let n = objects[i] as Leagues
}
}else if error != nil {
print("Error is: \(error)")
}
}
Error is:
Type PSObject has no subscript members
What should I do for taking all names from table?
The reason this is happening is that your "i" in your for loop is actually the reference to the PFObject not the PFObject Array.
So the compiler is giving you correct information when it says that the individual PFObject does not have any Subscript members.
Try this:
let query = PFQuery(className: "Leagues")
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) in
if( objects != nil && error == nil) {
for i in objects! {
let n = i as Leagues // Assuming your PFObject is a list of Leagues
}
}else if error != nil {
print("Error is: \(error)")
}
}
I'm trying to delete a received Photo with a button on tableview from parse with swift in x code, my problem is "I Got an error that says (object not found for delete)", and the following code is all i can write to delete this file, is there another way do delete the file or did I use the wrong code
let query: PFQuery = PFQuery(className: "Photos")
query.whereKey("recipientUsername", equalTo: (PFUser.currentUser()?.username)!)
query.whereKey("senderUsername", containsString: usernames[indexPath!.row])
query.findObjectsInBackgroundWithBlock({ (objects : [PFObject]?, error: NSError?) -> Void in
if error == nil {
for object in objects! {
object.deleteInBackground()
}
}
})
I've been getting this Command failed due to signal: Segmentation fault: 11 error for 2 days now, and I cannot wrap my head around why its doing so. The error pointed at a specific query I made to parse.. However to test if it was my code or just a bug, I copied this EXACT block from Parse's query doc into my project as a function:
var query = PFQuery(className:"GameScore")
query.whereKey("playerName", equalTo:"Sean Plott")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
And it threw the error once again to that block.. Xcode is has also been throwing this message to my compiler :
Why is this happening ?
UPDATE
so it seems Kevin's answer below cleared the compiler bug by letting the compiler tell me the type rather than specifying it in the query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in line , by correcting it to :
query.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
}
however this other block is a little more complex, how do i adjust it to rid the error? :
func loadBooks() {
var query = PFQuery(className: "Books")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
self.books.removeAll()
let bookObjects = objects as! [PFObject]
for (index, object) in enumerate(bookObjects) {
self.books.append(Book(pfBook: object))
}
}else if let secondMessage = error?.userInfo?["error"] as? String
where secondMessage == "The Internet connection appears to be offline." {
self.failedMessage(secondMessage)
self.activityIndicator.hidden = true
self.activityIndicator.stopAnimating()
}
dispatch_async(dispatch_get_main_queue()){
self.collectionView!.reloadData()
self.refreshControl.endRefreshing()
self.activityIndicator.stopAnimating()
}
}
}
objects is actually of type [PFObject]? not [AnyObject]?. A wild guess would say the root cause trying to downcast.
Anyway, just use the correct type to fix this
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
}
or just let the compiler tell you the type
query.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
}
let query = PFQuery(className:"GameScore")
query.whereKey("playerName", equalTo:"Sean Plott")
query.findObjectsInBackgroundWithBlock {
(objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
print(objects)
if let objects = objects {
for object in objects {
print(object.objectId)
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
Try this! It works!
I am facing an issue when trying to delete objects from Parse after having queried them.
My code:
var query = PFQuery(className:"sendMessage")
query.whereKey("messageSent", equalTo: PFUser.currentUser()!.username!)
query.whereKey("messageReceived", equalTo: self.nameLabel!.text!)
query.findObjectsInBackgroundWithBlock({ (objects, NSError) -> Void in
if objects != nil {
if let objects = objects as? [PFObject] {
for object in objects {
print(object["message"])
/// here I would go: object.deleteInBackground()
object.save()
}
}
}
})
But it seems that I cannot find the right way to do so. Any insights ?
var query = PFQuery(className:"sendMessage")
let username = PFUser.currentUser()?.username
query.whereKey("messageSent", equalTo: username)
query.whereKey("messageReceived", equalTo: self.nameLabel!.text!)
query.findObjectsInBackgroundWithBlock({ (objects:[AnyObject]?, error:NSError) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
let deletemessage = object["message"] as! String
print(deletemessage)
object.delete()
}
}
}
else {
println("Error")
}
})
I have used deleteEventually() with success before, together with PFObject(withoutDataWithClassName: YourClassName, objectId: YourObjectID).
If that works I wouldn't know why, but well :)
(as stated by Hector in this Parse Question (Objective-C): https://www.parse.com/questions/delete-row)
for object in objects {
print(object["message"]
var toDelete = PFObject(withoutDataWithClassName: "sendMessage", objectId: object.objectID)
toDelete.deleteEventually()
}