xcode iOS Flurry:从后台线程调用的 UI API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45183278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:08:34  来源:igfitidea点击:

iOS Flurry: UI API called from background thread

iosxcodeflurrybeta

提问by RSN

Implementing flurry using xcode 9.3 beta causes warning about UI API called on background thread. Must be called from main thread only.

使用 xcode 9.3 beta 实现flurry 会导致有关在后台线程上调用的 UI API 的警告。只能从主线程调用。

Any idea what to do to avoid this - is it only for flurry to solve?

知道如何避免这种情况 - 是否只是为了解决混乱?

Code used in app delegate:

应用程序委托中使用的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let builder = FlurrySessionBuilder.init()
                                      .withAppVersion("1.0")
                                      .withLogLevel(FlurryLogLevelAll)
                                      .withCrashReporting(true)
                                      .withSessionContinueSeconds(10)

    // Replace YOUR_API_KEY with the api key in the downloaded package
    Flurry.startSession("YOUR_API_KEY", with: builder)
    return true
}

回答by KKRocks

Try this :

尝试这个 :

Objective C

目标C

dispatch_async(dispatch_get_main_queue(), ^{
 // add UI related changes here
    });

Swift

迅速

DispatchQueue.main.async {
// add UI related changes here
}

回答by Subramanian P

UI operation should not happen on the background thread. It should be on main thread.

UI 操作不应发生在后台线程上。它应该在主线程上。

Move your UI update codes inside the main queue. You can use NSOperationQueueor GCD. NSOperationQueuevs GCD

将您的 UI 更新代码移到主队列中。您可以使用NSOperationQueueGCDNSOperationQueue对比GCD

NSOperationQueue

NSOperationQueue

Objective C:

目标 C:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
        // UI update code here.
}];

Swift

迅速

OperationQueue.main.addOperation { 
    // UI Update code here
}

GCD

大中华区

Objective C

目标C

dispatch_async(dispatch_get_main_queue(), ^{
       // UI update code here.
});

Swift

迅速

DispatchQueue.main.async {
    // UI Update code here
}

回答by RSN

The developers of the API are promising to make updates making it not use background threads when iOS 11 goes live.

API 的开发人员承诺进行更新,使其在 iOS 11 上线时不使用后台线程。