我已经提交了一个关于如何检索objectId(swift - retrieve objectId field causes fatal error)的问题,但这似乎只适用于保存期间的对象,因为我花了很长时间(几天)尝试从一个文件中检索objectId保存在Parse云DB中的行,绝对没有运气。我使用为objectId提供的特殊值来模仿Parse对象检索文档(https://parse.com/docs/ios_guide#objects-retrieving/iOS)中的代码:
let objectId = gameScore.objectId
我每次都会遇到崩溃但现在应用程序在打印成功的登录消息后才停止。这是事件的代码和顺序。
我注册了一个新用户(让他们称他为vin)。
注册码:
@IBAction func signUp(sender: AnyObject) {
if countElements(self.userNameTextField.text) > 0 && countElements(self.passWordTextField.text) > 0 {
var megabyker = PFUser()
user.username = self.userNameTextField.text
user.password = self.passWordTextField.text
user.signUpInBackgroundWithBlock{
(succeeded:Bool!, error:NSError!)->Void in
if error == nil {
println("Sign Up successfull")
//associate current user for parse remote push notifications
var installation:PFInstallation = PFInstallation.currentInstallation()
installation.addUniqueObject("goride", forKey: "channels")
installation["user"] = PFUser.currentUser()
installation.saveInBackground()
//default row for allData table
//save default settings row for GUEST user
var alldata:PFObject = PFObject(className: "allData")
alldata["divvy_routes"] = "ON"
alldata["sortBy"] = "distance"
alldata["totalRides"] = 0
alldata["username"] = user.username
//self.useGuestUser = "USER"
alldata["user"] = PFUser.currentUser()
alldata.saveInBackgroundWithBlock{(success:Bool!, error:NSError!) ->Void in
if success != nil {
NSLog("%@","OK-alldata REAL data saved")
self.allDataSignUpObjectId = alldata.objectId
println("printing out objectId var in ALLDATA REAL section: ")
println(self.allDataSignUpObjectId)
self.performSegueWithIdentifier("go-to-main-menu", sender: self)
}
else
{
NSLog("%@",error)
}
}
} else {
let alert = UIAlertView()
alert.title = "Invalid Signup"
alert.message = "Sorry, a username (unique) and a password are required to create a new account."
alert.addButtonWithTitle("OK")
alert.show()
}
}
}
else
{
let alert = UIAlertView()
alert.title = "Invalid Signup"
alert.message = "Sorry, a username (unique) and a password are required to create a new account."
alert.addButtonWithTitle("OK")
alert.show()
}
}
注册码工作正常。它在User表中创建一个新用户和行,在allData表中创建新的默认行。 allData表有一个用户指针列,用于将其与User表关联。我抓住objectId并将其分配给一个实例变量,因为我想将它传递给另一个使用的viewcontroller:
self.allDataSignUpObjectId = alldata.objectId
println(self.allDataSignUpObjectId)
这是ParseDB的屏幕截图,您可以在其中看到该行已保存在新用户的用户表中。他的objectId是9vRF8BvdlZ,allData表中的行的objectId是DEaj0iWZ3X。这是我想要获得的objectId(DEaj0iWZ3X)。
allData table:
现在这里是插入了新用户默认设置行的allData,它自动链接到Vin的用户表:
然后我转到设置视图控制器并另存为新创建的用户:
@IBAction func updateSettings(sender: AnyObject) {
var alldata:PFObject = PFObject(className:"allData")
var user = PFUser.currentUser()
var query = PFQuery(className:"allData")
query.whereKey("user", equalTo: user)
var id = allDataPass
println(id)
query.getObjectInBackgroundWithId(id) {
(alldata: PFObject!, error: NSError!) -> Void in
if error != nil {
NSLog("%@", error)
} else {
alldata["routes"] = self.RoutesSetting
alldata.saveInBackground()
}
}
}
它保存没有问题和行更新中的更新时间。
但是当我以Vin身份重新登录时(重置模拟器上的内容和设置以获得一个新的开始),我进入LOGIN但它只是停止(没有崩溃,只是停止并且赢了但没有超过登录界面)。
这是控制台打印输出:
2015-02-26 22:19:01.732 MegaByke[51906:6996084] Location Services Not Determined, must ask user
2015-02-26 22:19:01.734 MegaByke[51906:6996084] location services enabled: true
Location services success - <MegaByke.AppDelegate: 0x7ff4b145bd80>
2015-02-26 22:19:15.700 MegaByke[51906:6996084] Reachability Flag Status: -R -----l- networkStatusForFlags
<PFUser: 0x7ff4b167f830, objectId: 9vRF8BvdlZ, localId: (null)> {
username = vin;
}
Login successfull
objectId preFETCH
nil
这是我的登录代码:
@IBAction func login(sender: AnyObject) {
switch reachability!.currentReachabilityStatus().value{
case NotReachable.value:
var alert = UIAlertView(title: "No Internet Connection", message: "In order to login, you must have internet connection. You can still login as a Guest", delegate: nil, cancelButtonTitle: "Dismiss")
alert.show()
default:
if countElements(self.userNameTextField.text) > 0 && countElements(self.passWordTextField.text) > 0 {
PFUser.logInWithUsernameInBackground(userNameTextField.text, password: passWordTextField.text){
(user:PFUser!, error:NSError!)->Void in
if user != nil
{
println(user)
println("Login successfull")
//associate current user for parse remote push notifications
var installation:PFInstallation = PFInstallation.currentInstallation()
installation.addUniqueObject("goride", forKey: "channels")
installation["user"] = PFUser.currentUser()
installation.saveInBackground()
var user = PFUser.currentUser()
var query = PFQuery(className:"allData")
query.whereKey("user", equalTo: user)
// Retrieve the most recent one
query.orderByDescending("createdAt")
query.limit = 1;
//assign objectId
var alldata = PFObject(className:"allData")
println("objectId preFETCH")
println(alldata.objectId)
//refresh object
alldata.fetch()
let id = alldata.objectId
println("objectId postFETCH")
println(id)
query.getObjectInBackgroundWithId(id) {
(alldata: PFObject!, error: NSError!) -> Void in
if error == nil {
//Load parse data from cloud
NSLog("%@", alldata)
self.performSegueWithIdentifier("go-to-main-menu", sender: self)
} else {
NSLog("%@", error)
}
}
}
else { // Log details of the failure
NSLog("Error: %@ %@", error, error.userInfo!)
}
}
}
else{
println(user)
let alert = UIAlertView()
alert.title = "Invalid Login"
alert.message = "Sorry, that username and/or password is incorrect. Please enter a valid combination."
alert.addButtonWithTitle("OK")
alert.show()
println("Login failed")
}
}
}
在获取之后应该发生的println永远不会到达控制台,因此我假设我没有成功检索到objectId。我还检查了Parse的用户论坛,没有帮助。