swift - retrieve objectId field causes fatal error - ios

Here is my code:
#IBAction func saveSettings(sender: AnyObject) {
var settings:PFObject = PFObject(className: "Settings")
settings["routes"] = routesSetting as String
settings["sortBy"] = sortBySetting as String
settings["user"] = PFUser.currentUser()
settings.saveInBackgroundWithBlock{(success:Bool!, error:NSError!) ->Void in
if success != nil {
NSLog("%#","OK-settings data saved")
NSLog("%#",self.routesSetting as String)
}
else
{
NSLog("%#",error)
}
}
}
#IBAction func updateSettings(sender: AnyObject) {
var settings:PFObject = PFObject(className:"Settings")
var id = settings["objectId"] as String
var query = PFQuery(className:"Settings")
println(settings)
println(id)
println(query)
query.getObjectInBackgroundWithId(id) {
(settings: PFObject!, error: NSError!) -> Void in
if error != nil {
NSLog("%#", error)
} else {
settings["routes"] = self.routesSetting as String
settings["sortBy"] = self.sortBySetting as String
settings.save()
}
}
I can run the saveSettings code with no issues. I have set it up to run when a new user signs up with the application. I have verified that a new row is inserted into the settings table in the Parse cloud DB. But I would now like to simply UPDATE the single row instead of create a new row every time there is a save. So I have been working on the updateSettings function. I have followed the Parse documentation for updating objects (https://parse.com/docs/ios_guide#objects-updating/iOS). When I click on the Update button to run the function, I get:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
The line it points to is:
var id = settings["objectId"] as String
I am a complete beginner to Parse.
Some recent additional information:
The results of the println statements:
<Settings: 0x7fd423805260,
objectId: new, localId: (null)> {
}
nil
<PFQuery: 0x7fd4217191f0>
So part of the problem is it is not retrieving the objectId?

PFObject has a property called "objectId", it is not stored in a dictionary, so you can return it as
var id = settings.objectId

Related

Iterate through a custom Parse column in Swift app

I want to know how I could store the entire custom column (the user Pointer<_User> column from a custom class) and put them all in an array variable so that I can see if a the user exists in that class or not. This is what I have:
Old Code
var objectUserIdArray = [String]()
let objectUserIdQuery : PFQuery = PFQuery(className: "Scores")
objectUserIdQuery.findObjectsInBackgroundWithBlock {
(objects : [PFObject]? , error : NSError?) -> Void in
var objectID = objects! as [PFObject]
for i in 0..<objectID.count {
objectUserIdArray.append(objectID[i].objectId!)
}
for _ in objectID {
print(objectUserIdArray)
}
New Code
func saveScoresOnParse() {
objectUserIdQuery.whereKey("User", equalTo: PFObject(withoutDataWithClassName: "_User", objectId: userID))
objectUserIdQuery.findObjectsInBackgroundWithBlock {
(objects : [PFObject]? , error : NSError?) -> Void in
if error == nil {
//var objectID = objects! as [PFObject]
/*for i in 0..<objectID.count {
self.objectUserIdArray.append( objectID[i].objectId! )
}*/
for _ in objects! {
print(objects)
}
// The score key has been incremented
for (var i = 0 ; i < self.objectUserIdArray.count ; i++) {
if self.userID != objects![i] {
print("New Scores")
print("R: \(self.rightAnswers)")
print("W: \(self.wrongAnswers)")
print("S: \(self.skippedQuestions)")
self.scores["User"] = PFUser.currentUser()
self.scores["Right"] = self.rightAnswers
self.scores["Wrong"] = self.wrongAnswers
self.scores["Skipped"] = self.skippedQuestions
self.scores.saveInBackground()
} else if self.userID == objects![i] {
print("Updated Scores")
self.scores.incrementKey("Right", byAmount: 1)
self.scores.incrementKey("Wrong", byAmount: 1)
self.scores.incrementKey("Skipped", byAmount: 1)
print("R: \(self.rightAnswers)")
print("W: \(self.wrongAnswers)")
print("S: \(self.skippedQuestions)")
self.scores.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
// The score key has been incremented
} else {
// There was a problem, check error.description
}
}
} else {
print("Error")
}
}
} else {
print(error)
}
}
But it only stores the objectId column and not the Pointer<_User> column. I know this because when I print the stuff that is inside, it prints out the objectIds.
This is what happens, instead of just updating the current user's scores, it just makes new ones. I want the if statement to check if the user already exists in that column and if it does updates the scores and if it doesn't, make new ones. (The new code's if statement doesn't work, i have to bring it out for it to save...)
Your updated question make clearer what you are actually wanting to do;
Save or update a user's scores in your Parse Score object. To do this, there is no reason to retrieve any object Ids or loop through any results. More often than not you don't do use Object Ids explicitly when using Parse; you can simply pass the object itself with Parse working out the references for you.
I am not sure how you exactly want to change the scores; in your code above you increment in one case but set the scores explicitly in another, but the code below shows the general approach.
If you are frequently or repeatedly going to update a score record then you could make your code more efficient by holding a reference to the Scores object in a property after you find it the first time and simply update & save it subsequently.
func saveScoresOnParse() {
if let currentUser=PFUser.currentUser() {
let scoreQuery= PFQuery(className: "Scores")
scoreQuery.whereKey("User",equalTo:currentUser)
scoreQuery.getFirstObjectInBackgroundWithBlock {
(object : PFObject? , error : NSError?) -> Void in
if error == nil {
var scoreObject=object ?? PFObject.objectWithClassName("Scores")
if (scoreObject["User"]==nil) {
scoreObject["User"]=currentUser
}
scoreObject["Right"]=self.rightAnswers
scoreObject.saveInBackground()
} else {
print(error)
}
}
} else {
print("No current user!")
}
}

Parse queries do not show most recent data

import UIKit
import Parse
class loadMainViewController: UIViewController {
//create new pfQuery - This is the bridge between our app and Parse: "trivia" is our class name on Parse
let queryTrivia: PFQuery = PFQuery(className:"trivia")
#IBOutlet weak var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//retrieve data from parse query
retrieveTrivia()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func retrieveTrivia() {
//This CLOSURE gives access to all objects in "trivia" class using our queryTrivia Bridge
queryTrivia.findObjectsInBackgroundWithBlock { (objects:[PFObject]?, error:NSError?) -> Void in
if( error == nil ){
print("error is nil")
}
var didLoad = true
// Loop through the objects array
for triviaObject in objects!{
// Retrieve data for each object (key, question, ans2, ans3, correctAns)
let triviaQuest : String? = (triviaObject as PFObject)["question"] as? String
let triviaAns2 : String? = (triviaObject as PFObject)["ans2"] as? String
let triviaAns3 : String? = (triviaObject as PFObject)["ans3"] as? String
let triviaAns : String? = (triviaObject as PFObject)["correctAns"] as? String
let triviaKey : Int? = (triviaObject as PFObject)["key"] as? Int
//Check that items are not nil, and create trivia object, add to triviaQuestions Array
if ( triviaKey != nil && triviaQuest != nil && triviaAns2 != nil && triviaAns3 != nil && triviaAns != nil){
let trivia = triviaQuestion(Key: triviaKey!, Question: triviaQuest!, Answer: triviaAns!, WrongAnswer: triviaAns2!, WrongAnswer2: triviaAns3!)
triviaQuestions.append(trivia) // append to the global array of trivia questions
}else{
self.label1.text = "Network Error"
didLoad = false
}
}
for element in triviaQuestions{
print(element.Key)
}
if (didLoad == true) {
//perform segue to View Controller : Main menu
self.performSegueWithIdentifier("finnishLoad", sender: self)
}
}
}
}
My problem Lies when I query the data in the retrieveTrivia() method. The queries apeare to work with a few Problems. The data being saved in to the TriviaQuestion, TriviaAns, TriviaAns2, TriviaAns3 is old data that I have since changed using The parse website by simply clicking in the cell and editing each feild by hand. Furthermore I cannot seem to get the key field and it is always coming back as nil.
When I run this the triviaAns variable contains "Cow" but, online when looking at the data it says "Mozzarella."
Any idea as to why I would be receiving the wrong data after updating the fields and why the key field is coming back as nil?
Here is what the data looks like now
enter image description here
Make sure that you are connecting to the correct Parse app - Check the keys in the call to setApplicationId:clientKey in your App Delegate match those shown in your app's settings on Parse.com

Passing objectId from viewDidLoad to another function using Parse method getObjectInBackgroundWithId not working

I'm a beginner working with Parse and Swift. I need to update the object referred to in my viewDidLoad in another function within the same controller. How do I pass the currently loaded object's objectId without having to hardcode it like this:
query.getObjectInBackgroundWithId("8DkYgraEJq")
Here is my viewDidLoad function:
override func viewDidLoad() {
var query = PFQuery(className: "CheckedBaggage")
query.orderByAscending("createdAt")
query.whereKey("respondedTo", notEqualTo: true)
query.getFirstObjectInBackgroundWithBlock {
(CheckedBaggage: PFObject!, error: NSError!) -> Void in
if error != nil {
println("The getFirstObject request failed.")
} else {
// The find succeeded.
self.randomBaggageLabel.text = CheckedBaggage.objectForKey("message") as? NSString
CheckedBaggage.save()
println(CheckedBaggage.objectId)
let baggageId = CheckedBaggage.objectId
println("Successfully retrieved the object.")
}
}
I would like to try and pass the variable baggageId, which should be the object's ID as a string, as an argument to the getObjectInBackgroundWithId block in my carryIt function:
#IBAction func carryIt(sender: AnyObject!) {
println("CarryIt is being called")
var query = PFQuery(className: "CheckedBaggage")
query.getObjectInBackgroundWithId(baggageId) {
(CheckedBaggage: PFObject?, error: NSError?) -> Void in
if error != nil {
println(error)
} else if let CheckedBaggage = CheckedBaggage {
println("object hello!")
CheckedBaggage["respondedTo"] = true
CheckedBaggage["response"] = self.kindnessMessage.text
CheckedBaggage.save()
}
}
}
But I'm getting an "unresolved identifier" error. It updates my Parse database perfectly fine if I hardcode the object ID, but I can't do it this way. Here's a screenshot of the error:
Thank you so much for your help!
You have to initialize baggageId. To use it in multiple functions, it must be scoped at class level as the comment said. To set it after it has been declared, it must be a "var", not a constant "let".
var baggageId = ""
func viewDidload() {
var query = ...
query.get... {
baggageId = CheckedBaggege.objectId
}
}
func shipIt() {
var query = ...
query.getObjectWithId(baggageId) ...
}

Save current user's message list in parse with swift

I use parse for my app. I want to let user able to type messages that they want to send via textField and save it to that user's messages column in parse with PFRelation via save button in view controller and the messages will be saved as an array and show it in tableView.
The problem is I don't know how to add text in textfield to an array and save it to parse.
Any help is appreciated and let me know if you need any additional information!
UPDATE:
These are screenshots of my parse's class "User"
This is my current user's friend list inside "Friends" column
I've not yet create Messages column because when run relationForKey code in Xcode it will automatically create for me
UPDATE 2:
This is my code:
#IBAction func addMessage(sender: AnyObject) {
var newMessage = addMessageText.text
let message = PFObject(className: "Messages")
var query = PFQuery(className: "Messages")
message["messageTextColumn"] = newMessage
message.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
println("added to Message Class")
query.whereKey("messageTextColumn", equalTo: newMessage)
query.getFirstObjectInBackgroundWithBlock{(object:PFObject!, error: NSError!) -> Void in
if error == nil {
let relation = PFUser.currentUser().relationForKey("Messages")
var addMessageWithObject = object
if addMessageWithObject != nil {
relation.addObject(addMessageWithObject)
println("Added with getFirstObject")
}
else{
println("Error Added with getFirstObject")
}
}
}
} else {
println("added to Message class Error")
}
}
}
I save new message to the array first and then I save it with saveInBackgroundWithBlock.. and inside I query that message to add it to relation.
The messages that I've added appear on Messages class table but not in that user's relation but it shows log
"added to Message Class" and "Added with getFirstObject"
Which means that my code execute exactly like it should be. Probably about the method?
UPDATE 3 this is the object println
<Messages: 0x7fd4484f75f0, objectId: LFXoSaHfQl, localId: (null)> {
ACL = "<PFACL: 0x7fd4484d2e70>";
messageTextColumn = 9;
}
UPDATE 4
this is my code
#IBAction func addMessage(sender: AnyObject) {
var newMessage = addMessageText.text
let message = PFObject(className: "Messages")
var user = PFUser.currentUser()
var query = PFQuery(className: "Messages")
message["messageTextColumn"] = newMessage
message.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
println("added to Message Class")
query.getFirstObjectInBackgroundWithBlock{(object:PFObject!, error: NSError!) -> Void in
if error == nil {
var addMessageWithObject = object
if addMessageWithObject != nil {
user.saveInBackground()
println("Added with getFirstObject")
}
else{
println("Error Added with getFirstObject")
}
}
}
}
}
}
user column is (undefined) as in screenshot here
and the error log can't add non pointer to relation is back
how do I fix this? Thanks!
Here's what you do:
Manually create your Message table on Parse
Add a messages column to your user table of type Relation with Target Class as your Message table.
In your code, in your buttons trigger:
// Get the message text from your textField
let messageText = textField.text
// Create your new Message object
let newMessage = PFObject(className: "Message")
// ... Add your data to your new message object
newMessage["messageTextColumn"] = messageText
newMessage.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
// Add the new message to the user's relation
let relation = yourUser.relationForKey("messagesColumnName")
relation.addObject(newMessage)
// Save the user object
yourUser.saveInBackground()
} else {
// There was a problem, check error.description
}
}
Here's a link to Parse's Relation reference.
UPDATE:
Missing code to save your user object.
query.getFirstObjectInBackgroundWithBlock{(object:PFObject!, error: NSError!) -> Void in
if error == nil {
let relation = PFUser.currentUser().relationForKey("Messages")
var addMessageWithObject = object
if addMessageWithObject != nil {
relation.addObject(addMessageWithObject)
PFUser.currentUser().saveInBackground()
println("Added with getFirstObject")
}
else{
println("Error Added with getFirstObject")
}
}
}
UPDATE 2:
Messages without PFRelation:
Add a column (let's say user) of type Pointer with Target Class as _User to the Messages table to identify each message by their user.
Saving new messages: Save the new message object like above (just without adding the relation and it'e related code):
#IBAction func addMessage(sender: AnyObject) {
var newMessage = addMessageText.text
let message = PFObject(className: "Messages")
message["messageTextColumn"] = newMessage
message["user"] = PFUser.currentUser()
message.saveInBackgroundWithBlock {(success: Bool, error: NSError?) -> Void in
if (success) {
println("added to Message Class")
} else {
// Error saving message
}
}
}
Querying the messages for a user: You can query using the current user as a constraint so no matter which device a particular switches to, he/she will get only his messages.
var query = PFQuery(className:"Messages")
query.whereKey("user", equalTo:PFUser.currentUser())
query.findObjectsInBackgroundWithBlock {
...
}

Save data from parse into a string

I want to save data from my parse class into strings. I use the following code actually for retrieving data from my parse class.
#IBAction func readAction(sender: UIButton) {
var tagAutor = ""
var tagText = ""
var query = PFQuery(className:"Tags")
query.getObjectInBackgroundWithId("f3AXazT9JO") {
(tag: PFObject?, error: NSError?) -> Void in
if error == nil && tag != nil {
println(tag)
// tagAutor = tag["username"]
// tagText = tag["tagtext"]
} else {
println(error)
}
}
}
In the comments there is what I want to do, in my class called "Tags" there are two cols called "username" and "tagtext" I want to save them in the two string variables "tagAutor" and "tagText". The println(tag) is printing out the following:
How can I save the objects out of my query into this two string variables?
Tell the compiler to convert the AnyObject to a String:
if let author = tag["username"] as String {
tagAutor = author
}
And probably move the definition of tagAuthor so you can use I outside the function

Resources