我是使用解析的新手。我尝试使用解析用户注册创建用户对象,然后尝试将用户对象保存到本地存储。现在,当我尝试检索该对象时,我的错误是
[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/