Titanium - 侦听器回调是不受支持的类型:__ NSDictionary

时间:2017-11-02 19:46:48

标签: titanium appcelerator

我在iOS下使用Titanium SDK> = 6.1.0 GA和Ti.App.fireEvent时出现问题。直到使用带有JSON字典作为参数的Ti.App.fireEvent的SDK 6.0.4 GA工作正常,但是从6.1.0到GA(6.3.0),我收到以下消息:

Listener callback is of a non-supported type: __NSDictionaryM

我在KrollBridge.m中看到此行可能会导致此日志消息:

- (void)fireEvent:(id)listener withObject:(id)obj remove:(BOOL)yn thisObject:(TiProxy*)thisObject_
{
    if (![listener isKindOfClass:[KrollCallback class]])
    {
        NSLog(@"[ERROR] listener callback is of a non-supported type: %@",[listener class]);
        return;
    }

    KrollEvent *event = [[KrollEvent alloc] initWithCallback:listener eventObject:obj thisObject:thisObject_];
    [context enqueue:event];
    [event release];
}
当我在设备上试用应用程序时,

ONLY 失败,在模拟器上工作正常!。

添加一些NSLog并运行应用程序:

控制台:

Simulator:
[DEBUG] :  Firing app event: event_test
[DEBUG] :  Listener type: KrollCallback
[DEBUG] :  OK

Device:
[DEBUG] :  Firing app event: event_test
[DEBUG] :  Listener type: __NSDictionaryM
[ERROR] :  Listener callback is of a non-supported type: __NSDictionaryM

代码:

Ti.App.addEventListener('event_test', fun);
Ti.App.fireEvent('event_test', data: {"ID": 0, "Value": "Test"});

我不知道为什么。我感觉这是6.1.0 GA中引入的错误,它会在最后一个GA可用之前重现。

有什么建议吗? 感谢

1 个答案:

答案 0 :(得分:1)

我想这与此处报告的问题相同 Ti.App.fireEvent JIRA ,标记为已在SDK 6.3.0.GA中解决

将Ti SDK更新到最新的6.3.0.GA,因为它比其他6.x版本更稳定。

推荐的解决方案:

请在此处阅读为何避免使用Ti.App.addEventListener&按照以下步骤设置全局事件侦听器以传递您想要的任何内容。

  1. 在项目的应用中创建一个带有任何名称的js文件,例如 events_dispatcher.js - > lib 文件夹。
  2. 在此文件中添加以下代码行: module.exports = _.clone(Backbone.Events);
  3. 现在用以下内容替换你的 Ti.App.addEventListener('event_test', fun); 的代码:

    var eventHandler = require('events_dispatcher');
    
    eventHandler.on('event_test', fun);
    
    
    // to avoid duplication of adding the same event
    // make sure to remove this event when you close the controller
    // or you are planning to re-create the controller 
    
    eventHandler.off('event_test', fun);
    
  4. 最后将此代码 Ti.App.fireEvent('event_test', data: {"ID": 0, "Value": "Test"}); 替换为此代码 - require('events_dispatcher').trigger('event_test', data: {"ID": 0, "Value": "Test"});

  5. 这种代码方法可能看起来更多,但它是最推荐的方式&最佳解决方案,满足您替换Ti.App.fireEvent的所有需求。坚持下去&您可以通过任何类型的数据来安全地调用任何方法。