【问题标题】:Titanium - Listener callback is of a non-supported type: __NSDictionaryTitanium - 侦听器回调属于不受支持的类型:__NSDictionary
【发布时间】:2017-11-02 19:46:48
【问题描述】:

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 可用。

有什么建议吗? 谢谢

【问题讨论】:

    标签: titanium appcelerator


    【解决方案1】:

    我猜这与此处报告的相同问题 Ti.App.fireEvent JIRA 标记为 Resolved in SDK 6.3.0.GA

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


    推荐解决方案:

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

    1. 在项目的 app -> lib 文件夹中创建一个任意名称的 js 文件,例如 events_dispatcher.js
    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"});

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

    【讨论】:

    • 我正在尝试使用 6.3.0 GA,但结果相同。似乎一个 fireEvent 错误尚未解决:(
    • 别担心。我添加了一种新方法来处理全局或应用程序范围的事件处理。我强烈建议您坚持我在答案中添加的新解决方案。使用它总是更安全。
    猜你喜欢
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多