简体   繁体   English

iOS Typhoon DI框架将取代Objective-C中的单例

[英]iOS Typhoon DI framework to replace singleton in objective-c

I am working an storyboard/objective-c based iOS app with firebase authentication. 我正在使用带Firebase身份验证的基于Storyboard / objective-c的iOS应用程序。 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 我使用Cloud Firestore保存用户数据-年龄,性别等。当用户到达应用程序时,我检查用户是否已登录(以下)

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. 我想创建一个具有登录用户和来自Firestore的数据的用户对象,并将其在视图控制器之间传递。

Singleton seemed to do the job well and kind of ideal for my situation but I came across Typhoon! 辛格尔顿(Singleton)似乎做得很好,并且很适合我的情况,但是我遇到了台风!

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 这就是我获取AuthenticatedUser实例的方式

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 在组件中使用TyphoonScopeSingleton作为definition.scope
  2. make the assembly default 将程序集设为默认

I am wondering if someone could provide me with some guidance regarding this. 我想知道是否有人可以为此提供一些指导。

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. 台风仍然是Objective-C依赖注入库的最佳选择。 It is feature complete, so new features are generally not added, however it is maintained and supported by AppsQuick.ly. 它功能齐全,因此通常不添加新功能,但是由AppsQuick.ly维护和支持。

If you're working with Swift, Fiery Crucible is an excellent DI framework. 如果您正在使用Swift, Fiery Crucible是一个出色的DI框架。 It has most of the features of Typhoon, simple to use, and without the drawbacks of some of the other Swift frameworks. 它具有Typhoon的大多数功能,易于使用,并且没有其他Swift框架的缺点。

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 在组件中使用TyphoonScopeSingleton作为definition.scope
  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. 这个想法是让一个 Typhoon实例在composition root上创建,然后在运行(前景或背景停止)应用程序的整个生命周期中与您的应用程序一起生活。

  • We don't ask Typhoon for dependencies, we tell it to inject the dependencies to the controller, service or other class. 我们不要求Typhoon提供依赖关系,而是告诉它将依赖关系注入到控制器,服务或其他类中。
  • The only exception to this is when using the factory pattern , where we have a mix of static dependencies, alongside runtime arguments, for example: "give me the order view controller for this user". 唯一的例外是使用factory模式时 ,其中我们混合了静态依赖项和运行时参数,例如:“给我该用户的订单视图控制器”。 In this case we inject the assembly itself. 在这种情况下,我们将注入装配本身。

For iOS, Typhoon provides a way to bootstrap your assembly on startup, either with or without storyboards. 对于iOS,Typhoon提供了一种在启动时引导程序集的方法,无论有无故事板。 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. 如果您在尝试上述资源后仍遇到其他障碍,请询问另一个具体问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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