【问题标题】:Uploading changes to Parse PFUser Object not working上传对 Parse PFUser 对象的更改不起作用
【发布时间】:2015-04-08 15:03:29
【问题描述】:

我创建了一个带有 Parse Log In 功能的简单 iPhone 应用程序。然后,我有一个按钮链接到的页面,登录用户可以在该页面上查看他们的个人资料。

为了获取此信息并将其加载到配置文件页面中,我从 viewDidLoad() 调用以下 loadData() 函数:

func loadData() {

    // DISPLAY PICTURE CODE
    dpImage.image = UIImage(named: "profilePlaceholder")
    dpImage.layer.cornerRadius = 50
    dpImage.layer.masksToBounds = true

    // Get and set Name and Degree
    fullNameLabel.text = PFUser.currentUser()["fullName"] as? String
    degreeLabel.text = PFUser.currentUser()["degree"] as? String

    // Check degreeYear of user and create String
    var degreeYear = String(PFUser.currentUser()["degreeYear"] as Int)

    if (degreeYear == "1") {
        degreeYear += "st Year"
    } else if (degreeYear == "2") {
        degreeYear += "nd Year"
    } else if (degreeYear == "3") {
        degreeYear += "rd Year"
    } else {
        degreeYear += "th Year"
    }

    // Get Group Contribution Rating and add % sign
    var groupContribution = String(PFUser.currentUser()["groupContribution"] as Int) + "%"

    // Set labels of cells
    emailCell.detailTextLabel?.text = PFUser.currentUser().email
    institutionCell.detailTextLabel?.text = PFUser.currentUser()["institution"] as? String
    yearOfDegreeCell.detailTextLabel?.text = degreeYear
    groupContributionCell.detailTextLabel?.text = groupContribution

}

这很好用。然后我在个人资料页面上有一个编辑按钮,它链接(模态转场)到几乎相同的页面,但每个属性都有文本字段而不是标签。

在此配置文件编辑页面上,单击“保存”会运行以下命令:

@IBAction func saveAction(sender: AnyObject) {

    PFUser.currentUser()["fullName"] = fullNameTextField.text
    PFUser.currentUser()["degree"] = degreeNameTextField.text
    PFUser.currentUser().email = emailTextField.text
    PFUser.currentUser()["institution"] = institutionTextField.text

    self.dismissViewControllerAnimated(true, completion: nil)

}

Profile 页面的 viewDidAppear 函数再次运行 loadData() 函数刷新标签上的数据,成功将标签更新为更改后的值。

但是!我的问题是它似乎实际上并没有将我的更改上传到 Parse 数据库。这是因为当我以同一用户的身份注销并重新登录应用程序时,我看不到新更新的配置文件属性值,而是看到更改之前的原始值。

如何将我在“编辑个人资料”页面中所做的更改推送回 Parse 服务器?

注意:这是我在 StackOverflow 上的第一篇文章,如果我不够清楚,请告诉我 :)

【问题讨论】:

  • 尝试保存当前用户:[[PFUser currentUser] saveInBackground];

标签: ios iphone swift parse-platform xcode6


【解决方案1】:

您需要将信息保存在解析中。首先,我将首先为 PFUser.currentUser() 设置一个变量。就像这样:

var UserInfo = PFUser.currentUser()

然后你需要在后台保存你的信息。所以在你的dismissViewController之前,使用这个:

UserInfo.saveInBackgroundWithBlock {
(success:Bool, error:NSError!) -> Void in
}

【讨论】:

  • 除了可能的可读性改进之外,设置变量 UserInfo 有什么好处吗?另外,您能否解释一下我将如何使用该错误块?我在哪里放置我的错误处理代码? (即我可能会推送带有错误的 UIAlert)
  • 在那个代码块中,如果大括号,你可以放 if 语句,什么不。所有这一切都是将您的信息保存到 Parse 中的一个类中。所以在你看到的两个括号之间的任何地方。
【解决方案2】:

您在当前用户上正确设置了值,但您没有调用其中一种保存方法。

在为当前用户设置新值后尝试添加PFUser.currentUser().saveInBackground()

【讨论】:

    猜你喜欢
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    相关资源
    最近更新 更多