I have an timer in viewDidLoad, but I want to pass variable seconds assigned by slider, so what should I do for this for such implementation?
I think I have to restart app, so is there any other solution, or how can I restart my app?
a) Keep your timer as an instance variable in your appdelegate or somewhere else, and update its interval whenever the slider moves.
b) From the user's perspective you wouldn't want to restart the app for simple things such as these.
c) You can't restart an iPhone app or in any way play around with its lifecycle.
EDIT:
If you are planning to have an instance of the timer available across the app, there are twp ways you can do that.
put is as an instance variable in the appdelegate class, the one which xcode would have created for you, with the projectnameAppDelegate. You will then be able to access it using
[UIApplication sharedApplication].delegate
from which you can get the instance of the NSTimer
Or you can implement a common Singleton class, and put the timer in it. This is a much more accepted approach described here and scroll down to the bottom of the page.
For more info, search on ways to create a global object(although creating a global object is not a good idea), and follow the instructions.
Related
I am doing a lot of setup inside my app delegate (mainly for CoreData) inside of applicationDidFinishLaunchingWithOptions. And was curious how i would go about testing code inside the appDelegate? Thanks
Step one: Stop using your regular application delegate during testing. This avoids the "it will be called at launch" problem, and will likely also speed up your tests. See https://qualitycoding.org/ios-app-delegate-testing/
Step two: Now that your regular application delegate isn't invoked when tests are launched, directly call its methods from tests.
Move the functionality into smaller functions or other classes that you can test.
If you keep things in the App Delegate class, you can access them the normal way since the unit tests are linked to the app and the app is actually run. But you cannot call applicationDidFinishLaunchingWithOptions and expect it to work. It will be called by iOS at the start, like normal.
I am new to iPhone development, i am creating a location based app. i have searched lot related to location and came to knew that use one object ( singleton pattern ) of CLLocationManager through out app, in my app i have to update user's location to server using web service,
UPDATE
as discussion with Umer i came to knew that i can use one object in appdelegate of CLLocationManager and implement delegate methods in that, and update user location to server in app delegate ,
So, is it good to do in appDelegate ??
please help.
Your locationservices will run both in foreground and background, but to allow it to run in background (when the app is open and minimized), you need to declare a special background mode in your projects "Plist" file, if I'm not wrong.
The method didUpdateToLocation will give you updates every time there is a significant movement, which you can define and adjust through various settings, such as CLLocationAccuracy (locationManager.desiredAccuracy and locationManager.distanceFilter).
Depending on what desired accuracy you ask, battery main drain quicker or not.
I don't understand what you mean with "web service". These are not web-related, they're native functionalities of iOS.
You can just call the delegate method of locationManager StartUpdatingLocation when user moves to significant distance & also call the web-service for updating User's Location.
Both Task are done in Background mode.
UPDATED ANSWER
SOURCE CODE
If I init additional CLLocaitonManager instance that track user location, is It will increase the load? Or Should I using one CLLocaitonManager instance between classes?
Creating too many CLLocationManager or increasing the update intervals of the Core location services severely drains battery. So creating too many instances is not advised. Dont see a need for this.
A good practice is to init one CLLocationManager in a viewController. If moving to another viewController, then stopUpdates on the current CLLocationManager & create a new manager in the new viewController. This is one pattern.
Another pattern is to create a CLLocationManager in app delegate & make it available throughout your app. This is like a global variable. But generally avoid global declaration of this variable because it continuously consumes your battery life.
So basically if all your classes are part of only one viewController then create only one CLLocationManager & share the location updates. If not then create one for each viewController.
Location services require the presence of specific hardware on the given device. So thats why ,its doesn't matter how many instance of CLLocationManager are created. Generally avoid global declaration of this variable because it continuously consumes your battery life. Another wise stop and start each time when ever you need to finding user location.
In in my app, say it count downs to something and at the end just beeps forever until the user quits the app, is it safe to say have an NSTimer and never release (or in NSTimer's case, invalidate it) it, since it will need to stay until the user quits the app?
Technically, yes, but I've found two things to be true:
This is an easy habit to get in to, convincing yourself that X doesn't need to be released because you've convinced yourself it needs to live forever when it doesn't and leaks, or
At some point in the future you'll realize you really want to be able to control that timer after all, and setting up the ivar now will save you a heap of trouble down the road (no pun intended)
Yes you can! If I were you though, I would initialize it in the app delegate, in applicationDidFinishLoading and invalidate it in applicationWillTerminate.
Been researching how to send data from one child view to another child view. The application has several views, where one is kind of real-time Settings view. Changes in settings should effect stuff inside other views.
NSUserDefaults seems to be popular, but I don't want persistent data nor automatic saving into database/file. Looks like potential slowdown, which I want to avoid.
Second popular thing is passing a reference to some common top level object into each child view. Just haven't found any tutorial, which would show me in detail how to do this... Most likely so easy, no tutorial is needed - after you figure it out once! Would appreciate, if you can point me to right direction (URL)!
Question: I'm thinking about using one "singleton object" to store "global data", access it from everywhere anytime. What could wrong with this? If I create it at e.g. appDelegate.m it should exist before anyone tries to access it, right?
I'm looking at this sample code.
I would recommend using delegates and/or notifications.
Instead of creating a new singleton in the app delegate, add the necessary data (or, even better, references to the data) in the app delegate and have the views have references to the app delegate.
You can also set up notifications so that your views can keep track of changes to the data that they display.
The beauty of a singleton is that it is automatically created when you first access it via some [singletonClass sharedInstance]. So you don't need to "create" it while launching. If it is global data that needs to be accessed from any view singleton might be the right way of doing this.