I'm using signalr to update the values in the Swift app.
Following is my SignalRService class implementation class.
public class SignalRService {
private var connection: HubConnection
private let intIndesiesArgument : String = "argument1,argument2,..."
private let commoditiesArgument : String = "argument1,argument2,..."
init(url: URL,delegate : SignalRDataUpdateDelegate) {
connection = HubConnectionBuilder(url: url)
.withLogging(minLogLevel: .info)
.withAutoReconnect()
.build()
connection.delegate = self
connection.on(method: Events.PricesUpdated.rawValue, callback: { response in
do {
let channel = try response.getArgument(type: String.self)
if channel == Channels.commodities.rawValue{
if response.hasMoreArgs() {
let dataModel : CommoditiesModel = try response.getArgument(type: CommoditiesModel.self)
delegate.updateComodities(dataModel: dataModel)
}
}else{
if response.hasMoreArgs() {
let dataModel : CommoditiesModel = try response.getArgument(type: CommoditiesModel.self)
delegate.updateIntIndecies(dataModel: dataModel)
}
}
} catch {
//print(error)
}
})
connection.start()
}
func disconnectSignalR(){
connection.stop()
}
private func handleMessage(_ message: String, from user: String) {
// Do something with the message.
print(message)
}
}
extension SignalRService : HubConnectionDelegate{
public func connectionDidOpen(hubConnection: HubConnection) {
connection.invoke(method: Events.Subscribe.rawValue, arguments:[Channels.commodities.rawValue,commoditiesArgument]) { error in
if let e = error {
//self.appendMessage(message: "Error: \(e)")
print(e)
}else{
print("Succefully subscribe")
}
}
connection.invoke(method: Events.Subscribe.rawValue, arguments:[Channels.intlIndices.rawValue,intIndesiesArgument]) { error in
if let e = error {
//self.appendMessage(message: "Error: \(e)")
print(e)
}else{
print("Succefully subscribe")
}
}
print(hubConnection.connectionId ?? "")
}
public func connectionDidFailToOpen(error: Error) {
print(error.localizedDescription)
}
public func connectionDidClose(error: Error?) {
print(error?.localizedDescription ?? "")
}
}
Using this in ViewController as follow. Establish a connection when the view controller becomes active and disconnect when it disappears.
Class MyViewController : BaseViewController , SignalRDataUpdateDelegate {
private var connection: SignalRService?
@objc func didBecomeActive() {
connection = SignalRService(url: URL(string: "https://myURL.com/update")!,
delegate: self)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
connection?.disconnectSignalR()
}
}
It works fine and updates values but there is one problem the Memory graph goes up every single second when it receives the new value. Until App got crashed after it exceeded the memory limit. And through the following exception.
RESOURCE RESOURCE_TYPE_MEMORY (limit=2098 MB, unused=0x0)
I don't know what went wrong here.