简体   繁体   中英

What is the right way to store user data then inject it with Typhoon?

In my iOS app, I have user data that gets loaded from the network and saved as a plain Objective-C object which persists its data into NSUserDefaults . I need to be able to log out the current user and remove it, then log in a new user and start using that one instead throughout the app. Right now it is just a singleton I'm calling UserManager , with a method -(User)currentUser .

I'm transitioning to using Typhoon framework in my ( http://typhoonframework.org ) for dependency injection. What is the right way to store the user data? I know I could keep my UserManager class, inject it everywhere I need it via Typhoon, and set it as Typhoon type definition.scope = TyphoonScopeSingleton; .

But if I do this, isn't it still just a singleton? I know it's a little better because it's injected into classes rather than hidden in their implementation. But, it seems like there should be a better way to just keep a User object somewhere and inject whichever the current one is, without wrapping it in an [injected] singleton with a getter.

Thanks.

Thanks for using Typhoon!

If I understand you correctly, you need doing injecting of currentUser to your classes like as:

@implementation Assembly

- (User *)currentUser
{
    return [TyphoonDefinition withFactory:[self userManager] selector:@selector(currentUser)];
}

- (UserManager *)userManager
{
    return [TyphoonDefinition withClass:[UserManager class] configuration:^(TyphoonDefinition *definition) {
        definition.scope = TyphoonScopeSingleton;
    }];
}

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