0

First thing's first: I'm fairly new to iOS development.

I'm using "RestKit" in an application I'm putting together and I'm encountering an odd situation. I have a class method of which I intend to return an NSMutableArray. Below is the method in question:


+ (NSMutableArray *)
  getGames:(Player *)user
{
  NSDictionary *queryParams = @{@"user_id" : [NSString stringWithFormat:@"%d", user.user_id]};
  NSMutableArray *returnArray = [[NSMutableArray alloc] init];

  [[RKObjectManager sharedManager] getObjectsAtPath:@"games.json"
                                         parameters:queryParams
                                            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                              [returnArray addObjectsFromArray:mappingResult.array];
                                            }
                                            failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                              [returnArray addObjectsFromArray:@[error]];
                                            }];

  return returnArray;
}

In the success block, if I iterate through every object in the returnArray, it gives me the results I want. Same thing goes for failure.

However, if I iterate through the same returnArray outside of the block, in the line just above the return returnArray statement, it's empty.

Any idea why this is happening? Since returnArray is declared in the "getGames: method" shouldn't it be accessible outside the success or failure blocks within the method?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Orlando
  • 1,236
  • 3
  • 12
  • 24
  • 7
    Maybe because it's asynchronous. So The `return` is performed before the block is finished. – Larme May 12 '14 at 16:42
  • 3
    possible duplicate of [ObjectiveC Blocks and Variables](http://stackoverflow.com/questions/14082775/objectivec-blocks-and-variables) – Martin R May 12 '14 at 16:45
  • Agreed. You could have `-getGames:` return `void`, and take a completion block parameter if you want to call back asynchronously. – Zev Eisenberg May 12 '14 at 16:45
  • That makes sense. It wasn't even occurring to me that this was an asynchronous call. RTFM indeed. Thanks everyone. – Orlando May 12 '14 at 16:57
  • it is not an odd situation, in time you have the content _later_ (via a completion blocks), and you really iterate the `returnArray` _before_ the completion block runs. you just simply no have the details in that particular time when you expected. – holex May 12 '14 at 17:00
  • Can somebody (@Larme, @ZevEisenberg) who provided the answer as a comment post it as an answer so the OP can accept it. That will mark the question as answered, AND give you credit for the answer. – Duncan C May 12 '14 at 17:28
  • Instead of returning the return array you should consider using a block. (Completion block) since the success/failure is asynchronous. – Hackmodford May 12 '14 at 17:32
  • possible duplicate of [Returning method object from inside block](http://stackoverflow.com/questions/22267865/returning-method-object-from-inside-block) – Wain May 12 '14 at 18:18

0 Answers0