Delete received Object from Parse Backend service with Swift, xcode - ios

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()
}
}
})

Related

PFQuery not running after Swift 3 conversion

I migrated to Swift 3 and have updated all my Parse functions to the latest syntax. Now, none of the queries return anything. There is no no error but there are also no objects. Whats weird is that it doesn't look like its even making a call, as it instantly returns no objects and there is no activity indicator spinning in the status bar like usual. Here's the query code:
let profileQuery:PFQuery = PFQuery(className: "_User")
profileQuery.whereKey("emailLowercase", equalTo: emailField.text!.lowercased() as String)
profileQuery.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
print(objects?.count)
})
Thanks!
Try it as such:
let Query = PFQuery(className: "_User")
Query.whereKey("emailLowercase", equalTo: emailField.text!.lowercased() as String)
Query.findObjectsInBackground(block: { (objects, error) -> Void in
if (error == nil) {
print("Success")
print(objects?.count)
} else {
print("Error")
}
})
Your query code should be like this;
let query = PFUser.query()
The only thing wrong with your as Baris has suggest is that you are querying the class wrong. You can't query with ("_User")
let profileQuery = PFUser.query()
profileQuery.whereKey("emailLowercase", equalTo: emailField.text!.lowercased() as String)
profileQuery.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
if error == nil{
print(objects?.count)
}
})

How do I gain access to the info inside a PFObject?

I am importing the objects from parse, but I need to gain access to the information inside. The object has the name and the address of a user, and I need to get those. How would I do that?
let query = PFQuery(className: "People")
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) in
if(error == nil){
for object in objects!{
self.peopleObj.append(object)
}
}else{
print(error)
}
}
Would I do something like peopleObj["Name"], I don't think that is the correct syntax for a PFObject.
Just make a loop to access the single object and then fetch value like below:
for Oneobject in objects
{
let strAddress = Oneobject["address"] as String
let strName = Oneobject["name"] as String
}
Refer the following link:
https://parse.com/docs/ios/guide#objects-retrieving-objects

Why is the for loop being skipped over?

I am trying to get an array of a certain row which is equal to the name, but for some reason the for loop is getting skipped over. I put a breakpoint, but the breakpoint never gets called.
let query = PFQuery(className: "Tutors")
query.whereKey("name", equalTo: self.name.text!)
query.findObjectsInBackgroundWithBlock ({
(objects: [PFObject]?, error: NSError?) -> Void in
if(error == nil){
for object in objects!{
//placed break point on line below, program does not stop on breakpoint.
let arr = object["Subject"] as? [String]
self.subject = arr!
print("subjects\(self.subject)")
}
}else{
print(error)
}
})
In your parse dashboard your column is named Name, while you are using name in query.whereKey("name", equalTo: self.name.text!). Capitalize the key and you should be good.

How to delete an object from Parse?

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 😊

How to access other attributes/columns from a parse.com object after pulling the object from parse (swift)

I am pulling code from parse using the query below. I retrieve the objects but I can only access "object.objectId" if I try "object.name" and "name" is a column on the Funlists table my app crashes and I get this error: "Thread 1:EXC_BAD_INSTRUCTION(code=EXC_1286_INVOP, subcode=0x0)"
var query = PFQuery(className: "FunLists")
query.whereKey("createdBy", equalTo:"Sean Plott")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// The find succeeded.
NSLog("Successfully retrieved \(objects.count) scores.")
// Do something with the found objects
for object in objects {
NSLog("%#", object.objectId)
self.funlists.append(object.name)
}
} else {
// Log details of the failure
NSLog("Error: %# %#", error, error.userInfo!)
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
You probably have to access to the entity properties using dictionary-like syntax, like:
object["name"]
I presume that the error happens here:
self.funlists.append(object.name)
I would change that to:
self.funlists.append(object["name"])
or
self.funlists.append(object["name"] as String)

Resources