IPhone Core Architecture Architecture - How to Use It Through Multiple Controllers

I need a way to get my user's current location.

Since the user has friends and they also have location data, I believe that the right way is to put the location field in my main db data, both in Friend and User objects.

Now, here is what I thought:

You have some kind of location manager, which when I need the exact location, I tell him to start the CLLocationManager updates. (for which my manager is responsible) The manager will save the location in the user.location field in Db, and the controllers interested in the user information will be registered using KVO for this data. This sounds good to me, in addition to the fact that I have to access this singelton location from every controller that needs an exact location (to tell it to start generating the location, I don't want the location updates all the time) . Plus, I try not to pollute my code with a bunch of calls.

Although it might be wiser to have a location manager as a property inside my User class. (obviously, if I have several users in memory right now, they will all have the same singelton location manager) I will add another method to the user class and call it “ StartGettingLocation ”, and now that some kind of controller needs in the current exact location, he can do one of the following:

  • If I have a User object in my controller (which was passed to another UIViewController), I simply call its " StartGettingLocation " method and KVO its field.
  • If I do not have a User object in my controller, I get it from my database and do as described above.

Does this sound right?

If so, then I'm not sure if it is possible to add a new MyLocationManager member to my User class (which is a subclass of managedObject)? What is the right way to accomplish this?

thanks

+6
source share
2 answers

We implement our CoreLocationManager as a sharedInstance (singleton) template. I'm not sure if you want your managed entity to have CoreLocation behavior, because it really is not something specific to User , but rather a device (i.e. IPhone, iPad).

In fact, CoreLocationManager does not need to update database records. Its purpose is to send updated locations. Something else needs to be listened for for these updates, so that you could implement CLLocationManagerDelegate something else, which in turn could update the managed entity.

Alternatively, your CoreLocationManager may be your own delegate and have @property on CLLocation . You can use KVO to notify observers that the location has changed as you suggested. I believe that it all depends on whether the location is relevant only to the user, or if the location is something that can be used.

+4
source

I usually set my location manager as a property of the application delegate, and then it is very easily accessible from anywhere in the application as [[[UIApplication sharedApplication] delegate] myLocationManager] . It is also convenient to use #define in the AppDelegate.h file, for example ...

 #define commonLocationManager [[[UIApplication sharedApplication] delegate] myLocationManager] 

That way, you simply include the application delegate header in any class you need to access locations using commonLocationManager.someReallyCoolProperty .

EDIT:

Whenever I need my user interface to reflect changes in location or any other state in this regard, I use Key Value Monitoring (KVO),

+2
source

Source: https://habr.com/ru/post/912058/


All Articles