如要讓使用者選擇啟用或停用 Firebase Performance Monitoring,建議您設定應用程式,以啟用及停用 Performance Monitoring。您可能也會發現這項功能在開發與測試應用程式期間非常實用。
以下是您可以考慮的選項:
您可以在建構應用程式時停用 Performance Monitoring SDK,並選擇在執行階段重新啟用。
您可以在啟用 Performance Monitoring SDK 的情況下建構應用程式,但可以選擇使用 Firebase 遠端設定在執行階段停用。
您可以完全停用 Performance Monitoring SDK,但無法在執行階段啟用。
在應用程式建構程序中停用 Performance Monitoring
在應用程式建構過程中,停用 Performance Monitoring 的其中一個做法是,避免在開發和測試期間回報預先發布版應用程式的效能資料。
如要停用或停用 Performance Monitoring,您可以將以下兩個金鑰加入 Apple 應用程式的屬性清單檔案 (Info.plist
) 中:
如要停用 Performance Monitoring,但允許應用程式在執行階段啟用,請在應用程式的
Info.plist
檔案中將firebase_performance_collection_enabled
設為false
。如要完全停用 Performance Monitoring,但無法選擇在執行階段啟用,請在應用程式的
Info.plist
檔案中將firebase_performance_collection_deactivated
設為true
。
利用遠端設定在執行階段停用應用程式
Firebase 遠端設定可讓您變更應用程式的行為和外觀,因此是可讓您在已部署應用程式執行個體中停用 Performance Monitoring 的理想方式。
如要在下次 Apple 應用程式啟動時停用 Performance Monitoring 資料收集功能,請使用下方程式碼範例。如要進一步瞭解如何在 Apple 應用程式中使用遠端設定,請參閱「在 Apple 平台上使用 Firebase 遠端設定」一文。
請確保在您的
Podfile
中使用遠端設定:pod 'Firebase/RemoteConfig'
請將以下內容新增至應用程式的
AppDelegate
檔案頂端:Swift
注意:這項 Firebase 產品不適用於 macOS、Mac Catalyst 和 watchOS 目標。import FirebaseRemoteConfig
Objective-C
注意:這項 Firebase 產品不適用於 macOS、Mac Catalyst 和 watchOS 目標。@import FirebaseRemoteConfig;
在
AppDelegate
檔案中,將下列程式碼新增至application:didFinishLaunchingWithOptions:
執行個體方法中的launchOptions
陳述式:Swift
注意:這項產品不適用於 macOS、Mac Catalyst 和 watchOS 目標。remoteConfig = RemoteConfig.remoteConfig() // You can change the "false" below to "true" to permit more fetches when validating // your app, but you should change it back to "false" or remove this statement before // distributing your app in production. let remoteConfigSettings = RemoteConfigSettings(developerModeEnabled: false) remoteConfig.configSettings = remoteConfigSettings! // Load in-app defaults from a plist file that sets perf_disable to false until // you update values in the Firebase console. remoteConfig.setDefaultsFromPlistFileName("RemoteConfigDefaults") // Important! This needs to be applied before FirebaseApp.configure() if !remoteConfig["perf_disable"].boolValue { // The following line disables all automatic (out-of-the-box) monitoring Performance.sharedInstance().isInstrumentationEnabled = false // The following line disables all custom monitoring Performance.sharedInstance().isDataCollectionEnabled = false } else { Performance.sharedInstance().isInstrumentationEnabled = true Performance.sharedInstance().isDataCollectionEnabled = true } // Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
注意:這項 Firebase 產品不適用於 macOS、Mac Catalyst 和 watchOS 目標。self.remoteConfig = [FIRRemoteConfig remoteConfig]; // You can change the NO below to YES to permit more fetches when validating // your app, but you should change it back to NO or remove this statement before // distributing your app in production. FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:NO]; self.remoteConfig.configSettings = remoteConfigSettings; // Load in-app defaults from a plist file that sets perf_disable to false until // you update values in the Firebase console. [self.remoteConfig setDefaultsFromPlistFileName:@"RemoteConfigDefaults"]; // Important! This needs to be applied before [FIRApp configure] if (!self.remoteConfig[@"perf_disable"].numberValue.boolValue) { // The following line disables all automatic (out-of-the-box) monitoring [FIRPerformance sharedInstance].instrumentationEnabled = NO; // The following line disables all custom monitoring [FIRPerformance sharedInstance].dataCollectionEnabled = NO; } else { [FIRPerformance sharedInstance].instrumentationEnabled = YES; [FIRPerformance sharedInstance].dataCollectionEnabled = YES; } // Use Firebase library to configure APIs [FIRApp configure];
在
ViewController.m
或應用程式所使用的其他實作檔案中,新增下列程式碼,以擷取並啟用遠端設定值:Swift
注意:這項 Firebase 產品不適用於 macOS、Mac Catalyst 和 watchOS 目標。//RemoteConfig fetch and activation in your app, shortly after startup remoteConfig.fetch(withExpirationDuration: TimeInterval(30.0)) { (status, error) -> Void in if status == .success { print("Config fetched!") self.remoteConfig.activateFetched() } else { print("Config not fetched") print("Error \(error!.localizedDescription)") } }
Objective-C
注意:這項 Firebase 產品不適用於 macOS、Mac Catalyst 和 watchOS 目標。//RemoteConfig fetch and activation in your app, shortly after startup [self.remoteConfig fetchWithExpirationDuration:30.0 completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) { if (status == FIRRemoteConfigFetchStatusSuccess) { NSLog(@"Config fetched!"); [self.remoteConfig activateFetched]; } else { NSLog(@"Config not fetched"); NSLog(@"Error %@", error.localizedDescription); } }];
如要在 Firebase 控制台中停用 Performance Monitoring,請在應用程式的專案中建立 perf_disable 參數,然後將參數的值設為
true
。如果您將 perf_disable 的值設為
false
,Performance Monitoring 會保持啟用狀態。
分別停用自動或自訂資料收集功能
您可以對上方和 Firebase 控制台中的程式碼進行變更,以便分別停用所有自動 (現成) 監控和自訂監控。
將下列程式碼加入
application:didFinishLaunchingWithOptions:
執行個體方法中的launchOptions
陳述式 (而非上述執行個體方法的上方所示內容):Swift
注意:這項 Firebase 產品不適用於 macOS、Mac Catalyst 和 watchOS 目標。remoteConfig = FIRRemoteConfig.remoteConfig() let remoteConfigSettings = FIRRemoteConfigSettings(developerModeEnabled: true) remoteConfig.configSettings = remoteConfigSettings! // Important! This needs to be applied before FirebaseApp.configure() if remoteConfig["perf_disable_auto"].boolValue { // The following line disables all automatic (out-of-the-box) monitoring Performance.sharedInstance().isInstrumentationEnabled = false } else { Performance.sharedInstance().isInstrumentationEnabled = true } if remoteConfig["perf_disable_manual"].boolValue { // The following line disables all custom monitoring Performance.sharedInstance().isDataCollectionEnabled = false } else { Performance.sharedInstance().isDataCollectionEnabled = true } // Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
注意:這項 Firebase 產品不適用於 macOS、Mac Catalyst 和 watchOS 目標。self.remoteConfig = [FIRRemoteConfig remoteConfig]; FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES]; self.remoteConfig.configSettings = remoteConfigSettings; // Important! This needs to be applied before [FirebaseApp configure] if (self.remoteConfig[@"perf_disable_auto"].numberValue.boolValue) { // The following line disables all automatic (out-of-the-box) monitoring [FIRPerformance sharedInstance].instrumentationEnabled = NO; } else { [FIRPerformance sharedInstance].instrumentationEnabled = YES; } if (self.remoteConfig[@"perf_disable_manual"].numberValue.boolValue) { // The following line disables all custom monitoring [FIRPerformance sharedInstance].dataCollectionEnabled = NO; } else { [FIRPerformance sharedInstance].dataCollectionEnabled = YES; } // Use Firebase library to configure APIs [FirebaseApp configure];
在 Firebase 控制台中完成下列步驟:
- 如要停用所有自動 (立即可用的) 監控功能,請在應用程式的專案中建立 perf_disable_auto 參數,然後將參數值設為
true
。 - 如要停用所有自訂監控功能,請在應用程式的專案中建立 perf_disable_manual 參數,然後將參數值設為
true
。
- 如要停用所有自動 (立即可用的) 監控功能,請在應用程式的專案中建立 perf_disable_auto 參數,然後將參數值設為