我从 RestKit 开始,我设法进行查询并将其加载到 TableViewController 中。
查询返回:idTask、nameTask 和assignedUserId。
直到这里一切正常,完美运行。当我想从其assignedUserID(这是其他请求)加载用户名时,问题就来了。
我执行以下操作:
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
[self.refreshControl endRefreshing];
NSLog(@"%d Tasks received", objects.count);
self.resultsArray = objects;
Task *task;
for (int i=0; i<self.resultsArray.count; i++){
task=(Task*) [self.resultsArray objectAtIndex:i];
task.user= [[User alloc]init];
[task.user getUserDetails:task.idUser];
}
[self.resultsTable reloadData];
}
对于收费用户调用我的班级的每项任务,我在其上加载数据,但该用户无法在我的班级工作中使用它。他的属性总是NULL。
如何加载多个对象?
Mi 类用户是这样的:#import "User.h"
@implementation User
-(User *) getUserDetails:(NSNumber *)idUsuario{
[self loadUsersAssigned:idUsuario];
return self;
}
-(void) loadUsersAssigned:(NSNumber *)idUsuario
{
NSString *urlGetUser;
urlGetUser = [NSString stringWithFormat:@"MY URL", idUsuario,[[NSUserDefaults standardUserDefaults] objectForKey:@"userAccessToken"]];
//Load objects from url
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[User class]];
//Set relations (keys)
[objectMapping mapKeyPath:@"id" toAttribute:@"idUser"];
[objectMapping mapKeyPath:@"first_name" toAttribute:@"name"];
[objectMapping mapKeyPath:@"last_name" toAttribute:@"lastName"];
RKObjectManager* manager = [RKObjectManager sharedManager];
[manager loadObjectsAtResourcePath:urlGetUser delegate:self block:^(RKObjectLoader* loader){
loader.objectMapping = objectMapping;
[loader setCachePolicy:RKRequestCachePolicyNone];
}];
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
NSLog(@"Users recived: %d",objects.count);
NSArray *result = objects;
User *u = [result objectAtIndex:0];
self.idUser=u.idUser;
self.name=u.name;
self.lastName=u.lastName;
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error
{
// [self.refreshControl endRefreshing];
// NSLog(@"Error: %@", error.description);
}
@end
更新:
你怎么说,我认为我应该使用积木,但我不知道怎么做,因为我从不使用它。
我的对象正在返回正确的数据,但它来得太晚了。我给你看我的日志和我的代码。
我已经修改了你解释的循环。是这样的:
int i=1;
for (Task *task in [self resultsArray]){
NSLog(@"--- begin iteration %d ---",i);
task.user= [[User alloc]init];
task.user =[task.user getUserDetails:[task idUser]];
NSLog(@"task inside objectLoader of Task id:%@ - name:%@ - lastname:%@",task.user.idUser,task.user.name,task.user.lastName);
NSLog(@"--- end iteration %d ---",i);
i++;
}
然后日志显示:
2013-01-29 10:38:32.075 --- begin iteration 1 ---
2013-01-29 10:38:32.075 task inside objectLoader of Task id:(null) - name:(null) - lastname:(null)
2013-01-29 10:38:32.076 --- end iteration 1 ---
2013-01-29 10:38:32.076 --- begin iteration 2 ---
2013-01-29 10:38:32.077 task inside objectLoader of Task id:(null) - name:(null) - lastname:(null)
2013-01-29 10:38:32.078 --- end iteration 2 ---
2013-01-29 10:38:33.570 Users recived: 1
2013-01-29 10:38:33.570 Object loader of user: 522327,Xavi,Sanchez
2013-01-29 10:38:33.640 Users recived: 1
2013-01-29 10:38:33.640 Object loader of user: 522327,Xavi,Sanchez
当循环完成时,用户对象被加载。在它之前,用户的对象是空的。我需要使用块或类似的东西来等到用户对象正在加载。