NSUserDefaultsDidChangeNotification and multitasking issue
I am using Settings app to store application settings. I also userdefaults to store internal application settings (which are not exposed in Settings app). For eg, I use user defaults to store the last tab bar index which is not exposed in Settings app.
I am subscribing 开发者_开发知识库to NSUserDefaultsDidChangeNotification to listen to any changes in settings while my app is in background and that works fine.
The issue is that when I update user defaults (for my internal application setting parameters) from within my app, this also triggers NSUserDefaultsDidChangeNotification and which I do not want.
- Is it possible to just listen to change in settings from Settings app and not from within the application?
- Is it possible to listen to the notification only when the app is in background? I tried to subscribe to notification only in applicationWillResignActive (just before moving into background) (and that works fine for registration), but I could not find a way to deregister once the application is made Active again (applicationDidMakeActive does not seem to be the correct callback since notifications are delivered after this callback).
- Or is there some other simple way to achieve this?
applicationDidMakeActive? You must mean applicationDidBecomeActive.
You might consider implementing applicationDidEnterBackground and applicationWillEnterForeground instead of applicationWillResignActive and applicationDidBecomeActive.
The "Active" methods are also called when the app starts and ends, which mean you will stop listening to the notification once it starts (you weren't actually listening to this notification) and if you implement applicationDidBecomeActive you will start listening to the notification once your application ends (for nothing).
精彩评论