Application Will Enter Foreground Notification and memory - memory-management

I'm adding my View Controller as a listener to UIApplicationWillEnterForegroundNotification in my viewDidLoad function (as recommended on previous questions here):
if(&UIApplicationWillEnterForegroundNotification != nil)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myFunc) name:UIApplicationWillEnterForegroundNotification object:nil];
}
}
The problem is that when debugging the application through Instruments it appears this line of code is extremely memory consuming. Any thoughts as to why and how to make it more effective?

Related

NWSWindow events sequence when NSWindow closes?

Can Anyone give a list of NWSWindow events sequence when NSWindow closes. More specifically which is the last notification that a NSWindow that closes sends. Apple docs are very sparse on any sequence stuff.
The messages sent to a window when closing are – windowShouldClose: and – windowWillClose:. These are sent to the window's delegate and conform to the NSWindowDelegate protocol.
Also you can register to receive NSWindow's notifications such as NSWindowWillCloseNotification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowWillCloseNotification:) name:NSWindowWillCloseNotification object:self.window];
- (void)windowWillCloseNotification:(NSNotification*)notification
{
// ... do something, save information...
NSWindow *window = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowWillCloseNotification object:window];
}

How to detect when user opens the OS X Notification Center?

How do I detect when a user opens the OS X Mountain Lion Notification Center?
Is there an NSNotification (ugh, very similar term for a different thing) which I can observe?
I don't know of any officially documented solution or notification (let me know!), but this appeared to work (at least on OS X 10.10) when I tested it, so long as my application was in the foreground/had the frontmost window I believe.
Add your object as an observer:
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationCenterOpened:) name:#"com.apple.HIToolbox.beginMenuTrackingNotification" object:nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationCenterClosed:) name:#"com.apple.HIToolbox.endMenuTrackingNotification" object:nil];
Add methods similar to the following to your object, making sure to check for the correct ToolboxMessageEventData number (4927), for example:
- (void)notificationCenterOpened:(NSNotification*)notification {
if ([notification.userInfo[#"ToolboxMessageEventData"] isEqual: #4927]) {
NSLog(#"Notification center opened");
}
}
- (void)notificationCenterClosed:(NSNotification*)notification {
if ([notification.userInfo[#"ToolboxMessageEventData"] isEqual: #4927]) {
NSLog(#"Notification center closed");
}
}
Let me know if that does or doesn't work for you.
Nevermind - upon restart/log-off + log back in, the ToolboxMessageEventData appears to change.

NSDistributedNotifications not distributed between instances of (same) app(s)

On 10.7.2 I have trouble getting standard NSDistributedNotifications to work out of the box.
Even when brought back to just (Full XCode version at https://github.com/dirkx/Example-NSDistribtuedNotification-Failing) to something as simple as below I get:
Splendidly working notifications 'locally' (Like with a NSNotificationCenter) but
No inter-app comms when I start the app up twice (e.g. from the command line)
No notifications when another apps registers for this (or for nill)
No debug/info in the distnoted daemon logs either.
What am I missing ?
NSString * kSayNotification = #"org.webweaving.sayExample";
// Send a Distributed Notification on button press.
//
-(IBAction)buttonChange:(NSButton *)sender {
NSString * str = (sender.state == NSOnState) ? #"Yes" : #"No";
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:kSayNotification
object:str
];
}
// Update a label on receiving a Notification.
//
-(void)notif:(NSNotification *)nf {
.. snipped time string ...
// Textfield with the time of arrival and the value passed in the notification.
//
textField.stringValue = [NSString stringWithFormat:#"%#: %#",
dStr, (NSString *)nf.object
];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Register for the notifications.
//
[[NSDistributedNotificationCenter defaultCenter]
addObserver:self
selector:#selector(notif:)
name:kSayNotification
object:nil];
}
As an aside - notification watcher (https://github.com/melo/notification-watcher) does not show the Notifiactions - but the notifications are processed within the app.
Turns out that when there are no other reasons for CFRunLoop to return - the messages are queued indefinitely. This seems by design.
I found three work arounds for this - none of which is that nice - they both involve
adding a deliverImmediately:YES to the posting,
a deliverImmediately: with NSNotificationSuspensionBehaviorDeliverImmediately to the observer or
setting a timer to intentionally interrupt the runLoop every seconds.
Obviously - none of this comes cheap.
Dw

How to hide window of UIAgent process with cocoa

I have an UIAgent application with one window. I want to hide/show it from another application.How do I do it with cocoa? Seems like hide/unhide methods of NSRunningApplication doesn't affect UIAgent processes.
Thanks in advance
I solved it with NSDistributionNotifications. In the UIAgent application I add an observer to a #"QuitProcessNotification" (any other name):
[[NSDistributedNotificationCenter defaultCenter]
addObserver:self selector:#selector(quit:)
name:#"QuitProcessNotification"
object:#"com.MyCompany.MyApp"
suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
The callback looks like that:
- (void) quit:(NSNotification *) notification
{
[NSApp terminate:nil];
}
In the main application:
Sending notification:
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:#"QuitProcessNotification"
object:#"com.MyCompany.MyApp"
userInfo: nil /* no dictionary */
deliverImmediately: YES];
Be sure, that the object parameter is indeed your sender application's bundle identifier.

How do you reload a UIView?

I have a UINavigationController where the user can go back/fourth. When the user goes back, I want that UIView to reload. ( I am actually using an OHGridView ). On my ViewWillDisappear, I do something like this:
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] postNotificationName:#"ReloadOHGridView" object:self];
}
So when they go back, it will send a NSNotification to the OHGridView to refresh it's data. It get's called, but it get's an error Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DetailViewController reloadData]: unrecognized selector sent to instance 0x4b9e9f0
Here's how I set up my NSNotificationCenter (in my DetailViewController):
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ReloadGridNotification:) name:#"ReloadOHGridView" object:nil];
}
- (void)ReloadGridNotification:(NSNotification *)notification{
[database executeNonQuery:#"DELETE * FROM images"];
[items removeAllObjects];
[self reloadData];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Now you would think it would update, but I get that error... Please help!
Coulton
Actually, I wouldn't think that it would update. reloadData isn't the name of a documented method of UIViewController, and you don't seem to have implemented one yourself. I'm not familiar with OHGridView, but I perhaps that's the object to which you want to send the reloadData message.
So you can change the observer that you set up from self to your instance of OHGridView, or you can implement a method in your view controller called reloadData that in turn sends the appropriate reload message to your OHGridView.

Resources