Reputation: 193
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: [email protected]
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.
Upvotes: 1
Views: 2898
Reputation: 193
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!!
Upvotes: 4