I would like to know how to get local notifications while my application’s NSTimer is firing in the background. My NSTimer checks a particular folder for files every second for 10 minutes in the background. How would I go about receiving a local notification if a file is found?
EDIT : Code :
- (void) createTimeThread: (float) pIntervalTime
{
[NSThread detachNewThreadSelector:@selector(startTimerThread)
toTarget:self withObject:nil];
}
- (void) startTimerThread
{
UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{}];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
myTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(conditionChecking:)
userInfo:nil
repeats:YES];
[runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
[pool release];
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}
- (void)conditionChecking:(NSIndexPath *)indexPath
{
NSString *pathForFile = @"/User/Library/Logs/CrashReporter";
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:pathForFile]) { // Directory exists
NSArray *listOfFiles = [fileManager contentsOfDirectoryAtPath:pathForFile error:nil];
if (!listOfFiles || !listOfFiles.count)
{
NSLog(@"No Core Dumps found.....");
}
else
{
NSLog(@"Core Dump(s) found! :%@", listOfFiles);
}
}
}
I believe that you want to notify all other classes that folder is filled with files.
Following steps can do that for you.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkFiles:) name:@"FILES_AVAILABLE" object:nil];Write methods checkFiles with following signature in same class.
-(void)checkFiles:(id)senderAdd following line in timer class when files are available.
[[NSNotificationCenter defaultCenter] postNotificationName:@"FILES_AVAILABLE" object:self];If this is not helpful then you can use NSUserDefault to store status of application(Files are available or not in you case). OR With if you are interested in design patterns read about Observer Pattern.
In case you want to post notification when your application is in background mode and some process that is still running gets some update then that can be achieved using notification queue. read following link. I am not writing code because code is given in link itself.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Notifications/Articles/NotificationQueues.html#//apple_ref/doc/uid/20000217-CJBCECJC
Post here if you need more help.