【问题标题】:Saving info to user in Parse iOS 9在 Parse iOS 9 中向用户保存信息
【发布时间】:2015-10-16 00:46:41
【问题描述】:

所以我试图将位置、显示名称和图像文件保存到 Parse,我在 iOS 9 更新之前成功保存了图像,但现在我收到解析文件太大的错误。它只是来自图像选择器的图像之一。我第一次尝试时保存了显示名称,但第二次没有保存,并且位置根本没有。谢谢您的帮助!

func saveUserInfo() {

    //saves picture data to user

    let pictureData = UIImagePNGRepresentation(profileImageView.image!)

    let file = PFFile(name: "image", data: pictureData!)

    file.saveInBackgroundWithBlock { (success, error) -> Void in

        if success {

        self.user.setObject(file, forKey: "profilePicture")
        self.user.saveInBackground()

        }

        else {
            print("error: \(error?.localizedDescription)")
        }
    }

    //saves geo point to user (location)

    PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint, error) -> Void in

        if error == nil {

            self.user.setObject(geoPoint!, forKey: "location")
            self.user.saveInBackground()
        }

        else {
            print("error: \(error?.localizedDescription)")
        }

    //saves display name

        let displayName = self.displayName.text

        self.user.setObject(displayName!, forKey: "displayName")
        self.user.saveInBackground()
    }
}

获取有效地理点的方法

func getParseGeoPoint() {

    PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint: PFGeoPoint?, error: NSError?) -> Void in

        if (error == nil) {

        self.userGeoPoint = geoPoint
        }

        else if let error = error {

            print("error: \(error.localizedDescription)")
        }

    }

}

编辑:

我尝试一次保存它们,但应用仍然崩溃

func saveUserInfo() {

    //saves picture data to user

    let pictureData = UIImagePNGRepresentation(profileImageView.image!)

    let file = PFFile(name: "image", data: pictureData!)

    file.saveInBackgroundWithBlock { (success, error) -> Void in

        if success {

        self.user.setObject(file, forKey: "profilePicture")
        self.user.setObject(self.userGeoPoint!, forKey: "location")
        self.user.setObject(self.displayName.text!, forKey: "displayName")

        self.user.saveInBackground()

        }

        else {
            print("error: \(error?.localizedDescription)")
        }
    }

【问题讨论】:

    标签: parse-platform ios9


    【解决方案1】:

    为什么不一次性将所有数据保存到用户对象?在 geoPointForCurrentLocationInBackground 成功关闭中将所有数据添加到您的用户对象,并在一次保存中上传它们。 此外,上传以进行解析的 PFFile 的最大大小仅为 10 MB,因此请检查文件的大小。

    【讨论】:

    • 好的,我在上面进行了编辑,但它仍然崩溃。我让它在视图加载时获取geoPoint,然后将其设置为一个全局属性,当用户保存他们的个人资料时我会保存它,这应该没问题,对吧?再次感谢!
    • 您不需要先上传文件。如果您将 PFFile 分配给您的对象,它将在您保存该对象时自动上传。另一个问题是你为什么要通过 self 访问你的用户对象?使用 PFUser.currentUser() 方法可以在任何地方访问您的用户对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多