Question about notification.userInfo

Hello Guys,

I have two questions for notification.userinfo:

  1. The userInfo gives a dictionary containing 3 Key: “inserted” “updated” “deleted”, my question is where these keywords come from

  2. I checked the type of The value given by the dictionary, it’s any type, I find some tutorial on internet, this any type value can be casted to a single type rather than an array. Example:
    if let location = dictionary[“inserted”]
    What exactly the type will be given? Location or Array ?

Thanks guys!

  1. The keywords are set in the userInfo dictionary depends on the source of the message. If the operating system generates a notification, then the keywords in the dictionary would be set by the OS. If the notification is generated by an app, then the keywords would be set by that particular app.

  2. The type of the values in the dictionary is Any since the values can be any type. If you specify a particular type (like Location or Array) then the value can be only that particular type and can’t be any other type. Since the userInfo dictionary is meant to be used to pass any data relevant to the notification to the user and so the type of the data in the dictionary has to be Any.

Hope that makes sense.

Thanks, man. One more question, how could I know the type of a particular value given a key? I mean, for instance, in the tutorial, I have no idea which type it gives me? Location or [Location]?

The actual type of the objects in the userInfo dictionary depends on the notification. So to find out the keywords and the matching type for each keyword, you need to look up the documentation for each notification.

In the tutorial, I believe you are working with the NSManagedObjectContextObjectsDidChange notification. You can check the documentation for that notification here:

https://developer.apple.com/documentation/foundation/nsnotification.name/1506884-nsmanagedobjectcontextobjectsdid

If you read the documentation, you will notice that you don’t get any information about the type of data in the userInfo array. That is because the data can be of any type that is being saved. However, the keys themselves give you a clue as to whether it is a single value or an array. If you notice, all the keys (NSInsertedObjectsKey, NSUpdatedObjectsKey, and NSDeletedObjectsKey) are plurals. So the values would be arrays. The type would of course depend on the type of object that was saved since this is a CoreData notification.

1 Like

Hi,

Still don’t know where the app sets the keys, I went through the code again, I didn’t find any clue where these keys get set. I mean, where or how I get to know what keys are set?

Or in other words, where I could know what string can be used as the key??

Your app does not set the keys. The keys are set by iOS (or the CoreData framework, if you want to be specific) in this case.

Not sure why you think the keys have to be set by your app? Is there a reason for you thinking that the keys are set by your code? You cannot see where the keys are set because that code is in iOS itself - so not available to you.

As for what string can be used as the key, as I mentioned previously, take a look at the documentation for each notification. There is no central list of keys. If you took a look at the documentation link I provided, you would notice that it says:

“The notification object is the managed object context. The userInfo
dictionary contains the following keys: NSInsertedObjectsKey, NSUpdatedObjectsKey, and NSDeletedObjectsKey.”

That’s the list of keys for that particular notification. For other notifications, there would be other keys. Or no keys at all if a given notification does not use the userInfo object.

I see, what I mean is that what the value of key is? I know documentation provides a list of descriptions of keys, however, "NSInsertedObjectsKey, NSUpdatedObjectsKey, and NSDeletedObjectsKey” these are just constant variable, aren’t they?

The tutorial explicitly indicates the values of keys are “inserted” “deleted” “updated”, where you get these values from? Certainly not from the documentation, the doc just tells you there is a key
“let NSInsertedObjectsKey: String
A key for the set of objects that were inserted into the context.”

Where are the values? All I want to know is where you get the value? how you know NSInsertedObjectsKey = “inserted”? There must a way, right?

Actually, that is an oversight on my part - the code should not use the string values. It should use the keys as mentioned in the documentation since the string values can change. So simply use the key values as per the documentation in your own code. That is the safest, and recommended, way.

:joy::joy::joy::joy::joy: get it…

This topic was automatically closed after 166 days. New replies are no longer allowed.