0

I have a method when I call the post method of NotificationCenter:

func processMessage(message: CocoaMQTTMessage) {
    Log.d("DestinationTopicHandler: handling message")
    //message.string: Contains the response of the server
    print(message.string!) // print the response (String)

    bus.post(name: .DestinationRespReceived, userInfo: [message.string! : String()])
}

Here my message.string print:

{  
   "req_token":"test",
   "conv_token":"test",
   "ts":"2017-04-05 12:00:00.123",
   "code":0,
   "message":"ACCESO CONCEDIDO",
   "auth_token":"test",
   "response":{  
      "type":"test",
      "data":{  
         "destinos":[  
            {  
               "idDestino":"1",
               "desDestino":"ASUNCION"
            },
            {  
               "idDestino":"2",
               "desDestino":"MIAMI"
            }
         ]
      }
   }
}

This is my post method declaration:

func post(name: Notification.Name, userInfo info : [AnyHashable : Any]? = nil) {
    NotificationCenter.default
        .post(name: name, object: nil, userInfo: info)
}

All is OK until now. This is my final method where I try to access the userInfo data:

public func responseReceived(_ notification: Notification) {
    if processed {
        return
    }

    processed = true
    responseReceived = true
    Log.i("responseReceived:")

    guard let userInfo = notification.userInfo, let msg = userInfo["idDestino"] as? String else {
            Log.v("No userInfo found in notification")
            callback.onResponseReceived(nil)

            return
    }

    if let json = msg.json {
        callback.onResponseReceived(Response_Base(json: json.dictionary))
    } else {
        Log.v("Could not parse msg")
        callback.onResponseReceived(nil)
    }
}

The problem is I don't know why the program always return "No userInfo found in notification". Can anyone help me please?

My userInfo output is:

[AnyHashable("{ \n \"req_token\":\"test\",\n \"conv_token\":\"test\",\n \"ts\":\"2017-04-05 12:00:00.123\",\n \"code\":0,\n \"message\":\"ACCESO CONCEDIDO\",\n \"auth_token\":\"jakldsfjalkdfj\",\n \"response\":{ \n \"type\":\"test\",\n \"data\":{ \n \"destinos\":[ \n { \n \"idDestino\":\"1\",\n \"desDestino\":\"ASUNCION\"\n },\n { \n \"idDestino\":\"2\",\n \"desDestino\":\"MIAMI\"\n }\n ]\n }\n }\n}"): ""]

rmaddy
  • 314,917
  • 42
  • 532
  • 579
xhinoda
  • 1,032
  • 1
  • 19
  • 26
  • 1
    The first error seems to me that `[message.string! : String()]` is a dictionary with the message string as the *key,* and an empty string as the value. That is probably not what you intend. – Martin R Jul 19 '17 at 19:14
  • sorry...but in what way that can help me when i try no fall in "No userInfo found in notification"? – xhinoda Jul 19 '17 at 19:24
  • Break your `guard let userInfo....` statement into two pieces so you know whether it's a user info problem or a "idDestino" problem. – Phillip Mills Jul 19 '17 at 19:46

1 Answers1

0

OK...i solve my problem...thanks to @martin R

The problem was this line:

 bus.post(name: .DestinationRespReceived, userInfo: [message.string! : String()])

I did not realize that the key is the first element of my userInfo and the second element is the value. So i correct my element of my userInfo position like this:

bus.post(name: .DestinationRespReceived, userInfo: ["msg" : message.string!])

So...in my func responseReceived is correct now:

 guard let userInfo = notification.userInfo, let msg = userInfo["msg"] as? String else {

            Log.v("No userInfo found in notification")
            callback.onResponseReceived(nil)

            return
    }

Thanks!

xhinoda
  • 1,032
  • 1
  • 19
  • 26