Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse.com - How to refresh the User's information

When I'm changing for example a bool in the Parse backend I can not retrieve this value in my iOS app. Please see the following screenshot where the first bool of the user object was changed manually and the second was changed from the app.

enter image description here

The first one does not work whereas the one (that was changed from the app) does work. I'm getting the value with the following piece of code:

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

In the first case the return object is always nil (where I've changed the bool manually).

like image 654
gpichler Avatar asked Dec 18 '14 18:12

gpichler


2 Answers

After you've made changes to your 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

before

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

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

Note: refresh and refreshInBackgroundWithBlock: are now deprecated and have been replaced with fetch and fetchInBackgroundWithBlock:

Update:

As Ryan Kreager pointed out in the comments, it's probably better/more efficient to use fetchIfNeeded, fetchIfNeededInBackground, or fetchIfNeededInBackgroundWithBlock: "since they will use the local cache when available."

like image 87
Lyndsey Scott Avatar answered Nov 02 '22 11:11

Lyndsey Scott


Examples in Swift:

First and foremost, please have a 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()

Also it is probably better/more efficient to use fetchIfNeeded, fetchIfNeededInBackground, or fetchIfNeededInBackgroundWithBlock: "since they will use the local cache when 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 the 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
})
like image 26
King-Wizard Avatar answered Nov 02 '22 11:11

King-Wizard