如果从 RestKit 回调中调用 PhoneGap 回调似乎不起作用。我们已经通过删除 RestKit 调用验证了我们的 PhoneGap 回调逻辑工作正常(加上我们所有其他 PhoneGap 插件回调工作正常)。在下面的代码中,loader.onDidLoadResponse 在 RestKit 调用完成后执行,然而,即使通过 PhoneGap / Cordova 回调行执行,javascript 中相应的完成例程也不会执行。就好像回调消失了。我们是否在上下文或我们如何编写 RestKit 或 PhoneGap 异步逻辑时做错了什么?
任何帮助将不胜感激。
-(void)GetRestJson:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSString* callbackID = [arguments pop];
NSLog(@"%@",callbackID);
NSString* url = [arguments objectAtIndex:0];
NSString* sublocation = [arguments objectAtIndex:1];
NSDictionary * args = [NSDictionary dictionaryWithObjectsAndKeys:
callbackID, @"callBackID",
self, @"thisObject",
nil];
RKClient* client = [RKClient clientWithBaseURL:[RKURL URLWithString:url]];
// <---- RESTKIT CALL MADE (WORKS)
[client get:sublocation usingBlock:^(RKRequest* loader)
{
loader.onDidLoadResponse = ^(RKResponse *response) // <---- RESTKIT COMPLETION CALLED (WORKS)
{
NSString* result = [response bodyAsString];
NSString * callbackID = [args objectForKey:@"callBackID"];
id callingObject = [args objectForKey:@"thisObject"];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString: [result stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[callingObject writeJavascript: [pluginResult toSuccessCallbackString:callbackID]]; // <---- PHONEGAP CALLBACK MADE (BROKEN --- DOESN'T ARRIVE BACK ON JS CALLBACK)
};
loader.onDidFailLoadWithError = ^(NSError *error)
{
NSString* result = [error description];
NSLog(@"Loaded payload: %@", result);
NSString * callbackID = [args objectForKey:@"callBackID"];
id callingObject = [args objectForKey:@"thisObject"];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString: [result stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[callingObject writeJavascript: [pluginResult toErrorCallbackString:callbackID]];
};
}];
}