使用 Promise 从 Objective-C 导出(桥接)方法到 React native:仅 returns null

Exporting(bridging) method from Objective-C to React native with Promise : returns null only

我正在尝试将 objective-c 方法导出到带有 Promise 的 react-native,但是当我检查 react-native 的 return 值时,它始终为 null,即使 objective-c return一个正确的数据。

这是我的 objective-c 方法。

RCT_EXPORT_METHOD(checkEmail:(NSString *)email 
    resolver:(RCTPromiseResolveBlock)resolve 
    rejecter:(RCTPromiseRejectBlock)reject)
{
    @autoreleasepool {
        [GRPCCall useInsecureConnectionsForHost:kHostAddress];

        AccountService *client = [[AccountService alloc] initWithHost:kHostAddress];

        EmailCheckRequest *request = [EmailCheckRequest message];
        request.email = email;
        NSLog(@"Requested Email: %@\n", request.email);

        [client checkEmailWithRequest:request handler:^(EmailCheckResponse *response, NSError *error) {
            NSLog(@"Email Check result: %d\n", response.ret);
            if (response) {
                NSLog(@"Resolved response\n");
                resolve(response);
            }
            else {
                NSLog(@"Rejected response\n");
                reject(@"no_events", @"There was no event", error);
            }
        }];
    }
}

react-native 端方法定义是这样的。

checkEmail = (email:string) => {
    console.log('Network Check Email is called');
    return this._network.checkEmail(email);
}

checkEmail = (email:string) => {
    return new Promise((resolve, reject) => {
        console.log('Network Check Email is called');
        this._network.checkEmail(email).then((res) => {
            console.log('check email result: ' + res);
            resolve(res);
        });
    });
}

我测试了这两种情况,但是 checkEmail 的 return 值总是 Null.

async _checkEmailDuplication() {
    try {
        console.log('Check Email Duplication is called');
        const res = await MyNetwork.checkEmail(this.state.email);
        console.log('Check email return value: ' + res);
        if (res === 0) {
            console.log('Available Email');
            return false;
        }
        else {
            console.log('Email Duplication Check Error');
            this.state.errMsg = WSStrings.errCodes[res];
            return true;
        }
    }
    catch (e) {
        console.error('Check Email Call Error', e.message);
        this.state.errMsg = e.message;
        return true;
    }
}

下面是 Xcode 的日志,当我 运行 上面的代码时。

2017-07-18 00:51:43.781 [info][tid:com.facebook.React.JavaScript] Check Email Duplication is called
2017-07-18 00:51:43.782 [info][tid:com.facebook.React.JavaScript] Network Check Email is called
2017-07-18 00:51:43.839 [5782:110747] Requested Email: xxx@xxx.com
2017-07-18 00:51:43.872 [5782:109339] Email Check result: 2
2017-07-18 00:51:43.872 [5782:109339] Resolved response
2017-07-18 00:51:43.873 [info][tid:com.facebook.React.JavaScript] check email result: null
2017-07-18 00:51:43.874 [info][tid:com.facebook.React.JavaScript] Check email return value: null
2017-07-18 00:51:43.874 [info][tid:com.facebook.React.JavaScript] Email Duplication Check Error

没有得到正确的return代码,只是NULL,无法进行下一步。

Objective-C代码或react-native代码是否有语法错误?

我没有 Objective-C 背景知识,我在 Promise 和 react-native 桥接方面也没有足够的经验,所以如果有人能帮助我,我将不胜感激。

终于,我找到了答案。 resolve(response) 这部分是问题所在。

响应对象没有被Javascript方解释,所以不得不像这样交出NSDictionary对象,而不是交出响应对象本身。

            if (response) {
                NSLog(@"User ID: %d Language: %@\n", response.uid, response.language);
                NSDictionary *dictRes = @{
                    @"ret":@(response.ret),
                    @"uid":@(response.uid),
                    @"lang":response.language
                    };
                resolve(dictRes);
            }

无论如何,这是 Objective-C 的次要问题,而不是 Javascript。

问题已解决!!