As far as I could find out, there is no easy random query function in parse. Is there a way to shuffle the objects after I queried them?
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if (error == nil) {
// shuffle objects here?
PFObject.pinAllInBackground(objects, withName:"Card", block: nil)
Any ideas?
Thanks!
EDIT:
I think the problem is at another point.
When I add like in the suggest post (thanks for that) the extension for shuffling, it works like a breeze!
BUT!
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if (error == nil) {
println(objects)
var shuffled = objects.shuffled()
println(shuffled)
PFObject.pinAllInBackground(shuffled, withName:"Card", block: nil)
As you can see, I pin the objects to the local storage.
On my next view I pull this data again out of the storage:
var query = PFQuery(className: "Card")
query.fromLocalDatastore()
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if (error == nil){
println("from local store: \(objects)")
And here I have again the data in the same order as they are saved in the database... There are no shuffled items at all.
Isn't that strange?
Just take the objects and use a shuffle-method like provided in this answer on the objects:
var shuffledObjects = shuffle(objects)
Related
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
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 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 using parse in my app and I want to satisfy the two query and return object without using orQueryWithSubqueries. Here my query to parse code:
func queryToParse(){
var queryForBlood = PFQuery(className: "Donors")
queryForBlood.whereKey("BloodGroup", equalTo: bloodGroupTextField.text)
var queryForCity = PFQuery(className: "Donors")
queryForCity.whereKey("City", equalTo: citySearchTextField.text)
var query = PFQuery.orQueryWithSubqueries([queryForCity,])
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
self.tableData = objects as NSArray
println(self.tableData)
self.tableView.reloadData()
}
else
{
println(error)
}
}
}
Instead of creating two separate PFQuery, then you just have to create one. You only need to create several PFQuery when you want to make an OR query.
Your code should look something like this:
func queryToParse(){
let query = PFQuery(className: "Donors").whereKey("BloodGroup", equalTo: bloodGroupTextField.text).whereKey("City", equalTo: citySearchTextField.text)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
...
}
}
When I run this query:
var query = PFQuery(className: "CardSet")
query.whereKey("user", equalTo: PFUser.currentUser())
query.includeKey("lesson")
query.orderByAscending("createdAt")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in ........
The results are never sorted by data, in fact they are always sorted by objectId.
What is wrong in my query?
This is my data on parse, sorted by date
This is my output on the device, sorted by objectId...
Had the same issue. Try adding this to your above code in block, it worked for me:
let array:NSArray = self.queryobjectsArray.reverseObjectEnumerator().allObjects
self.queryobjectsArray = NSMutableArray(array: array)
This whole issue ended quite strange...
What I did before, like in other views within the same project, I fetched the data from parse and pinned it to the local storage and called a second method to fetch the data from the local storage. This way I have fast reachable data from local storage and update it in the background for later use.
For some reason, the fetched data from online was sorted by date.
But the fetched data from the local pin was not and that was the reason, why my app displays the data in the wrong timeline.
I was not able to understand or solve this, but I helped myself by removing the local pinning and just grab the data from the internet.
I will show what the original process was:
var cardSetObjects: NSMutableArray! = NSMutableArray()
override func viewDidAppear(animated: Bool) {
self.fetchAllObjectsFromLocalDatastore()
self.fetchAllObjects()
}
func fetchAllObjectsFromLocalDatastore(){
var query = PFQuery(className: "CardSet")
query.fromLocalDatastore()
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if (error == nil){
var temp: NSArray = objects as NSArray
self.cardSetObjects = temp.mutableCopy() as NSMutableArray
self.tableView.reloadData()
}else{
println(error.userInfo)
}
}
}
func fetchAllObjects(){
var query = PFQuery(className: "CardSet")
query.whereKey("user", equalTo: PFUser.currentUser())
query.includeKey("lesson")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if (error == nil) {
PFObject.pinAllInBackground(objects, withName:"CardSet", block: nil)
self.fetchAllObjectsFromLocalDatastore()
self.tableView.reloadData()
}else{
println(error.userInfo)
}
}
}
What I did at the end was to remove the fetchAllObjectsFromLocalDatastore method and put the data into the NSMutableArray right in fetchAllObjects.
But I still wonder why I had this sorting issue with this code...
Maybe someone will know...