ios - 在 Swift iOs 中使用 Parse 的 LocalStorage 出错

标签 ios swift parse-platform

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

[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;
}

以上代码部分,将上述错误返回给我。

提前致谢。

最佳答案

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. }

关于ios - 在 Swift iOs 中使用 Parse 的 LocalStorage 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31203489/

相关文章:

ios - 确定网络可用性

ios - 在 iOS swift 中查询使用我的应用程序的用户(解析)

ios - 不使用 UIApplication.shared 获取状态栏的大小

ios - 推送新 View Controller 时如何让语音读出标题?

objective-c - 在 xcode 中以 MM/dd/yyyy 格式转换日期?

ios - 如何用静态时间计算当前时间和昨天日期的时差

swift - 为什么自定义字体没有根据我的设置进行更改?

ios - 删除状态保存和恢复过程中保存的数据

ios - XCTestCase waitForExpectationsWithTimeout :handler: throwing EXC_BAD_ACCESS when expectation is not fulfilled

swift - 如何使用 GCD 创建串行队列