我正在使用带有 firebase 身份验证的 Storyboard/objective-c 的 iOS 应用程序。我使用 cloud firestore 来保存用户数据——年龄、性别等。当用户到达应用程序时,我检查用户是否登录了以下(类似)代码
FIRUser *firUser = [FIRAuth auth].currentUser;
if (firUser) {
// user logged in
// fetch updated user date from cloud firestore
} else {
// NO logged in user
}
当用户登录后,他们可以导航到应用程序的其他部分,否则他们会看到注册/登录页面。
外观导航到不同的 View 通常意味着我必须调用上面的代码来再次确定登录状态——我不想这样做。我想创建一个用户对象,其中包含登录用户和来自 firestore 的数据,并在 View Controller 之间传递它。
Singleton 似乎做得很好,非常适合我的情况,但我遇到了 Typhoon!
第一个问题是,使用那个框架还可以吗?似乎有点不活跃,但技术非常惊人。
其次这是我的实现 - 我有一个看起来像这样的程序集
- (AuthenticatedUser*)authenticatedUser {
return [TyphoonDefinition withParent:[self user] class:[AuthenticatedUser class] configuration:^(TyphoonDefinition* definition){
definition.scope = TyphoonScopeSingleton;
}];
}
这就是我获取 AuthenticatedUser 实例的方式
ModelsAssembly *modelsAssembly = [ModelsAssembly defaultAssembly];
// no default ModelsAssembly set
if( modelsAssembly == nil ){
modelsAssembly = [[ModelsAssembly new] activated];
[modelsAssembly makeDefault];
}
authenticatedUser = [modelsAssembly authenticatedUser];
要在不同的 View 中获取相同的启动类,我似乎需要执行以下操作:
- 在程序集中使用 TyphoonScopeSingleton 作为 definition.scope
- 使程序集成为默认值
我想知道是否有人可以就此向我提供一些指导。
最佳答案
First question is, is it still ok to use that framework? Seems a little inactive, but very amazing technology though.
答案:
Typhoon 仍然是 Objective-C 依赖注入(inject)库的最佳选择。它功能完整,因此通常不会添加新功能,但由 AppsQuick.ly 维护和支持。
如果您使用的是 Swift,Fiery Crucible是一个优秀的DI框架。它具有 Typhoon 的大部分功能,使用简单,并且没有其他一些 Swift 框架的缺点。
To obtain the same initiated class in different views seems like I need to do the following:
- use TyphoonScopeSingleton as definition.scope in the assembly
- make the assembly default
I am wondering if someone could provide me with some guidance regarding this.
答案:
这不是正确的方法。这个想法是在 composition root 处创建 一个 Typhoon 实例,然后它将与您的应用程序一起在运行(前台或后台)应用程序的生命周期内存在。
- 我们不向 Typhoon 询问依赖项,我们告诉它将依赖项注入(inject) Controller 、服务或其他类。
- 唯一的异常(exception)是使用 factory pattern ,其中我们混合了静态依赖项和运行时参数,例如:“给我这个用户的订单 View Controller ”。在这种情况下,我们注入(inject)程序集本身。
对于 iOS,Typhoon 提供了一种在启动时 Bootstrap 集的方法,无论是否使用 Storyboard。 sample显示如何执行此操作,以及 this guide在 Storyboard上。
如果您在尝试上述资源后遇到另一个障碍,请提出另一个具体问题。
关于iOS Typhoon DI 框架替代 objective-c 中的单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54950554/