菜鸟教程小白 发表于 2022-12-11 22:19:16

iOS Typhoon DI 框架替换 Objective-c 中的单例


                                            <p><p>我正在开发一个基于 Storyboard/objective-c 的 iOS 应用程序,并带有 firebase 身份验证。我使用 Cloud Firestore 来保存用户数据 - 年龄、性别等。当用户访问应用程序时,我会检查用户是否已登录以下(类似)代码</p>

<pre><code>FIRUser *firUser = .currentUser;
if (firUser) {
    // user logged in
    // fetch updated user date from cloud firestore
} else {
    // NO logged in user
}
</code></pre>

<p>当用户登录后,他们可以导航到应用程序的其他部分,否则他们会看到注册/登录页面。 </p>

<p>它的外观导航到不同的 View 通常意味着我必须调用上面的代码来再次确定登录状态——我不想这样做。我想使用登录用户和来自 firestore 的数据创建一个用户对象,并在 ViewController 之间传递它。 </p>

<p>Singleton 似乎做得很好,并且非常适合我的情况,但我遇到了 Typhoon! </p>

<p>第一个问题是,仍然可以使用该框架吗?似乎有点不活跃,但非常惊人的技术。 </p>

<p>其次是我的实现——我有一个看起来像这样的程序集</p>

<pre><code>- (AuthenticatedUser*)authenticatedUser {
    return class: configuration:^(TyphoonDefinition* definition){

      definition.scope = TyphoonScopeSingleton;
    }];
}
</code></pre>

<p>这就是我获取 AuthenticatedUser 实例的方式</p>

<pre><code>ModelsAssembly *modelsAssembly = ;

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

authenticatedUser = ;
</code></pre>

<p>要在不同的 View 中获得相同的启动类,我需要执行以下操作:</p>

<ol>
<li>在程序集中使用 TyphoonScopeSingleton 作为 definition.scope </li>
<li>将程序集设为默认 </li>
</ol>

<p>我想知道是否有人可以为此提供一些指导。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><blockquote>
<p>First question is, is it still ok to use that framework? Seems a little inactive, but very amazing technology though.</p>
</blockquote>
<p><strong>答案:</strong></p>
<p>Typhoon 仍然是 Objective-C 依赖注入(inject)库的最佳选择。它功能齐全,一般不会添加新功能,但由 AppsQuick.ly 维护和支持。</p>
<p>如果您正在使用 Swift,<a href="https://github.com/jkolb/FieryCrucible" rel="noreferrer noopener nofollow">Fiery Crucible</a>是一个优秀的DI框架。它具有 Typhoon 的大部分功能,使用简单,并且没有其他一些 Swift 框架的缺点。</p>
<blockquote>
<p>To obtain the same initiated class in different views seems like I need to do the following:</p>
<ol>
<li>use TyphoonScopeSingleton as definition.scope in the assembly</li>
<li>make the assembly default</li>
</ol>
<p>I am wondering if someone could provide me with some guidance regarding this.</p>
</blockquote>
<p><strong>答案:</strong></p>
<p>这不是正确的方法。想法是拥有 <em>一个</em> Typhoon 实例,在 <a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot/" rel="noreferrer noopener nofollow">composition root</a> 创建,然后它将在运行(前台或后台)应用程序的整个生命周期内与您的应用程序一起存在。</p>
<ul>
<li>我们不会向 Typhoon 询问依赖项,而是告诉它将依赖项注入(inject)到 Controller 、服务或其他类中。</li>
<li>唯一的异常(exception)是使用 <a href="https://github.com/appsquickly/typhoon/wiki/Types-of-Injections#injection-with-run-time-arguments" rel="noreferrer noopener nofollow">factory pattern</a> ,其中我们混合了静态依赖项以及运行时参数,例如:“给我这个用​​户的订单 ViewController ”。在这种情况下,我们注入(inject)程序集本身。</li>
</ul>
<p>对于 iOS,Typhoon 提供了一种在启动时 Bootstrap 集的方法,无论有没有 Storyboard。 <a href="https://github.com/appsquickly/Typhoon-example" rel="noreferrer noopener nofollow">sample</a>显示如何做到这一点,以及 <a href="https://github.com/appsquickly/typhoon/wiki/Storyboards" rel="noreferrer noopener nofollow">this guide</a>在 Storyboard上。</p>
<p>如果您在尝试上述资源后遇到另一个障碍,请提出另一个具体问题。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS Typhoon DI 框架替换 Objective-c 中的单例,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/54950554/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/54950554/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS Typhoon DI 框架替换 Objective-c 中的单例