Parse save pointer - ios

This is my code that tries to save the table/Class inscricoes
view.showHUD(view)
var inscricaoClass = PFObject(className: INSCRICAO_CLASS_NAME)
inscricaoClass[INSCRICAO_SORTEIO_ID] = self.eventObj.objectId
inscricaoClass.saveInBackgroundWithBlock { (success, error) -> Void in
if error == nil {
self.view.hideHUD()
} else { errorAlert.show(); self.view.hideHUD() }
}
This is my class / table where sorteioId is a pointer to my table/class sorteios
when I try to save an error of warning that can not save as a string pointer.
[Error]: invalid type for key sorteioId, expected Sorteios, but got string (Code: 111, Version: 1.7.5)
How do I send a pointer to table / class using parse?

You first need an instance of PFObject of type Sorteios. Revise your code like so:
view.showHUD(view)
var query = PFQuery(className: "Sorteios")
query.getObjectInBackgroundWithId(self.eventObj.objectId) {
(object: PFObject?, error: NSError?) -> Void in
if error == nil && object != nil {
// after finding Sorteios, you can assign it to inscricaoClass
var inscricaoClass = PFObject(className: INSCRICAO_CLASS_NAME)
inscricaoClass[INSCRICAO_SORTEIO_ID] = object
inscricaoClass.saveInBackgroundWithBlock { (success, error) -> Void in
if error == nil {
self.view.hideHUD()
} else {
errorAlert.show(); self.view.hideHUD() }
}
}
}

Related

Is it possible to query dictionary on parse.com?

i’m trying to search dictionary match result but no luck
my parse dictionary column look like this
columnName: Tag property: object
{"firstKey”:”David”,”secondKey”:”Guetta”}
columnName: name property: string
cool

when I try to search name column
here is my code snippet,
static func parseQueryDictionary() {
let query = PFQuery(className:"TestDictionary")
query.whereKey("name", equalTo: "cool")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil && objects != nil {
print("objects is", objects)
} else {
print(error)
}
}
}
i get result below
objects is Optional([ {
Tag = {
firstKey = David;
secondKey = Guetta;
};
name = cool;
tagArray = (
David,
Guetta
);
}])
i've try array column
columnName: tagArray property: array
["David","Guetta"]
static func parseQueryDictionary() {
let query = PFQuery(className:"TestDictionary")
query.whereKey("tagArray", equalTo:"David")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil && objects != nil {
print("objects is", objects)
} else {
print(error)
}
}
}
i get result
objects is Optional([ {
Tag = {
firstKey = David;
secondKey = Guetta;
};
name = cool;
tagArray = (
David,
Guetta
);
}])
but when i try to search dictionary column
columnName: Tag property: object
{"firstKey”:”David”,”secondKey”:”Guetta”}
like this
static func parseQueryDictionary() {
let query = PFQuery(className:"TestDictionary")
query.whereKey("Tag", equalTo:"David")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil && objects != nil {
print("objects is", objects)
} else {
print(error)
}
}
}
i get no result
objects is Optional([])

i’ve try google and parse official doc but can’t find this case, is it possible to do that?
i've try search string column, array column it's work but only dictionary column not work...
search in google with "findObjectsInBackgroundWithBlock" in swift.It will give you many results.
var query = PFQuery(className: parseClassName)
query.whereKey("Position", equalTo: "iOS Developer")//Here Position is column name and Sales Manager is value.
query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
if(error == nil){
for object in objects {
}
}
else{
println("Error in retrieving \(error)")
}
})

Check if PFObject is nil

I want to check if a PFObject is nil
I retrieve the object
var userLibrary: PFObject?
func getUserLibrary(user: PFUser) {
self.libraryQuery = PFQuery(className: "Library")
self.libraryQuery?.whereKey("owner", equalTo: user)
self.libraryQuery?.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if objects!.count > 0 {
self.userLibrary = objects![0]
} else {
print(self.userLibrary)
}
}
})
}
The last line with the print statement prints out nil.
However when I check :
if userLibrary != nil {
}
Xcode tells me
Binary operator '!=' cannot be applied to operands of type 'PFObject' and 'NilLiteralConvertible'
How do I fix this ?
I'm not 100% sure that this will work, but did you try.
if let lib = userLibrary {
//do whatever
}
Let me know.
Also, if you are just using the first object of your query. It would better to use getFirstObjectInBackground

Retrieving user data from Parse "_User" class using "ObjectId"

i am trying to retrive a user's data to get the user info from the _User class using the object id. i used this :
var data:NSMutableArray = NSMutableArray()
func loadData() {
data.removeAllObjects()
var profileQuery:PFQuery = PFUser.query()!
profileQuery.getObjectInBackgroundWithId(userId, block: { (objects, error) -> Void in
if error == nil {
self.data.addObject(objects!)
}
})
println(userId) // this is the userId as String.
println(self.data) ********* // empty array.
}
i am getting an empty array data here.. I've tried this also but same thing's happening here too. :
var profileQuery:PFQuery = PFUser.query()!
profileQuery.whereKey("objectId", equalTo: userId)
profileQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
self.data.addObject(object)
}
}
}
Remember that "findObjectsInBackgroundWithBlock" happens async! You need to put any logic pertaining to the data inside of the block.
var profileQuery:PFQuery = PFUser.query()!
profileQuery.getObjectInBackgroundWithId(userId, block: { (objects, error) -> Void in
if error == nil {
self.data.addObject(objects!)
println(self.data) //shouldn't be empty.
}
})
Putting aside why you're using an NSMutableArray for a singular PFUser object—you're basically expecting to see self.data populated in the wrong location. The user would be added to it (assuming a successful retrieval), inside the closure. So do something like:
var data:NSMutableArray = NSMutableArray()
func loadData() {
data.removeAllObjects()
var profileQuery:PFQuery = PFUser.query()!
profileQuery.getObjectInBackgroundWithId(userId, block: { (objects, error) -> Void in
if error == nil {
self.data.addObject(objects!)
println(self.data) //...shouldn't be empty here
} else {
println("Error retrieving user: \(error.description")
}
})
}

Arithmetic operation fails on AnyObject

I'm doing an app in iOS Swift with Parse. I need to increment 1 every time a product is viewed. Following is my code to do that:
var prodQuery = PFQuery(className: "prodDet")
let id = prodId[currprod] as String
prodQuery.whereKey("objectId", equalTo:id)
prodQuery.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
for object in objects {
var key1: Int = object["key1Total"]! as Int
var key1New: Int = key1 + 1
object["key1Total"] = key1New
}
}
}
The above code gives error "#lvalue$T5 is not identical to AnyObject...". Is anything wrong the way I'm dealing with AnyObject object here; How do we do arithmatic calculation on Parse AnyObjects?
Could somebody please shed some light?
I sorted it out, seems like I should cast the object as PFObject and use object.setValue. The following is the working code:
var prodQuery = PFQuery(className: "prodDet")
let id = prodId[currprod] as String
prodQuery.whereKey("objectId", equalTo:id)
prodQuery.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
for object in objects as [PFObject] {
var keyVal = object["key1Total"]! as Int
var keyNew = keyVal + 1
object.setValue(keyNew, forKey: "key1Total")
object.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
//
} else {
//
}
}
}
}
}
object.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
//
} else {
//
}
}
}
}
}

Retrieve values out of PFObject

I have a save button to save Score and Playername to GameScore in Parse. When I load the GameScore from Parse I want to set the value that i loaded to the variable "score". This don't work, can anyone please tell me what i am doing wrong?
Thanks
Exapmle: let score = gameScore["score"] as Int
// Load button tapped
#IBAction func loadButtonTapped(sender: UIButton) {
var query = PFQuery(className:"GameScore")
query.getObjectInBackgroundWithId("F1efANYzOE") {
(gameScore: PFObject?, error: NSError?) -> Void in
if error == nil && gameScore != nil {
println(gameScore)
let score = gameScore["score"] as Int
} else {
println(error)
}
}
}
}
Try this following code for retrieving specific data from specific columns. You have to enter your object name, Object ID and column name in the below code and run it. It will work.
let query = PFQuery(className:"Your Object name")
query.getObjectInBackgroundWithId("Your Object ID") {
(gameScore: PFObject?, error: NSError?) -> Void in
if error == nil {
print(gameScore!.objectForKey("your column name") as! String)
} else {
print(error)
}
}

Resources