简体   繁体   中英

How to reach userLocation globally?

I want to get all the classes in my project userLocation variable. How can I identify as a global? Is that reasonable? Do I have to define each class separately? Also I would like to access the application in the background too.

Thanks

Never use global variables in general (in any programming langage), that's bad architecture/design in general.

You should use the Singleton Pattern (see Apple Doc) for such stuff of gathering common information or features global to the application. Use it parcimoniously (using it everywhere without justification is also bad design)

In the case of the userLocation, you can anyway get the latest user location retrieved by the GPS using CLLocationManager 's location property anyway, so there is no need for a global variable or a singleton for this case.

It is not recommended to create many CLLocationManager's instances. Instead, I am usually using the appDelegate to hold the single instance. You can also take a look at CLLocationManager in a single instance tutorial which demonstrate how to use the appDelegate.

As for accessing the application from the background - it depends on what you want to do. can you elaborate what exactly do you want to achieve?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM