Notification Service Extension usage time

Hello all. I noticed, that NSE living more than 30 seconds ( that described in doc ). When app receive notification, it created process NSE, and send notification to didReceive function, after this, app have 30 seconds to call contentHandler closure, after contentHandler is called, I expected that NSE process is killed, but it's not. If app using singletons in NSE, they won't dealloc after contentHandler is called, so, after new notification received, singletons still alive. Does it legal to not drop connection to websocket after contentHandler closure get called? For example, notification received, NSE process is loaded, websocket manager signleton is initialzied and started session, after few seconds contentHandler closure get called, so, system won't kill NSE because of 30 seconds timer, and my web socket connection will alive so long as possible, so, I not need to open it each 30 seconds, is that legal or not?)

I've not noticed them living longer than 30 seconds. How are you confirming that? If you have the Xcode debugger attached that might affect the lifetime

Once the contentHandler is closed, the system may decide that it is more efficient to keep the NSE process in memory and re-use the same process instead of terminating and reloading it for the next notification. The system will dynamically decide whether it is appropriate to handle the NSE one way or another.

So, yes, sometimes you may find that your singletons, static or class variables are still valid and containing data from the last time the NSE was run. Your code should be aware of this possibility and handle it as necessary.

This does not mean that your NSE will execute indefinitely after the contentHandler is called. Once this is called, your section of the NSE process exits, and the process will no longer get CPU time. Also, there is no guarantee that the NSE session will continue for any given instance. This is completely up to the system and cannot be influenced by anything you do in your code. Rather, the only influence on the behavior would be to not call contentHandler in a timely fashion, then the process will definitely be terminated (and any modifications to the notification content up to that point will be discarded)

For the specific use case you presented, yes you will need to close and re-open your web sockets with every notification.

@mungbeans yes, I received notification, after that web socket get ( from singleton class ) so, after contentHandler is get called, NSE process is still alive, and singleton also continue exist in memory, so, next time when you receive new notification, singleton won't init again, and same instance will be used. It was tested on real device, my NSE process ( and socket connection ) alive about 10 minutes, I think it can alive more, but as I understand from answer, system can kill it anytime

Notification Service Extension usage time
 
 
Q