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)")
}
}
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 have an app where I'm trying to load a User and all his related data. In the relations I have BodyTracking as one relation. Another is DiaryWeek, which in turn has DiaryDays (of the week).
I successfully pulled all the BodyTracking, but when I try the same for the DiaryWeeks, I get an error saying that I have to wait. I guess what is happening is that the BodyTracking, which is requested with 'findObjectsInBackgroundWithBlock' is still going on when the DiaryWeek request is called.
Is there a way to pull all the data in one go?
// Body Tracking
bodyEntries.removeAll()
let trackingQuery = PFQuery(className: "BodyTracking")
trackingQuery.whereKey("user", equalTo: member)
trackingQuery.orderByDescending("trackingDate")
trackingQuery.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
print("Successfully retrieved \(objects!.count) body entries.")
if let objects = objects! as? [PFObject] {
for object in objects {
let bodyEntry = BodyTracking(object: object)
self.bodyEntries.append(bodyEntry)
}
self.bodyTrackingTableView.reloadData()
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
// Food Diary
foodWeeks.removeAll()
let diaryQuery = PFQuery(className: "FoodDiaryWeek")
diaryQuery.whereKey("user", equalTo: member)
diaryQuery.orderByDescending("weekStartDate")
trackingQuery.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
print("Successfully retrieved \(objects!.count) food weeks")
if let objects = objects! as? [PFObject] {
for object in objects {
let foodWeek = FoodDiaryWeek(object: object)
self.foodWeeks.append(foodWeek)
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
Error message: This query has an outstanding network connection. You have to wait until it's done.
It looks like you are accidentally executing the trackingQuery twice. The diaryQuery is never executed. Maybe a copy/paste error (?)
// Food Diary
foodWeeks.removeAll()
let diaryQuery = PFQuery(className: "FoodDiaryWeek")
diaryQuery.whereKey("user", equalTo: member)
diaryQuery.orderByDescending("weekStartDate")
**trackingQuery**.findObjectsInBackgroundWithBlock {
replace trackingQuery by diaryQuery
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 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 😊
I am trying to get an array of PFObjects, [PFObject], from Parse and seeing the issue below. What am I missing?
Error is Missing argument for parameter #1 in call
func loadData() {
rooms = [PFObject]()
users = [PFUser] ()
self.tableView.reloadData()
let pred = NSPredicate(format: "user1 = %# OR user2 = %#", PFUser.currentUser()!, PFUser.currentUser()!)
let roomQuery = PFQuery(className: "Rooms", predicate: pred)
//gives us all the information - includeKey all columns for the user class itself
roomQuery.includeKey("user1")
roomQuery.includeKey("user2")
roomQuery.findObjectsInBackgroundWithTarget{ (results: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
self.rooms = results as [PFObject]
for room in self.rooms {
let user1 = room.objectForKey("user1") as PFUser
let user2 = room.objectForKey("user2") as PFUser
if user1.objectId != PFUser.currentUser() {
self.users.append(user1)
}
if user2.objectId != PFUser.currentUser() {
self.users.append(user2)
}
}
self.tableView.reloadData()
}
}
}
Error:
It seems that you want to handle the response with a block but you decided to use findObjectsInBackgroundWithTarget. You should go with findObjectsInBackgroundWithBlock:. So you should be having something like this:
roomQuery.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in