-1

In have a problem to Change CalUsed value I want to put Data in NSNumber *CalUsed1 to CalUsed for use in other function

@property (nonatomic, assign) NSNumber *CalUsed;



- (void) GetCalUsed{
PFUser *user = [PFUser currentUser];
[PFCloud callFunctionInBackground:@"CalUsed"
                   withParameters:@{@"user": user.objectId}
                            block:^(NSNumber *CalUsed1, NSError *error) {
                                if (!error) {
                                    NSLog(@"Calories : %@",CalUsed1);
                                    CalUsed = CalUsed1;
                                }
                                CalUsed = CalUsed1;
                                NSLog(@"TDEE IN FN is : %@",CalUsed);
                            }];
}


- (void) TestPrint{
NSLog(@"TDEE OUT FN : %@",CalUsed);
}

RESULT

TDEE OUT FN : (null)

Calories : 700

TDEE IN FN : 700

but I need global variable "CalUsed" change value to 700

bosz.KCH
  • 15
  • 5

1 Answers1

0

Some Parse methods have a block in them. Because there will always be a delay in retrieving the data, there needs to be someone that the app "waits" until the data is retrieved. Unfortunately there is no easy way to do this so the block is there so anything that is within it will run AFTER the data is retrieved. If you put your NSLog in the block, it should work.

You have to remember that goal of any program is to execute the code as fast as possible. So when you call the Parse method, anything below it, NOT in the block will continue to run.


Update

If your going to use Parse you must understand the following (if you don't, there's no use in using Parse). Any parse functions that have a block will act differently than you would expect.

- (void) GetCalUsed{
PFUser *user = [PFUser currentUser];
[PFCloud callFunctionInBackground:@"CalUsed" //This is the Parse function
               withParameters:@{@"user": user.objectId}
                        block:^(NSNumber *CalUsed1, NSError *error) { // This is where the block starts
                            if (!error) { //if the block retrieves the data with no problem, this will run
                                NSLog(@"Calories : %@",CalUsed1);
                                CalUsed = CalUsed1;
                            }
                            CalUsed = CalUsed1;
                            NSLog(@"TDEE IN FN is : %@",CalUsed);
                        }];

}

The thing about callFunctionInBackground is that there isn't really a way to return a value. So try to follow me: When the function is called, it will create a request to the Parse database to retrieve the data. But no matter how strong your connection, there will always be a delay (even if it is by milliseconds it still counts as a delay). Like I said before, the compiler wants to execute the code as fast as possible so this delay is going to cause problems. Using the above code, the Parse function will be called, however, as the app waits to retrieve the data, it's going to continue executing everything OUTSIDE of the Parse method. This is why your getting the null value (the NSLog outside of callFunctionInBackground is executed before the data is returned from Parse). To prevent this from happening, anything that involves calUsed1 or calUsed must go inside the block. Just like this:

- (void) GetCalUsed{
PFUser *user = [PFUser currentUser];
[PFCloud callFunctionInBackground:@"CalUsed"
                   withParameters:@{@"user": user.objectId}
                            block:^(NSNumber *CalUsed1, NSError *error) {
                                if (!error) {
                                    NSLog(@"Calories : %@",CalUsed1);
                                    CalUsed = CalUsed1;

                                   //HERE YOU CAN MANIPULATE THE DATA HOWEVER YOU WISH. YOU CAN CALL A METHOD OR DO SOMETHING ELSE (aka, you can do whatever you want here)
                                }

        }];
}

All in all, everything inside the block will be run only after the data from Parse has been retrieved...no matter how long it takes. If you still have questions feel free to ask.

nick9999
  • 601
  • 1
  • 8
  • 22