从Objective-C导出(桥接)方法以Promise为本机:仅返回null [英] Exporting(bridging) method from Objective-C to React native with Promise : returns null only

查看:179
本文介绍了从Objective-C导出(桥接)方法以Promise为本机:仅返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的objective-c方法。

  RCT_EXPORT_METHOD(checkEmail:(NSString * )电子邮件
解析器:(RCTPromiseResolveBlock)解析
拒绝:(RCTPromiseRejectBlock)拒绝)
{
@autoreleasepool {
[GRPCCall useInsecureConnectionsForHost:kHostAddress];

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

EmailCheckRequest * request = [EmailCheckRequest message];
request.email = email;
NSLog(@请求的电子邮件:%@ \,request.email);

[客户端checkEmailWithRequest:请求处理程序:^(EmailCheckResponse *响应,NSError *错误){
NSLog(@电子邮件检查结果:%d\\\
,response.ret);
if(response){
NSLog(@Resolved response\\\
);
resolve(response);
}
else {
NSLog(@Rejected response\\\
);
reject(@no_events,@没有事件,错误);
}
}];
}
}

反馈本地方法定义如下。

  checkEmail =(email:string)=> {
console.log('Network Check Email is called');
返回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的返回值总是 Null

  async _checkEmailDuplication(){
try {
console.log检查电子邮件复制被称为');
const res = await MyNetwork.checkEmail(this.state.email);
console.log('检查邮件返回值:'+ res);
if(res === 0){
console.log('Available Email');
返回false;
}
else {
console.log('Email Duplication Check Error');
this.state.errMsg = WSStrings.errCodes [res];
返回true;
}
}
catch(e){
console.error('检查电子邮件呼叫错误',e.message);
this.state.errMsg = e.message;
返回true;
}
}

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

  2017-07-18 00:51:43.781 [info] [tid:com.facebook.React。 JavaScript]检查电子邮件复制称为
2017-07-18 00:51:43.782 [info] [tid:com.facebook.React.JavaScript]网络检查电子邮件称为
2017-07-18 00 :51:43.839 [5782:11​​0747]要求的电子邮件:xxx@xxx.com
2017-07-18 00:51:43.872 [5782:109339]电子邮件检查结果:2
2017-07-18 00:51:43.872 [5782:109339]已解决回应
2017-07-18 00:51:43.873 [信息] [tid:com.facebook.React.JavaScript]检查电子邮件结果:null
2017 -07-18 00:51:43.874 [info] [tid:com.facebook.React.JavaScript]检查邮件返回值:null
2017-07-18 00:51:43.874 [info] [tid:com .facebook.React.JavaScript]电子邮件复制检查错误

它没有得到正确的返回码,但是只是 NULL ,不能进入下一步。



是在Objective-C代码或反应本地代码中有任何语法错误?



我没有Objective-C背景知识,在Promise和Bridge中没有足够的经验的反应原因,所以如果有人可以帮助我,那将是高度赞赏。

解决方案

最后,我找到了答案。
resolve(response)这部分是问题。



响应对象未被JavaScript解释一方面,所以不得不交出这样的NSDictionary对象,而不是递交响应对象本身。

  if(response){
NSLog(@用户ID:%d语言:%@ \,response.uid,response.language);
NSDictionary * dictRes = @ {
@ret:@(response.ret),
@uid:@(response.uid),
@lang :response.language
};
resolve(dictRes);
}

无论如何,它是Objective-C方面的问题,而不是Javascript。 >

问题解决!!


I'm trying to export objective-c methods to react-native with Promise, but when I check the return value at react-native, it's always null, even though objective-c returns a correct data.

Here is my objective-c method.

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);
            }
        }];
    }
}

And the react-native side method definition is like this.

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

or

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);
        });
    });
}

I tested both cases, but the return value of checkEmail is always 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;
    }
}

The below is the log from Xcode, when I run the above code.

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

It does not get the correct return code, but just NULL and can't go to the next step.

Is there any grammatical error at Objective-C code or react-native code?

I have no Objective-C background knowledge and I don't have enough experience in Promise and bridge of react-native, so if someone can help me, that will be highly appreciated.

解决方案

Finally, I found the answer. resolve(response) This part was the problem.

The response object was not interpreted by Javascript side, so had to hand over NSDictionary object like this, instead of handing over response object itself.

            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);
            }

Anyway, it was Objective-C side issue, not Javascript.

Problem solved!!

这篇关于从Objective-C导出(桥接)方法以Promise为本机:仅返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆