Look, suppose I have a "currentUser" instance of my User Model. I want to be able to access that specific "currentUser" instance anywhere in my app -- in every single controller.
As some people have pointed out, using the App Delegate seems to be a good way to do this -- and people use it this way in various examples and whatnot on the net -- but to some of us, this doesn't seem like an ideal solution.
An idea I've had -- but have not yet tried -- is to put these instances into appropriate static class vars. That is, to make instance-aware classes, so I can do something like this:
User currentUser = [User currentUser];
Or even more generically,
NSArray myUsers = [User findByName:@"Joe Bob"];
And it would return an array of users with the name Joe Bob (who exist in memory -- not persisted to the DB).
But we could take this approach a step further and bring it to persisted context. At this point the classes would be behaving more like we're in Ruby on Rails than anything else. I'm not sure whether it would be better to have something which would differentiate between in-memory models and in-db models:
[User findInMemByName:@"Joe Bob"];
[User findInDbByName:@"Joe Bob"];
Or if it would be better to make it generic:
[User findByName:@"Joe Bob"];
And then the class would do memory management -- pushing dirty objects back to the DB and clearing unchanged objects from memory when they are no longer used and when we get low memory warnings.
What do you folks think?
-Steve
|