在MVVM设计中使用块设计此viewModel的正确方法是什么?

时间:2015-05-18 06:56:26

标签: ios objective-c mvvm viewmodel objective-c-blocks

我正在使用MVVM设计为我的应用程序开发一个注册函数。

我创建了一个sharedInstance类,并使用块实现了register方法:

- (void)registerUserWithName:(NSString *)name
                   phone:(NSString *)phone
                password:(NSString *)password
              completion:(CompletionBlock)aCallback
{
    UserModel *user = [[UserModel alloc] init];
      user.name = name;
      user.phone = phone;
      user.password = password;

[[self.client registerUser:user].task continueWithBlock:^id(BFTask *task) {
    if (task.error) {
        aCallback(NO, task.error);
        return nil;
    }
    aCallback(YES, nil);
    return nil;
}];
}

在MVC设计中,我只是这样做来调用这个方法:

 GlobalFunctions *function = [GlobalFunctions sharedInstance];
[function registerUserWithName:name phone:number password:password completion:^(BOOL isSuccess, NSError *error) {
    if(isSuccess){
        ViewController *vc = [[ViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    }
}];

我尝试将此代码应用于MVVM设计。在我的viewModel中,我有:

-(BOOL)registerUserWithUser:(NSString*)name withPhone: (NSString*)number withPassword: (NSString*)password{
RBCFunctions *function = [RBCFunctions sharedInstance];
[function registerUserWithName:name phone:number password:password completion:^(BOOL isSuccess, NSError *error) {
    return isSuccess;
}];
}

但是,这不起作用,因为我收到编译器错误:

Incompatible block pointer types sending BOOL

如何正确实施?

1 个答案:

答案 0 :(得分:0)

您获得的错误是因为您尝试从返回类型为void的块中返回布尔值。看看我在这段代码中的评论,

-(BOOL)registerUserWithUser:(NSString*)name withPhone: (NSString*)number withPassword: (NSString*)password{ 
// Begin method that returns BOOL
    RBCFunctions *function = [RBCFunctions sharedInstance];
// Call function that takes a completion handler that returns VOID!
    [function registerUserWithName:name phone:number password:password completion:^(BOOL isSuccess, NSError *error) {
        // Returning BOOL in block that should return VOID!
        return isSuccess;
    }];
    // NO RETURN VALUE!
}

如果您正在处理块(AKA异步编程)并且想要传递值,则需要创建更多块,以便代码看起来像这样:

-(void)registerUserWithUser:(NSString*)name withPhone: (NSString*)number withPassword: (NSString*)password handler:(void(^)(BOOL success))handler{
RBCFunctions *function = [RBCFunctions sharedInstance];
[function registerUserWithName:name phone:number password:password completion:^(BOOL isSuccess, NSError *error) {
    !handler ?: handler(isSuccess);
    // The above line is equivalent to:
    /*
    if (handler) {
        handler(isSuccess);
    }
    */
}];
}

你会这样称呼它:

[self registerUserWithUser:@"Badass" withPhone:@"01234567891" withPassword:@"password" handler:^(BOOL OK) {
    if (OK) {
        // Boss!
    } else {
        // Ahh NO!
    }
}];

要注意的一些事项:

  • 1)如果您的块变量为nil并且您调用它(即myBlock(args)),那么您的应用程序将崩溃。你需要在调用块之前测试是否存在;最简单的方法是使用三元运算符:!myBlock ?: myBlock(args);

  • 2)你确定你的RCBFunctions课程需要单身吗?一旦你知道单身人士在他们不需要的时候使用它们就很容易(我以前自己做过)。将注册码添加到UserModel类可能不容易吗?即:

    [myUser registerWithCallback:^(BOOL success) { // Blah blah }];