Yoav Rothschild
Yoav Rothschild

Reputation: 21

ios - ivar is null inside a block, although set before block execution

I have the following code:

[GetUserData getUserDataWithBlock:^(UserData *userData, NSError *error)
              {
                  self.userData = userData;
              #1  self.userData.fbUser = user;
                  // Call REST API of server 'GetAllWords'
                  [GetAllWords getAllWordsWithBlock:^(NSSet *newWords, NSError *error)
                   {
                       [self saveAllWords:newWords];
                   #2  [self showRootView];
                   }];
              }];

The problem is, that self.userData is correctly set in #1 (and is not null), but when I'm getting to #2 - self.userData becomes null... Point #1 is the only place where I set self.userData.

Upvotes: 2

Views: 167

Answers (1)

Rob
Rob

Reputation: 437552

I could imagine the behavior you describe if the userData property was defined as weak (notably, if getAllWordsWithBlock runs asynchronously).

If not, I'd suggest setting up a "watch" on the underlying variable:

  • setting a breakpoint at point #1 (and at point #2, presumably) in your code and start the app in the debugger;

  • when the debugger stops at your first breakpoint, add a watch on the variable that backs your userData property by right-clicking (or control-clicking) on the variable in the "Variables" view and choosing "Watch" (obviously, this screen snapshot is a different piece of code, but it illustrates how to create a "watch" in Xcode):

    add watch

  • resuming execution by hitting the continue button: enter image description here

  • when you hit a watch breakpoint, sometimes you'll be staring at assembler, but you can hit the "step out" button enter image description here until you get to a point in your code you recognize.

That can be helpful in identifying what is changing your variable (if not a simple weak property problem).

Upvotes: 2

Related Questions