mcd
mcd

Reputation: 7186

RKObjectManager mapping results issue

I am trying to store the results from [[RKObjectManager sharedManager] getObjectsAtPath:@"/groups" ... in an array and am wondering why the array is not storing my object outside of my getObjectsAtPath?

@property (strong, nonatomic) NSArray *groups;

- (void)loadGroups
{

[[RKObjectManager sharedManager]  getObjectsAtPath:@"/groups"  parameters:nil     
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    [self.refreshControl endRefreshing];

    self.groups = mappingResult.array;
    NSLog(@"GROUP COUNT INSIDE %@", [NSString stringWithFormat:@"%d", _groups.count]);
    NSLog(@"GROUP NAME INSIDE %@",[self.groups objectAtIndex:0]);
    NSLog(@"GROUP NAME INSIDE %@",[[self.groups objectAtIndex:0] valueForKey:@"groupName"]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    [self.refreshControl endRefreshing];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}];

NSLog(@"GROUP NAME OUTSIDE %@",[self.groups objectAtIndex:0]);
NSLog(@"GROUP NAME OUTSIDE %@",[[self.groups objectAtIndex:0] valueForKey:@"groupName"]);
NSLog(@"GROUP COUNT OUTSIDE %@", [NSString stringWithFormat:@"%d", self.groups.count]);
}

NSLOG output:

2013-11-10 12:04:33.281 Test[64618:11603] GROUP NAME OUTSIDE (null)
2013-11-10 12:04:33.294 Test[64618:11603] GROUP NAME OUTSIDE (null)
2013-11-10 12:04:33.345 Test[64618:11603] GROUP COUNT OUTSIDE 0
2013-11-10 12:04:33.482 Test[64618:11603] GROUP COUNT INSIDE 1
2013-11-10 12:04:33.483 Test[64618:11603] GROUP NAME INSIDE <NSManagedObject: 0x957b990> (entity: Group; id: 0x7693570 <x-coredata://174C4545-59FF-4A3A-A3B5-29520354ADD9/Group/p1> ; data: {
    groupDescription = "Admin Group";
    groupId = 1;
    groupName = Admin;
})
2013-11-10 12:04:33.484 Test[64618:11603] GROUP NAME INSIDE Admin

Upvotes: 0

Views: 399

Answers (1)

Wain
Wain

Reputation: 119041

When you say "outside of getObjectsAtPath", you really mean outside of the success block. The reason is simply that the success block hasn't run yet so your property has not had a value set yet. This is because the call to getObjectsAtPath is asynchronous and could take tens of seconds to obtain the requested data. You shouldn't expect the data to be available until the success block has been called. (Calling getObjectsAtPath doesn't block the thread until it has completed).

Upvotes: 1

Related Questions