0

So I have this method in which I query some Singleton class to get data, I add it to my mutable array called profile details, but It doesn't get added, if i add it to a temp array first and then use that to assign it to my profile details array it works? What's happening here?

//Property declaration 
@property (nonatomic, strong) NSMutableArray *profileDetails;

//Method definition
- (void) getAllProfiles : (NSArray *) profiles
{
if (_myHealthDetailService == nil) {
    self.myHealthDetailService = [[MyHealthDetailService alloc] init];
}

if (profiles) {

    if (self.currentProfileCounter < [profiles count]) {

        MyHealth *profile = [profiles objectAtIndex:self.currentProfileCounter];

        [self.myHealthDetailService requestForMyHealthDetailWithHealth:profile completion:^(NSArray *healths) {

            self.currentProfileCounter = self.currentProfileCounter + 1;

            if (healths) {

                NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:healths.count];

                for (MyHealth *profile in healths) {

                    NSLog(@"Adding Proile: %@",profile);


                    [self.profileDetails addObject:profile];

                    [tempArray addObject:profile];

                    NSLog(@"Proile details array after adding %@ profile is: %@",profile, self.profileDetails);

                    NSLog(@"Temp Proile details array after adding %@ profile is: %@",profile, tempArray);


                }

                self.profileDetails = tempArray;
                NSLog(@"Proile details array after copying is: %@",self.profileDetails);


            }

            DashboardHealthCell_iPad *cell = (DashboardHealthCell_iPad *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:DashboardRow_iPadTypeHealth inSection:0]];

            NSLog(@"Proile details array is: %@",self.profileDetails);

            [cell populateMyHealthProfiles:self.profileDetails];

        } failureBlock:^(NSError *error) {

        }];
    }
}
}

Relevant NSLog output:

2014-03-12 12:45:58.144 CHM[9768:70b] Proile details array after adding <MyHealth: 0xd562ce0>     profile is: (null)
2014-03-12 12:45:58.144 CHM[9768:70b] Temp Proile details array after adding <MyHealth: 0xd562ce0> profile is: (
"<MyHealth: 0xd562270>",
"<MyHealth: 0xd5629e0>",
"<MyHealth: 0xd562ce0>"
)
2014-03-12 12:45:58.144 CHM[9768:70b] Proile details array after copying is: (
"<MyHealth: 0xd562270>",
"<MyHealth: 0xd5629e0>",
"<MyHealth: 0xd562ce0>"
)
ManicMonkOnMac
  • 1,476
  • 13
  • 21
  • possible duplicate of [Having trouble adding objects to NSMutableArray](http://stackoverflow.com/q/851926) [Cannot add items to an NSMutableArray ivar](http://stackoverflow.com/q/7125326), [\[NSMutableArray addObject:\] not affecting count](http://stackoverflow.com/q/3683761), [\[NSMutableArray addObject:\] not working](http://stackoverflow.com/q/1827058) – jscs Mar 12 '14 at 18:55

1 Answers1

2

The problem seems to be that self.profileDetails is nil by the time you invoke

 [self.profileDetails addObject:profile];

There's no place where you alloc / init that Array, so nothing gets added. You can probably get rid of your tempArray by doing

// this
profileDetails = [NSMutableArray arrayWithCapacity:healths.count];
// instead of this
NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:healths.count];
Merlevede
  • 8,140
  • 1
  • 24
  • 39