Why is the for loop being skipped over? - ios

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.

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 to delete object from Parse via PFQuery

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

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

Delete received Object from Parse Backend service with Swift, xcode

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

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 😊

Resources