以下是我正在采取的步骤 -
步骤1:我在以下字段中输入值,并将它们保存在Parse表中。效果很好。
步骤2:我再次访问此屏幕,再次单击“提交”而不编辑字段,并删除Parse表行。
我想弄清楚为什么会这样。所需的功能是当用户第一次加载此屏幕时,字段为空。此后加载此屏幕后,现有值将显示字段,如果用户编辑字段并单击“提交”,则值将是偶然的。
这是我的特定视图控制器的代码 -
import UIKit
import Parse
class RSVPViewController: UIViewController {
/*----------- ~ OUTLETS AND ACTIONS ~ -----------*/
//View Controller Title Bar Outlets
@IBOutlet weak var usernameTitle: UIBarButtonItem!
@IBOutlet weak var menuTitle: UIBarButtonItem!
//View Controller Body Outlets
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label3: UILabel!
@IBOutlet weak var label4: UILabel!
@IBOutlet weak var label1Field: UITextField!
@IBOutlet weak var label2Field: UITextField!
@IBOutlet weak var label3Field: UITextField!
@IBOutlet weak var label4Field: UITextField!
//View Controller Body Actions
@IBAction func submitButton(sender: AnyObject) {
let query = PFQuery(className:"RSVPData")
query.whereKey("username", equalTo: (PFUser.currentUser()?.username)!)
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects! as? [PFObject] {
for object in objects {
object.deleteInBackground()
}
}
} else {
print(error)
}
}
let RSVPData = PFObject(className: "RSVPData")
RSVPData["label1"] = label1Field.text
RSVPData["label2"] = label2Field.text
RSVPData["label3"] = label3Field.text
RSVPData["label4"] = label4Field.text
RSVPData["username"] = PFUser.currentUser()?.username
RSVPData.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
if let error = error {
if let errorString = error.userInfo["error"] as? String {
self.displayAlert("Failed", message: errorString)
}
} else {
self.performSegueWithIdentifier("RSVPToMain", sender: self)
}
} else {
// There was a problem, check error.description
}
}
}
/*----------- ~ MAIN THREAD ~ -----------*/
override func viewDidLoad() {
super.viewDidLoad()
//Run title bar function
TitleBar()
label1.text = "Label 1"
label2.text = "Label 2"
label3.text = "Label 3"
label4.text = "Label 4"
let query = PFQuery(className:"RSVPData")
query.whereKey("username", equalTo: (PFUser.currentUser()?.username)!)
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects! as? [PFObject] {
for object in objects {
self.label1Field.text = object["label1"] as? String
self.label2Field.text = object["label2"] as? String
self.label3Field.text = object["label3"] as? String
self.label4Field.text = object["label4"] as? String
}
}
} else {
print(error)
}
}
}
/*----------- ~ FUNCTIONS ~ -----------*/
//Function containing all title bar instructions
func TitleBar() {
/*----------- ~ NAVIGATION BAR (USER INTERFACE) ~ -----------*/
//Set the navigation bar tet color and size
let nav = self.navigationController?.navigationBar
nav?.titleTextAttributes = [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 20)!, NSForegroundColorAttributeName: UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1.0)]
/*----------- ~ OUTLET - 'usernameTitle' ~ -----------*/
//Set the 'usernameTitle' font and size
usernameTitle.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Arial", size: 11)!], forState: UIControlState.Normal)
//Set the 'usernameTitle' outlet to the current user
usernameTitle.title = PFUser.currentUser()?.username
}
//Function to display an alert
func displayAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
SOLUTION:
@IBAction func submitButton(sender: AnyObject) {
let query = PFQuery(className:"RSVPData")
query.whereKey("username", equalTo: (PFUser.currentUser()?.username)!)
query.findObjectsInBackgroundWithBlock { (object: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if object!.count > 0 {
if let object = object! as? [PFObject] {
for object in object {
object["label1"] = self.label1Field.text
object["label2"] = self.label2Field.text
object["label3"] = self.label3Field.text
object["label4"] = self.label4Field.text
object.saveInBackground()
}
self.performSegueWithIdentifier("RSVPToMain", sender: self)
}
} else if object?.count == 0 {
let RSVPData = PFObject(className: "RSVPData")
RSVPData["label1"] = self.label1Field.text
RSVPData["label2"] = self.label2Field.text
RSVPData["label3"] = self.label3Field.text
RSVPData["label4"] = self.label4Field.text
RSVPData["username"] = PFUser.currentUser()?.username
RSVPData.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
if let error = error {
if let errorString = error.userInfo["error"] as? String {
self.displayAlert("Failed", message: errorString)
}
} else {
self.performSegueWithIdentifier("RSVPToMain", sender: self)
}
} else {
// There was a problem, check error.description
}
}
}
} else {
print(error)
}
}
}
答案 0 :(得分:1)
如果存在,我不需要删除该行。您可以使用从用户获得的新值更新,而不是删除行。以下是parse.com网站上有关如何更新现有对象的示例。如果您仍然有问题,请告诉我,我将在Xcode中尝试并使用您的代码更新它。 https://parse.com/docs/ios/guide
var query = PFQuery(className:"GameScore")
query.getObjectInBackgroundWithId("xWMyZEGZ") {
(gameScore: PFObject?, error: NSError?) -> Void in
if error != nil {
print(error)
} else if let gameScore = gameScore {
gameScore["cheatMode"] = true
gameScore["score"] = 1338
gameScore.saveInBackground()
}
}
您可以使用您的代码并查看下面的评论。
query.whereKey("username", equalTo: (PFUser.currentUser()?.username)!)
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects! as? [PFObject] {
//see comments bellow
//for object in objects {
//object.deleteInBackground()
// }
}
} else {
print(error)
}
}
获取对象数组时,您将检查数组中是否只有一个对象,以确保该用户只有一行,然后执行object["sangeet"] = label1Field.text
更新所有字段并执行object.saveInBackground();
==========
当对象返回nil时,您可以创建一个新行。我想你应该可以原谅findObjectsInBackgroundWithBlock
块里面的保存操作我对swift并不是很熟悉所以我复制粘贴你的代码它应该可行。如果您还有问题,请告诉我。
let query = PFQuery(className:"RSVPData")
query.whereKey("username", equalTo: (PFUser.currentUser()?.username)!)
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects! as? [PFObject] {
for object in objects {
object.deleteInBackground()
}
}else{
//assuming that object is nil and there is no record for that user
//you would then add the user and information into the database
let RSVPData = PFObject(className: "RSVPData")
RSVPData["sangeet"] = label1Field.text
RSVPData["tibetan"] = label2Field.text
RSVPData["hindu"] = label3Field.text
RSVPData["reception"] = label4Field.text
RSVPData["username"] = PFUser.currentUser()?.username
RSVPData.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
if let error = error {
if let errorString = error.userInfo["error"] as? String {
self.displayAlert("Failed", message: errorString)
}
} else {
self.performSegueWithIdentifier("RSVPToMain", sender: self)
}
} else {
// There was a problem, check error.description
}
}
}
} else {
print(error)
}
}
答案 1 :(得分:0)
如果用户名与PFUser的用户名相同,那么我确实删除了该行。我之前没有使用Parse,但object.deleteInBackground()
看起来像是对我的删除。
let query = PFQuery(className:"RSVPData")
query.whereKey("username", equalTo: (PFUser.currentUser()?.username)!)
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects! as? [PFObject] {
for object in objects {
object.deleteInBackground()
}
}
}
}
所以我想如果你两次使用同一个用户,你只会删除该条目。