I am writing a small systray application that retrieves data from the API and accordingly updates its menu, and I am having problems updating the menu when it opens.
I donโt even know where to start, so let's start from the beginning.
I have a custom class PNLinksLoader , whose task is to PNLinksLoader data and analyze it:
- (void)loadLinks:(id)sender {
The bootloader starts once when the application starts (it works fine), and then the timer is configured for periodic updates:
loader = [[PNLinksLoader alloc] init]; [loader setRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://papyrus.pandanova.com/links"]]]; [loader setDelegate:self]; [loader loadLinks:self]; NSMethodSignature *signature = [loader methodSignatureForSelector:@selector(loadLinks:)]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; [invocation setTarget:loader]; [invocation setSelector:@selector(loadLinks:)]; NSTimer *timer = [NSTimer timerWithTimeInterval:10 invocation:invocation repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
Now, when the bootloader downloads new data on the server, if the menu is closed, then everything is in order, I open the menu and new data appears.
But if during the update the menu opens, then nothing happens. If I close and open the menu again, I will see new data.
I think I'm missing something about RunLoop, but I donโt see that (my understanding of this is very poor, since I am actually writing this small application to learn Objective-C).
EDIT
The problem is not to refresh the menu while it is open, but it actually works when I use performSelector:withObject: instead of performSelectorOnMainThread:withObject:waitUntilDone:modes: in the bootloader. The fact is that I do what I get strange results when updating the menu when it opens (works fine when the menu is closed):

Adding an NSLog call to my menu, fixes population loop, and from what I read on the Internet, this may be a sign that I have a race condition in my threads (why I tried to use performSelectorOnMainThread , but I cannot figure it out.