0

我正在尝试将结果存储[[RKObjectManager sharedManager] getObjectsAtPath:@"/groups" ... 在一个数组中,并且想知道为什么该数组没有将我的对象存储在我的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 输出:

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
4

1 回答 1

1

当您说“在 getObjectsAtPath 之外”时,您的真正意思是在成功块之外。原因很简单,成功块尚未运行,因此您的属性尚未设置值。这是因为调用getObjectsAtPath是异步的,可能需要几十秒才能获取请求的数据。在调用成功块之前,您不应期望数据可用。(调用getObjectsAtPath不会阻塞线程,直到它完成)。

于 2013-11-10T20:44:51.183 回答