我想在ModelUser init中创建一个异步方法。来自Parse会话的ModelUser init _userId:
-(id)init{
self = [super init];
if (self) {
sqlUserModelImpl = [[UserModelSql alloc] init];
parseUserModelImpl = [[UserModelParse alloc] init];
sqlGroupModelImpl = [[GroupModelSql alloc] init];
parseGroupModelImpl = [[GroupModelParse alloc] init];
modelUserGroupImpl = [[ModelUserGroup alloc] init];
sqlUserGroupModelImpl = [[UsersGroupsRelationShipSql alloc] init];
parseUserGroupModelImpl = [[UsersGroupsRelationshipParse alloc] init];
_user = [self getCurrentUser];
}
return self;
}
如果用户从上一个会话连接,登录控制器会调用ModelUser Instance来登录应用程序:
if ([ModelUser instance].user != nil) {
NSLog(@"username and password were correct");
//init next controller
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
screenController* nVC = [sb
instantiateViewControllerWithIdentifier:@"screenController"];
NSString* userId=[[ModelUser instance] getCurrentUser];
NSLog(@"userId Logged = %@",userId);
nVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self showViewController:nVC sender:self];
}
问题是制作异步" getCurrentUser"因为init方法返回" self"在_user参数获取Id之前。然后,我回到登录控制器nil而不是getCurrentUser结果