在Swift iOs中使用Parse的LocalStorage出错

时间:2015-07-03 09:38:02

标签: ios swift parse-platform

我是使用解析的新手。我尝试使用解析用户注册创建用户对象,然后尝试将User对象保存到本地存储。现在,当我尝试检索该对象时,我错误

[Error]: No results matched the query. (Code: 101, Version: 1.7.5)

我注册用户的代码是

var user = PFUser()
            user.username = userNameTF.text
            user.password = passwordTF.text
            user.email = self.userProfile.userEmail
            // other fields can be set just like with PFObject
            user["gender"] = self.userProfile.userGender
            user["school"] = self.userSchoolName.text
            user["userFullName"] = self.userProfile.userFullName



            user.signUpInBackgroundWithBlock {
                (succeeded: Bool, error: NSError?) -> Void in
                if let error = error {
                    let errorString = error.userInfo?["error"] as? NSString
                    var errorCode: String = error.valueForKey("code") as! String

                } else {
                    // Hooray! Let them use the app now.
                    println("Signup Successfull")
                    self.objectId = user.objectId!

                }
            }

        user.pinInBackgroundWithBlock { (success, error) -> Void in

            if(success == true){
                self.performSegueWithIdentifier("signUpToProfile", sender: self)
            }
        }

检索已保存用户对象的代码如下所示

let query = PFQuery(className: "User")
query.fromLocalDatastore()
var queryResult =  query.getObjectInBackgroundWithId("SO3LKyHtaa").continueWithBlock {
    (task: BFTask!) -> AnyObject in
    if let error = task.error {
        // Something went wrong.
        println("Error fetching from DB")
        return task;
    }
    println(task.result.count)
    // task.result will be your game score
    return task;
}

上面的代码部分,返回上述错误。

提前致谢。

1 个答案:

答案 0 :(得分:0)

Parse会自动为您登录/登录PFUser。而不是这样做并固定userId这样做。正常进行注册步骤。

user.signUpInBackgroundWithBlock { (succeeded: Bool, error: NSError?) -> Void in
            if let error = error {
                let errorString = error.userInfo?["error"] as? NSString
                var errorCode: String = error.valueForKey("code") as! String

            } else {
                // Hooray! Let them use the app now.
                println("Signup Successfull")
                // Don't need to store the userId
                // Do your segue or whatever after sign up here.
            }
        }

然后在他/她登录/注册后检索用户:

if let user = PFUser.currentUser()
{
     // user is now the current user object.
}
else 
{
     // User has not yet signed up or logged in show sign up/login page. 
}

以下是关于它的Parse文档:https://parse.com/docs/ios/api/Classes/PFUser.html#//api/name/currentUser

此外,当您想要将用户注销以便“取消固定”时,用户只需执行以下操作:

PFUser.logOutInBackground { // Logged out now. }