1

I am very new at iOS and objective-c development, so I am struggling with an understanding of how I do this.

First my code:

-(NSMutableArray *)messagesGetList
{  

NSMutableArray *messageList = [[NSMutableArray alloc] init]; 

NSURL *url = [NSURL URLWithString:@"http://xxxx/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
                                     {    
                                         for (NSDictionary * dataDict in JSON) {

                                             [messageList addObject: dataDict];

                                         }                                             
                                     } 
                                     failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON)
                                     {
                                         NSLog(@"Failed: %@", error);        

                                     }];

[operation start];


return messageList;
} 

What I am having a problem with, is that I can not access the NSMutableArray *messageList inside my for (NSDictionary * dataDict in JSON) loop. I.e. nothing is added to the array while executing my loop.

How do I access the array from within my loop?

Thanks in advance for your help, fischer

3
  • Add a NSLog(@"loop"); right above your addObject line. Make sure you are actually in the loop at all. Commented Mar 29, 2012 at 17:42
  • Probably want to log the JSON object (or just set a breakpoint and inspect in a debugger) to make sure you are getting back an array. Commented Mar 29, 2012 at 17:45
  • It's in the loop two times.. And loggin JSON returns a nicely formatted array! Thanks for your suggestions, though! Commented Mar 29, 2012 at 18:03

3 Answers 3

3

Since +JSONRequestOperationWithRequest: takes blocks to be called on success and on failure, it's a good guess that this method runs asynchronously. So, if you're checking the array that's returned from your -messagesGetList right away, it's likely to be empty. If you wait a while before checking, you may see it fill up.

Sign up to request clarification or add additional context in comments.

2 Comments

I had not thought about this at all.. That's a very good point. I will check if that's the case :)
@fischer: Specifically, I think you want to do [operation waitUntilFinished]. But note that this will lock your app up until the operation is finished (barring running your own run loop). You'd probably be better off not trying to force this to be synchronous.
3

I think you need to add the __block storage modifier to your NSMutableArray variable, in order for it to be mutable inside the block response.

__block NSMutableArray *messageList = [[NSMutableArray alloc] init]; 

3 Comments

I tried adding the __block modifier. I made no difference, sadly. I am guessing the problem might be what Caleb suggested instead.. I had not thought about that at all! But thanks anyway! Great help :)
Thinking it through a little more, the code isn't going to modify messageList, it's going to modify the mutable array to which messageList refers. So __block probably isn't helpful here.
No, this doesn't matter. The __block qualifier is only necessary if you are modifying the pointer (i.e., re-assigning a new object). You can send messages to a mutable object inside a block all day long without problems. /cc @Caleb
0

You should pass a delegate object to this method, and send a message to the delegate when the network request has completed. This is a best practice on the iOS platform.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.