Parse.com - How to Update User Information

When I change, for example, bool in the Parse backend, I cannot get this value in my iOS application. See the following screenshot where the first bool of the user object was manually changed and the second was changed from the application.

enter image description here

The first does not work, while the one that was changed from the application works. I get the value with the following code snippet:

[[PFUser currentUser] objectForKey:@"is_pro"] 

In the first case, the returned object is always nil (where I changed the bool manually).

+6
source share
3 answers

After making changes to the user's Parse table, call

 [[PFUser currentUser] fetch]; //synchronous 

or

 [[PFUser currentUser] fetchInBackground]; //asynchronous 

or

 [[PFUser currentUser] fetchInBackgroundWithBlock:^(PFObject *object, NSError *error) { //asynchronous with completion block 

front

 [[PFUser currentUser] objectForKey:@"is_pro"] 

to get an updated copy of your object [PFUser currentUser] .

Note: refresh and refreshInBackgroundWithBlock: now deprecated and replaced with fetch and fetchInBackgroundWithBlock:

Update:

As Ryan Kraeger noted in the comments, it is better / more efficient to use fetchIfNeeded , fetchIfNeededInBackground , or fetchIfNeededInBackgroundWithBlock: "since they will use the local cache if available."

+14
source

Examples in Swift :

First of all, look at isDataAvailable and isDirty .

 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() 

It is also probably better / more efficient to use fetchIfNeeded , fetchIfNeededInBackground or fetchIfNeededInBackgroundWithBlock: "since they will use the local cache if they are available."

 // 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 }) 

Otherwise, you can also use fetch , fetchInBackground , fetchInBackgroundWithBlock if you do not need to use a local cache.

 // 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 }) 
+2
source

If you have pointers, you cannot use fetching. this is the best way:

  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) { }]; 
0
source

Source: https://habr.com/ru/post/979876/


All Articles