Xcode iphone event listener - iphone

I need guidance for the following concept:
My iOS app has a text/messaging feature... I want to create a class that checks a database every 2 mins for new data no matter where the user is at in the application. I am assuming I would create a new class and just include it on all of my other files.
Is that the best workflow for what I am trying to achieve?

You can probably run a repeat NSTimer with defined frequency from your AppDelegate and in the implemented selector you can write your piece of code to check DB for new data and may be fire a notification if it finds new data. Then you can have any of your viewController listen to that notification to get their UI updated.
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(checkDBForUpdates) userInfo:nil repeats:YES];

Related

NSTimer speeds up after UI activity

I make an asynchronous http request using NSURLConnection every 11 seconds using a NSTimer.
The NSTimer runs on the main run loop. The request has a timeout of 10 seconds.
The NSTimer works well until I increase UI activity on the app (like tapping on UIButtons, dismissing UIAlertViews frequently).
After this the NSTimer speeds up and runs like a while loop without any delays. This creates a lot of problems in the app as I can't make proper handling of the connection responses.
The problem is that you are declaring an NSTimer inside your button action... now every time this button is pressed the NSTimer interval is compiled onto its previous interval and the result is that it gets called twice as often... the next time it's twice as often than the previous twice as often.... etc.
The best way to do it is define the NSTimer in the .h file and then every time you have code in your .m file to call it first check if it's already there and if so invalidate and release it.
if(myTimer){
[myTimer invalidate];
[myTimer release];
}
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(timeInterval) userInfo:nil repeats:YES];

NStimer issue in Cocoa-Touch

My application needs to show a temporary message, so I created Toast like android myself. It works well for only one toast. I am using NSTimer to hide the message. If I display more than one toast the timer becomes a problem. Only the last added toast becomes hidden, others do not become hidden. How can I hide all toasts?
Code:
Remove function:
-(void)removeToast
{
NSLog(#"removed");
[self.view removeFromSuperview];
}
Timer start:
timer = [NSTimer scheduledTimerWithTimeInterval:(4.0f)
target:self
selector:#selector(xxxx)
userInfo:nil repeats:NO];
I wrote this in above in separate NSObject class and created an object. I need some clarification on how run the two NSTimer simultaneously or keep track of all NSObjects.
Object creation in viewcontroller is
#property(nonatomic,strong)Toast *toast;
No need to keep the istance of timer in a ivar for your purpose. Use the userinfo: parameter to pass the view you mean to hide when the timer fires, like this:
[NSTimer scheduledTimerWithTimeInterval:(4.0f)
target:self
selector:#selector(xxxx)
userInfo:yourView repeats:NO];
Then in your selector retrieve the view from the userInfo and hide it. You can find a working sample here, line 37.

How to schedule events programmatically in ios?

I have been tasked to write an app that allows a user to schedule emails to be sent out in future.
The user selects a date time from a date picker, composes the message and recipient and then schedules the event. When the date/time occurs the message is sent out.
Can someone guide me to how to get about scheduling lets say a text message. I know how to send a text message. Just was not sure on the scheduling aspect of things.
Any pointers will be much appreciated.
The first response will technically allow you to establish a timer that will fire every 2.5 seconds, however the original poster asked for a solution that would fire at a specific time. For that you need to use the following method of NSTimer:
- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
The first argument is an NSDate indicating when the timer should fire.
The original poster did not specify, but if this is an iOS app then it is important to understand that timers scheduled to fire at a distant date/time will not fire if your app is not the foreground app. In fact there is no way to schedule such an event to occur when your app is in the background on iOS, so you must take that into account.
Here's a snippet of code which sets a one use timer to call self's imageSavedLabelOff: selector with itself (the timer) as the object parameter to the method. The timer schedules the call to be made in 2.5 seconds.
NSTimer *quickie = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:#selector(imageSavedLabelOff:) userInfo:nil repeats:NO];
You may have already found the answer by now but for future visiters like me I would like to suggest an answer- i.e. EventKit :
https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/EventKitProgGuide/ReadingAndWritingEvents.html
You can schedule/fetch events for any time and do your stuff accordingly. Hope this helps somebody.
You should be able to achieve this using NSRunLoop. Check out the Threading Programming Guide.
Apart from the use of NSTimer, you should be aware that sending of the E-Mail can fail for several reasons (no network available and others). Then you need to reschedule the request, maybe give up after 3 retries and notify the user about this.
You can use -
[self performSelector:#selector(myFunc:) withObject:nil afterDelay:5.0];

iPhone: NSTimer

Have a question...
I have Timer
[NSTimer scheduledTimerWithTimeInterval:120
target:self
selector:#selector(action1:)
userInfo:nil
repeats:YES];
But when I move to another screen of my app I want to change selector...How can I change selector ? I know that I can stop my timer and set a new one, but I don't wont to reset a time remained to fire action...Thanks....
You can't. NSTimer takes its targeting information in its instantiation methods, and doesn't expose any properties to modify that later.
You're going to have to invalidate this timer and create a new one on the new target.
It looks like the selector is immutable. I would wrap this functionality into it's only tiny class with a setSelector method. Internally, create the NSTimer with a private selector. Inside that method, call the external selector that's been set using the setSelector method.
You might call a generic selector that, depending on the page shown, calls other methods:
[NSTimer scheduledTimerWithTimeInterval:120
target:self
selector:#selector(selectorDispatcher)
userInfo:nil
repeats:YES];
and than obviously your method selectorDispatcher will look something like:
- (void) selectorDispatcher{
if(pageshown1)
[self callmethod1];
else
[self callmethod2];
}
I think this should work...let me know!

Snooze method in EventKit Framework?

I am able to access all functionality of EKEventStore like save any event or remove any event from Calendar.
But how can create snooze for that event lets say 15 min snooze i needed for all saveEvent ?
I didn't find out as such method
Anyone know such method ?
If you are trying to set some function in your application to be performed after a fifteen second delay, you could use something like this:
[self performSelector:#selector(yourMethod) withObject:nil afterDelay:15];
EventKit is intended to set local notifications for the user that can be shown whether the user is running your application or not. They are exactly like push notifications, except they are stored locally on the user's device and don't require a network connection.
If you are are trying to add a snooze function to an EventKit notification, you could implement it in your application using the ApplicationDidLoadWithOptions method. That method is called whenever the user clicks the "OK" button on your local notification. As far as I know, there is no built in snooze functionality in the EventKit framework itself.
I've never tried that library, but have you tried NSTimer? Something like:
NSTimer *snoozeTimer;
//make it reachable in whole class
//setting the snooze timer. 900 s = 15 min. change to "repeats:NO" if you want just one snooze.
snoozeTimer = [NSTimer scheduledTimerWithTimeInterval:900.0 target:self selector:#selector(someAlarmMethod) userInfo:nil repeats:YES];
//and after finished snooze
[snoozeTimer invalidate];
Maybe it's not what you're looking for, but it might work :)