I have the following PFObject saved locally:
<Local: 0x6180000a4080, objectId: SAMPLEID, localId: (null)> {
ACL = "<PFACL: 0x60800002f0a0>";
image = "<PFFile: 0x60800005e7b0>";
name = Anonymous;
}
I'm trying to run the following code to retrieve the image:
let query = PFQuery(className: "Local")
query.fromLocalDatastore()
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
if let objects: [PFObject] = objects {
for object in objects {
let imageFile = object["image"] as! PFFile
imageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = NSImage(data:imageData)
}
}
}
}
}
}
Parse is giving me the following error:
Can't download a file that doesn't exist on the server or locally. (Code: 151, Version: 1.11.0)
I had the same issue trying to load a file I had loaded dozens of times before. I hadn't change the file, but I did save the same image multiple times. It seemed like there was a glitch with parse saving for some reason. I changed nothing, but just re-saved the same image once again and it worked fine.
I found the same workaround. Just pinInBackground and saveInBackground in the next line and it'll work... It'll cost you one request though which I was trying to avoid.
Related
I have to query for a class called "Commitment" it has a pointer called "need". I need to be able to access the data in the pointer of need while querying Commitment. I have to query for Commitment because I have to check the "committer" object against the current user. Essentially I want to be able to see the needs that the current user has committed to.
Commitment Class:
Need Class:
let query = PFQuery(className: "Commitment")
query.order(byDescending: "createdAt")
query.whereKey("committer", equalTo: PFUser.current()!)
query.includeKey("need")
query.findObjectsInBackground {
(objects: [PFObject]?, error: Error?) -> Void in
if let objects = objects as [PFObject]? {
self.committment = []
self.commitObject = []
for object in objects {
self.committment.append(Committment(id: object.objectId!, needName: object["needName"] as! String))
self.commitObject.append(object)
}
self.tableView.reloadData()
} else {
print("failure")
}
}
This is my current query. It returns nil on needName so obviously that isn't working. Any help is appreciated.
The reason that you get nil is because you are not accessing the need object but you are trying to get it from the Commitment object
just change your code to something like this:
let query = PFQuery(className: "Commitment")
query.order(byDescending: "createdAt")
query.whereKey("committer", equalTo: PFUser.current()!)
query.includeKey("need")
query.findObjectsInBackground {
(objects: [PFObject]?, error: Error?) -> Void in
if let objects = objects as [PFObject]? {
self.committment = []
self.commitObject = []
for object in objects {
let need = object["need"] as [PFObject]?
self.committment.append(Committment(id: object.objectId!, needName: need["needName"] as! String))
self.commitObject.append(object)
}
self.tableView.reloadData()
} else {
print("failure")
}
}
Notice that i first access the need object and from there extract the needName property.
BTW! i am not sure that the syntax is 100% accurate (i wrote it in freestyle) but i am sure that you got the idea..
I am trying to download some data from parse but I get an error message saying "Value of type 'PFObject' has no member 'name' What am I doing wrong?
here is my parse dashboard screenshot
here is my code to upload the data to parse:
var coordinates = PFGeoPoint (latitude: (newCoordinate2.latitude), longitude:(newCoordinate2.longitude))
var aboutSpot = PFObject(className: "spotdetail")
aboutSpot ["PFGeoPoint"] = coordinates
aboutSpot["name"] = "name"
aboutSpot.saveInBackgroundWithBlock { (succes, error) -> Void in
print("separate name and geopoint have been saved")
}
and here is my code to download my data:
var query = PFObject.query()
query!.findObjectsInBackgroundWithBlock ({ (objects, error) in
if let places1 = objects {
for object in places1 {
if let spotdetail = object as? PFObject {
self.rideSpots.append(spotdetail.name!)
}
}
}
print(self.rideSpots)
})
also not that on the line that says
if let spotdetail = object as? PFObject {
I get a warning saying "conditional cast from 'PFObject' to 'PFObject' always succeeds
I can probably solve this pretty easily but I wanted to mention it in case it could help solve the issue
I am attempting a query from Parse to get the object ids in an array and not have to hard code the ids. I attempt to use the following code:
var ObjectIDQuery = PFQuery(className: "QuestionsandAnswers")
ObjectIDQuery.findObjectsInBackgroundWithBlock({
(objectsArray : [AnyObject]?, error : NSError?) -> Void in
var ObjectIDs = objectsArray as! [PFObject]
for i in 0..<ObjectIDs.count{
self.ObjectIDsPublicArray.append(ObjectIDs[i].objectId)
}
})
But the code causes Xcode to state "Xcode encountered a problem. Source editor functionality is limited.Attempting to restore"
Anyone know why that code would cause that? Also any suggestions to fix?
you are on the right path but Parse doesn't use [AnyObject]? anymore in their new SDK so change to [PFObject]?
Example:
let objectIdQuery = PFQuery(className: "QuestionsandAnswers")
objectIdQuery.findObjectsInBackgroundWithBlock({
(objectsArray : [PFObject]?, error : NSError?) -> Void in
if error == nil
{
if let objects = objectsArray
{
for one in objects
{
let objectID = one.objectID //<--- objectID
// then append the objectID into your data structure
}
}
}
})
This is a common problem, basically parse updated their SDK
Just change
[AnyObject]? to [PFObject]?
Same problem here I think
PFArrayResultBlock(parse) is causing an error while converting to swift 2.0
Im having trouble with Swift and Parse.com array because the documentation does not say much about the Syntax and im pretty new to Swift.
So what i want to do:
I want to add two values from two CGFloats to my Database.
Later i want to pull this two Values out from Parse Database and write them into a local CG Float array. But im Having a lot of trouble
Here is my Code for uploading the Value to the Parse.com Array: (This works fine, i have the three values in my Pase Database)
var positionAx : CGFloat = -1000
var positionAy : CGFloat = -1000
var post = PFObject(className: "Post")
post["antwortA_koord"] = [positionAx, positionAy, 0]
post.saveInBackgroundWithBlock{(success: Bool!, error: NSError!) -> Void in
if success == false {
self.displayAlert("Could not post your Question", error: "Please check your internet connection")
}
else {
println("You have posted")
}
And here the Code for Getting the data from Parse.com
var buttonA = [CGFloat] ()
var query = PFQuery(className:"Post")
query.whereKey("user", notEqualTo: PFUser.currentUser().username)
query.limit = 1
query.findObjectsInBackgroundWithBlock{
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
for object in objects {
self.buttonA.append(object["antwortA_koord"] as CGFloat)
println(self.buttonA)
}
I get an error in this line: self.buttonA.append(object["antwortA_koord"] as CGFloat) but i dont know how to write the Syntax in the right way.
I hope you can help me
All I am trying to get multiple objects out of a parse database.
Here is some of my code :
So this does the query :
var MainPicture = PFQuery(className: "Staff")
MainPicture.whereKey("Position", equalTo: "Sales Manager")
MainPicture.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
if(error == nil){
self.getMainImageData(objects as [PFObject])
}
else{
println("Error in retrieving \(error)")
}
Then I want to get a few rows out of the query :
func getMainImageData(objects: [PFObject]) {
for object in objects {
let MainPic = object["StaffPic"] as PFFile
let MainData = object["FirstName","SecondName","Position"] as PFFile
MainPic let works, but when I try and do multiple ones like MainData , I get an error : "Extra argument in call" .. I thought this would have worked.
I suspect you cannot subscript PFObject with multiple items. It is like calling
dictionary["key1", "key2"]
That will also result in too many arguments.
It is confusing that your variables are Capitalized. They look like class names.