简体   繁体   English

是什么让“FBAudienceNetwork”链接耗时 20 秒?

[英]What makes "FBAudienceNetwork" link took 20 seconds?

Recently I found out that the reason of taking ~20secs to link the project is because of "FBAudienceNetwork" SDK install by CocoaPods, as this image shows:最近我发现需要~20secs来链接项目的原因是因为CocoaPods安装了“FBAudienceNetwork”SDK,如下图所示:

在此处输入图片说明

You can clone the demo project here:您可以在此处克隆演示项目:

git@github.com:JohnnyTseng/FBDemoProject.git

This is a clean project that only contains "FBAudienceNetwork" installed by CocoaPods, and it'll take ~20secs to link the project.这是一个干净的项目,只包含 CocoaPods 安装的“FBAudienceNetwork”,链接项目需要大约 20 秒。

Steps to reproduce:重现步骤:

  1. Install "FBAudienceNetwork" using CocoaPods使用 CocoaPods 安装“FBAudienceNetwork”
  2. Build the app构建应用程序
  3. The linking time will take ~ 20secs (on MBPR 16", 16G, 8 Core)链接时间大约需要 20 秒(在 MBPR 16"、16G、8 核上)

The interesting thing is, if you install even 10+ libraries with CocoaPods, the linking time is still very fast, once you installed "FBAudienceNetwork", it took ~20secs for each incremental build.有趣的是,如果你用 CocoaPods 安装 10+ 个库,链接时间仍然非常快,一旦你安装了“FBAudienceNetwork”,每次增量构建需要大约 20 秒。

Does anyone know how this happens?有谁知道这是怎么发生的? I'm very curious but have not enough knowledge to dig into this issue我很好奇,但没有足够的知识来深入研究这个问题

Thank you!谢谢!

I have this problem too for quite some time and it is very annoying.我也有这个问题有一段时间了,很烦人。 I don't know a way to fix the linking time issue (maybe use the dynamic version of the framework, not static, but this means managing this dependency manually, which is a no-go for me), I think it is up to the FB team to fix.我不知道解决链接时间问题的方法(也许使用框架的动态版本,而不是静态版本,但这意味着手动管理此依赖项,这对我来说是行不通的),我认为这取决于FB团队来修复。

But there is a workaround - avoid linking FBAudienceNetwork at all.但是有一个解决方法 - 完全避免链接 FBAudienceNetwork。

This reduces the issue to only the development related to FB ads, which is luckily not that often.这将问题减少到仅与 FB 广告相关的开发,幸运的是这种情况并不常见。

  • create a separate target in XCode and Podfile在 XCode 和 Podfile 中创建一个单独的目标
  • don't specify FBAudienceNetwork dependency in Podfile不要在 Podfile 中指定 FBAudienceNetwork 依赖项
  • create preprocessor macros to exclude FBAudienceNetwork related code from compilation创建预处理器宏以从编译中排除 FBAudienceNetwork 相关代码
  • create a helper class that will encapsulate all FB related logic创建一个将封装所有 FB 相关逻辑的辅助类
  • use factory method with dummy ad helper使用带有虚拟广告助手的工厂方法

Some details:一些细节:

  • target目标

Suppose your fb-free target is FBDemoProject-no-fb .假设您的无 fb 目标是FBDemoProject-no-fb Then in Podfile :然后在Podfile

target 'FBDemoProject' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for FBDemoProject
  pod 'FBAudienceNetwork'
  # any other libs

end

target 'FBDemoProject-no-fb' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # any other libs

end
  • preprocessors:预处理器:

for usage in swift: Other swift flags -> -D NO_FB_AD用于 swift:其他 swift 标志 -> -D NO_FB_AD

for usage in obj-c or header files: Preprocessor macros -> NO_FB_AD=1用于 obj-c 或头文件:预处理器宏 -> NO_FB_AD=1

  • helpers:帮手:
protocol FbAd {
    func tryLoad()
    func isLoaded() -> Bool
    func tryShow(_ from: UIViewController?)
}

class FbAdFactory {
    static func create(
        _ placementId: String,
        _ didLoad: @escaping () -> (), //to be used in `FBInterstitialAdDelegate` methods - just to get an idea
        _ didClose: @escaping () -> (),
        _ onError: @escaping () -> ()
    ) -> NvFbAd {
        #if !NO_FB_AD
            return FbAdHelper(
                placementId,
                didLoad,
                didClose,
                onError)
        #else
            return FbAdDummy() //dummy implementation
        #endif
    }
}

Use conditional import in FbAdHelper :FbAdHelper使用条件导入:

//to avoid compiler errors
#if canImport(FBAudienceNetwork)
    import FBAudienceNetwork
#endif

#if !NO_FB_AD
class FbAdHelper: NSObject, FbAd, FBInterstitialAdDelegate {
    //here go encapsulated logic and delegate methods
}
#endif
  • then in your controller:然后在您的控制器中:

var fbAdHelper: NvFbAd!

//...

self.fbAdHelper = FbAdFactory.create("placementId",
            { },
            { },
            { }
        )

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

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