Ios Parse.com-如何刷新用户';s信息
例如,当我在解析后端更改bool时,我无法在iOS应用程序中检索该值。请参见以下屏幕截图,其中用户对象的第一个布尔值手动更改,第二个布尔值从应用程序更改 第一个不起作用,而另一个(从应用程序中更改)起作用。 我通过以下代码获得值:Ios Parse.com-如何刷新用户';s信息,ios,parse-platform,Ios,Parse Platform,例如,当我在解析后端更改bool时,我无法在iOS应用程序中检索该值。请参见以下屏幕截图,其中用户对象的第一个布尔值手动更改,第二个布尔值从应用程序更改 第一个不起作用,而另一个(从应用程序中更改)起作用。 我通过以下代码获得值: [[PFUser currentUser] objectForKey:@"is_pro"] 在第一种情况下,返回对象总是nil(我手动更改了bool)。在更改用户的解析表后,调用 [[PFUser currentUser] fetch]; //synchronou
[[PFUser currentUser] objectForKey:@"is_pro"]
在第一种情况下,返回对象总是
nil
(我手动更改了bool)。在更改用户的解析表后,调用
[[PFUser currentUser] fetch]; //synchronous
或
或
以前
[[PFUser currentUser] objectForKey:@"is_pro"]
获取[PFUser currentUser]
对象的更新副本
注意:和现在已弃用,并已替换为和
更新:
正如Ryan Kreager在评论中指出的那样,使用它可能更好/更有效,或者“因为他们将在可用时使用本地缓存。”Swift中的示例: 首先,请看一看和 此外,使用它可能更好/更有效,或者“因为它们将在可用时使用本地缓存” 否则,如果不需要使用本地缓存,也可以使用
// Synchronously fetches the PFObject with the current data from the server.
user.fetch()
// Fetches the PFObject asynchronously and sets it as a result for the task.
user.fetchInBackground()
// Fetches the PFObject asynchronously and executes the given callback block.
user.fetchInBackgroundWithBlock({
(object: PFObject?, error: NSError?) -> Void in
// Code here
})
如果有指针,则不能使用fetch。这是最好的方法:
PFQuery *userQuery = [PFUser query];
[userQuery includeKey:@"addresses"];
[userQuery includeKey:@"cards"];
[userQuery whereKey:@"objectId" equalTo:[PFUser currentUser].objectId];
[userQuery getFirstObjectInBackgroundWithBlock:^(PFObject * _Nullable object, NSError * _Nullable error) {
}];
刷新您的[PFUser currentUser]。您再次保存了我:)但是
Refresh
已被弃用,所以我使用了fetch
,这就成功了。@user1463853好的,很酷。:)不知道它已经被弃用了。我会更新的。谢谢如果您不打算同时跨多个设备对用户对象进行更改,最好使用fetchifneededInBackgroundithBlock
,因为它将在可用时使用本地缓存。@RyanKreager确实如此。我猜更改数据库中的值可能不会经常发生,因此最好仅在需要时获取。。。我可以把它作为我答案的补充。我尝试了FetchiNeededInBackgroundWithBlock,但没有得到我的更新。然而,fetchInBackgroundWithBlock工作得很好。
var user = PFUser.currentUser()!
// Gets whether the PFObject has been fetched.
// isDataAvailable: true if the PFObject is new or has been fetched or refreshed, otherwise false.
user.isDataAvailable()
// Gets whether any key-value pair in this object (or its children) has been added/updated/removed and not saved yet.
// Returns whether this object has been altered and not saved yet.
user.isDirty()
// Synchronously fetches the PFObject data from the server if isDataAvailable is false.
user.fetchIfNeeded()
// Fetches the PFObject data asynchronously if isDataAvailable is false, then sets it as a result for the task.
user.fetchIfNeededInBackground()
// Fetches the PFObject data asynchronously if isDataAvailable is false, then calls the callback block.
user.fetchIfNeededInBackgroundWithBlock({
(object: PFObject?, error: NSError?) -> Void in
// Code here
})
// Synchronously fetches the PFObject with the current data from the server.
user.fetch()
// Fetches the PFObject asynchronously and sets it as a result for the task.
user.fetchInBackground()
// Fetches the PFObject asynchronously and executes the given callback block.
user.fetchInBackgroundWithBlock({
(object: PFObject?, error: NSError?) -> Void in
// Code here
})
PFQuery *userQuery = [PFUser query];
[userQuery includeKey:@"addresses"];
[userQuery includeKey:@"cards"];
[userQuery whereKey:@"objectId" equalTo:[PFUser currentUser].objectId];
[userQuery getFirstObjectInBackgroundWithBlock:^(PFObject * _Nullable object, NSError * _Nullable error) {
}];