UserDefaults Size Limit Notification
UserDefaults has a size limit, and Apple provides a notification for when you go over it: UserDefaults.sizeLimitExceededNotification
It’s posted by the system when your app stores more data in UserDefaults than the platform allows. If you’re using UserDefaults for anything beyond small preferences (like large arrays or blobs), you’re doing it wrong — and this notification can alert you before things silently break.
You can listen for it like this:
1
2
3
4
5
6
7
8
NotificationCenter.default.addObserver(
forName: UserDefaults.sizeLimitExceededNotification,
object: nil,
queue: .main
) { _ in
print("UserDefaults size limit exceeded!")
// Consider logging or cleaning up large keys
}