0

Ive been trying to solve this for the past one hour, but still had no luck

I have an NSMutableArray instance variable which holds objects in the following class:The array successfully gets populated in the method i populate it in, but it shows up as empty in all other methods/classes

.h file...

#import "RedditPostItem.h"

@interface RedditRepository : NSObject

@property (nonatomic, strong) NSMutableArray *redditPosts; //<<** this
is the problem array

@property (nonatomic, strong,readwrite) NSDictionary *allJSONData;
@property (nonatomic, strong,readwrite) NSMutableData *incomingData;


- (void)getPosts;
- (void)printAllTitles;

@end

.m file

@implementation RedditRepository . . @synthesize
redditPosts=_redditPosts;

. .

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {


    self.redditPosts = [[NSMutableArray alloc] initWithCapacity:20];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    //parse json data

    _allJSONData = [NSJSONSerialization JSONObjectWithData:_incomingData options:0 error:nil];

    NSDictionary *dataDictionary = [_allJSONData objectForKey:@"data"];

    NSArray *arrayOfChildren = [dataDictionary objectForKey:@"children"];


    for (NSDictionary *diction in arrayOfChildren) {

        NSDictionary *childrenData = [diction objectForKey:@"data"];


        RedditPostItem *postItem = [[RedditPostItem alloc]initWithAPIResponse:childrenData];

       //******************************* add to array..... 

        [self.redditPosts addObject:postItem];



    }

    //******* if i iterate through 'self.redditPosts' i can see everything uptill this point and the array successfully gets
populated//

}

//********* if I execute any operation from any other method in this
class or any other class...the array shows up as empty!!***//

- (void)printAllTitles{

     if(self.redditPosts == nil) {

    NSLog(@"array is empty.....");   ///always shows up as empty for some reason<<<<<< }

     }
2
  • Please indent your code with 4 spaces for proper formatting. Commented Sep 1, 2013 at 2:59
  • im using ARC by the way Commented Sep 1, 2013 at 2:59

1 Answer 1

2

Your array is being populated asynchronously - you're downloading from a URL in the background while your user interface continues to run in the foreground. The reason you're seeing it as empty is because it hasn't been populated yet. Your app should be written in such a way that it can deal with data not being available instantaneously and being able to update itself when the data becomes available.

If that doesn't get you on the right track, please ask a more specific question about the problem you're trying to solve.

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

5 Comments

Agreed, I would suggest using NSNotifications to be notified when the array is fully populated.
Thanks for answering! I can see the contents of the entire nsarray when i iterate through it right after the for loop, but it doesnot retain its values. when I try to access the array from any other method/ or even antother class (as an instance variable)... it shows up as empty. I tried adding a loop in another method to try what you said, but it just kept on looping and no values appeared.
I added the following iterator after the above for loop and I could see the data: for (RedditPostItem *testPost in self.redditPosts) { [testPost printTitle]; ` NSLog(@"printing titles");` ` }` but when i add the same lines in another method and call it again later on, the same array shows up empty
How did you verify that you're calling it later on?
nevermind what i said, you were right :D, I added a refresh button and tried to print out a log with the array values and tried it after a few seconds and it worked _ Thanks!, now i just have to figure out a way to make my code wait for the array to get populated

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.