LiquidPlayer/LiquidCore

Events are not emitted when extending NSObject

dsemenovsky opened this issue · 1 comments

I am trying to create a service class which, I believe, is more common approach when talking to JavaScript in the background. However, I cannot get any events emitted.

Here is the class:

import Foundation
import LiquidCore

class TestClass: NSObject, LCMicroServiceDelegate,
  LCMicroServiceEventListener {


    override init() {
        super.init()
        let url = LCMicroService.bundle("example")
        let service = LCMicroService(url: url,
                                       delegate: self)
        print(service)
        service?.start()
    }

  func onStart(_ service: LCMicroService) {
    print(service)
    service.addEventListener("ready",listener: self)
    service.addEventListener("pong", listener: self)
  }

  func onEvent(_ service: LCMicroService,
                   event: String,
                 payload: Any?) {
    print(event)
    if event == "ready" {
      service.emit("ping")
    } else if event == "pong" {
      let p = (payload as! Dictionary<String,AnyObject>)
    print(payload)
      let message = p["message"] as! String
      DispatchQueue.main.async {
        print(message)
      }
    }
  }
}

Here is the ViewController:

import UIKit

class ViewController: UIViewController {

  @IBOutlet weak var textBox: UITextField!

  override func viewDidLoad() {
    super.viewDidLoad()
    
    TestClass()
  }
}

And this is the output:

Optional(<LCMicroService: 0x6000039b6530>)
2020-05-25 14:26:23.980155-0400 Test[24162:4495040] MicroService User-Agent : LiquidCore/0.7.8 (iOS; API=13.5)
2020-05-25 14:26:23.987398-0400 Test[24162:4495025] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
2020-05-25 14:26:23.987633-0400 Test[24162:4495025] Cannot start load of Task <010A76A4-AFAE-4A16-A08B-0B348D5F4C77>.<1> since it does not conform to ATS policy
2020-05-25 14:26:23.992659-0400 Test[24162:4495025] Task <010A76A4-AFAE-4A16-A08B-0B348D5F4C77>.<1> finished with error [-1022] Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x600001cc8510 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=http://localhost:8082/example.bundle?platform=ios&dev=true, NSErrorFailingURLKey=http://localhost:8082/example.bundle?platform=ios&dev=true, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}
2020-05-25 14:26:24.043894-0400 Test[24162:4495040] stdout: Hello, World!

As you can see, events are not getting emitted.

Same sample code works when extending UIViewController.

Solution:

TestClass in this case wasn't reference anywhere else and delegate in LCMicroService got removed since it is a weak reference.

Closing this issue.