在Parse iOS 9中将信息保存给用户

时间:2015-10-16 00:46:41

标签: parse-platform ios9

所以我试图将位置,显示名称和图像文件保存到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)")
        }
    }

1 个答案:

答案 0 :(得分:0)

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