Notification Service Extension restrictions

I have an iOS app which uses Notification Service Extension (NSE) to process incoming notifications before it displayed to user.

In NSE, as part of initialization of my app, I need to iterate through a 2D array. There are roughly 65k iterations. I've noticed that this iteration fails somewhere in between and the NSE process crashes... I can say it crashes because the logs stopped in between the iterations. This results in 'unmodified notification' getting displayed immediately, whereas NSE is granted 30 sec of background execution.

My question is, why does this happen? The above iteration of 2D array works in app, but fails in NSE. Is there some kind of restriction on background extensions? - the documentation only talks about a time limit of 30sec. If there is some kind of restriction (like CPU and memory), how does one know this and code for it... since Apple did not provide any documentation. Or perhaps, there is a completely different reason?

The extension has a set memory limit, which is much less than that of an app. Why can you not do the iteration in the app when it first launches rather than the extension? (the extension is not part of the app, it is separate from it but data from the iteration process can be shared via the filesystem for example).

Also an extension doesn't simply run for 30 seconds, it will run until the completion handler is called, if the handler is not called within 30 seconds then the system will call the handler for you. If you have to do the iteration within the extension, you will need to delay calling the completion handler until the iterations have completed. If that takes longer than 30 seconds, then that's another good reason why you need to look at doing this in the app rather than the extension.

You say the iteration is part of the initialization process, hence its a one time operation, why therefore cannot it be performed by the app (I use the term app to not include the extension) the first time the app runs, and then the results of that made available to the extension?

You say the iteration is part of the initialization process, hence its a one time operation, why therefore cannot it be performed by the app (I use the term app to not include the extension) the first time the app runs, and then the results of that made available to the extension?

I've not thought about it until now... since I didn't know extensions will fail like this.

Also an extension doesn't simply run for 30 seconds, it will run until the completion handler is called, if the handler is not called within 30 seconds then the system will call the handler for you. If you have to do the iteration within the extension, you will need to delay calling the completion handler until the iterations have completed.

I am delaying the invocation of completion handler until the iterations are completed. In the app, these 65k iterations take 2 sec. In the extension, after 38k interactions (roughly), the process is killed... and the default notification is displayed as is. This does not take the whole 30 sec... after 1 or 2 sec, process is killed. So, I'm not hitting the time limit.

The extension has a set memory limit, which is much less than that of an app.

The 2D array is static. So, memory usage shouldn't spike suddenly. But CPU usage will spike when the iterations begin. So, is there similar restriction on CPU usage? Are all these restrictions, whether its CPU or memory or any other system resource consumption, documented somewhere? I'd like to know the boundaries when rethinking initialise step.

I've never encountered any CPU restrictions with an extension. So maybe it is running out of memory. You have enough memory to load the array, but then maybe you are consuming more memory as you process the array. If you run/debug the extension (as opposed to running/debugging the app) in Xcode you can monitor the memory usage in real time just as you can with the app. I think from memory the memory limit is 20Mg, so you can see if you are getting close to and then hitting this limit (if you go over while running the extension in Xcode then it'll tell you) If you are running out of memory then you can try using autoreleasepool {} during processing to free memory.

The OS churns out a lot of logging, if you search in the log console (not Xcode's log console) for your extension bundle id while its running, the OS will probably log what the problem is. Or as mentioned above, run the extension in Xcode and that'll probably tell you what the issue is.

Notification Service Extension restrictions
 
 
Q