iosobjective-cdependency-injectiontyphoon

iOS Typhoon DI framework to replace singleton in objective-c


I am working an storyboard/objective-c based iOS app with firebase authentication. I use cloud firestore to save user data - age, gender etc. When user reaches the app I check if user is logged in or not the following(similar) code

FIRUser *firUser = [FIRAuth auth].currentUser;
if (firUser) {
    // user logged in 
    // fetch updated user date from cloud firestore 
} else {
    // NO logged in user 
}

When user is logged in, they can navigate to other section of the app, otherwise they would see a signup/login page.

What it looks Navigating to different views usually means I have to call above code to figure out the logged in state again - which I do not want to do. I would like to create a user object with logged in user and data from firestore and pass it between view controllers.

Singleton seemed to do the job well and kind of ideal for my situation but I came across Typhoon!

First question is, is it still ok to use that framework? Seems a little inactive, but very amazing technology though.

Secondly here is my implementation of it - I have an assembly that looks like this

- (AuthenticatedUser*)authenticatedUser {
    return [TyphoonDefinition withParent:[self user] class:[AuthenticatedUser class] configuration:^(TyphoonDefinition* definition){

        definition.scope = TyphoonScopeSingleton;
    }];
}

And this is how I am obtaining AuthenticatedUser instance

ModelsAssembly *modelsAssembly = [ModelsAssembly defaultAssembly];

// no default ModelsAssembly set
if( modelsAssembly == nil ){
    modelsAssembly = [[ModelsAssembly new] activated];
    [modelsAssembly makeDefault];
}

authenticatedUser = [modelsAssembly authenticatedUser];

To obtain the same initiated class in different views seems like I need to do the following:

  1. use TyphoonScopeSingleton as definition.scope in the assembly
  2. make the assembly default

I am wondering if someone could provide me with some guidance regarding this.


Solution

  • First question is, is it still ok to use that framework? Seems a little inactive, but very amazing technology though.

    Answer:

    Typhoon is still the best choice of of Dependency Injection library for Objective-C. It is feature complete, so new features are generally not added, however it is maintained and supported by AppsQuick.ly.

    If you're working with Swift, Fiery Crucible is an excellent DI framework. It has most of the features of Typhoon, simple to use, and without the drawbacks of some of the other Swift frameworks.

    To obtain the same initiated class in different views seems like I need to do the following:

    1. use TyphoonScopeSingleton as definition.scope in the assembly
    2. make the assembly default

    I am wondering if someone could provide me with some guidance regarding this.

    Answer:

    This is not the correct approach. The idea is to have one instance of Typhoon, create at the composition root, and then it will live alongside your application for the life of the running (foreground or back-grounded) app.

    For iOS, Typhoon provides a way to bootstrap your assembly on startup, either with or without storyboards. The sample shows how to do this, along with this guide on storyboards.

    If you face another obstacle after trying the above resources, please ask another specific question.