Để cho phép người dùng chọn sử dụng hoặc không sử dụng Firebase Performance Monitoring, bạn có thể định cấu hình ứng dụng để có thể bật và tắt Performance Monitoring. Bạn cũng có thể thấy tính năng này hữu ích trong quá trình phát triển và thử nghiệm ứng dụng.
Sau đây là một số lựa chọn cần cân nhắc:
Bạn có thể tắt SDK Performance Monitoring khi tạo bản dựng ứng dụng, với tuỳ chọn bật lại SDK này trong thời gian chạy.
Bạn có thể tạo bản dựng ứng dụng với SDK Performance Monitoring đã bật nhưng có tuỳ chọn tắt SDK này trong thời gian chạy bằng Firebase Remote Config.
Bạn có thể hoàn toàn ngừng kích hoạt SDK Performance Monitoring mà không có tuỳ chọn bật SDK này trong thời gian chạy.
Tắt tính năng Performance Monitoring trong quy trình xây dựng ứng dụng
Một trường hợp mà việc tắt tính năng Performance Monitoring trong quy trình xây dựng ứng dụng có thể hữu ích là để tránh báo cáo dữ liệu hiệu suất từ phiên bản phát hành trước của ứng dụng trong quá trình phát triển và thử nghiệm ứng dụng.
Để tắt hoặc ngừng kích hoạt tính năng Performance Monitoring, bạn có thể thêm một trong hai khoá vào
tệp danh sách thuộc tính (Info.plist) cho ứng dụng Apple:
Để tắt Performance Monitoring, nhưng cho phép ứng dụng bật tính năng này trong thời gian chạy, hãy đặt
firebase_performance_collection_enabledthànhfalsetrong tệpInfo.plistcủa ứng dụng.Để ngừng kích hoạt hoàn toàn Performance Monitoring mà không có tuỳ chọn bật tính năng này trong thời gian chạy, hãy đặt
firebase_performance_collection_deactivatedthànhtruetrong tệpInfo.plistcủa ứng dụng.
Tắt ứng dụng trong thời gian chạy bằng Remote Config
Firebase Remote Config cho phép bạn thay đổi cách hoạt động và giao diện của ứng dụng. Vì vậy, đây là cách lý tưởng để bạn tắt tính năng Performance Monitoring trong các thực thể đã triển khai của ứng dụng.
Để tắt tính năng thu thập dữ liệu Performance Monitoring vào lần tiếp theo ứng dụng Apple của bạn khởi động, hãy sử dụng mã ví dụ bên dưới. Để biết thêm thông tin về cách sử dụng Remote Config trong ứng dụng Apple, hãy xem Sử dụng Firebase Remote Config trên các nền tảng của Apple.
Đảm bảo rằng Remote Config được sử dụng trong
Podfile:pod 'Firebase/RemoteConfig'Thêm nội dung sau vào đầu tệp
AppDelegatecủa ứng dụng:Swift
Lưu ý: Sản phẩm Firebase này không có trên các mục tiêu macOS, Mac Catalyst, watchOS.import FirebaseRemoteConfigObjective-C
Lưu ý: Sản phẩm Firebase này không có trên các mục tiêu macOS, Mac Catalyst, watchOS.@import FirebaseRemoteConfig;Trong tệp
AppDelegate, hãy thêm mã sau vào các câu lệnhlaunchOptionstrong phương thức thực thểapplication:didFinishLaunchingWithOptions::Swift
Lưu ý: Sản phẩm này không có trên các mục tiêu 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
Lưu ý: Sản phẩm Firebase này không có trên các mục tiêu 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];Trong
ViewController.mhoặc một tệp triển khai khác mà ứng dụng của bạn sử dụng, hãy thêm mã sau để tìm nạp và kích hoạt các giá trị Remote Config:Swift
Lưu ý: Sản phẩm Firebase này không có trên các mục tiêu 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
Lưu ý: Sản phẩm Firebase này không có trên các mục tiêu 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); } }];Để tắt Performance Monitoring trong bảng điều khiển Firebase, hãy tạo tham số perf_disable trong dự án của ứng dụng, sau đó đặt giá trị của tham số này thành
true.Nếu bạn đặt giá trị của perf_disable thành
false, Performance Monitoring vẫn bật.
Tắt tính năng thu thập dữ liệu tự động hoặc tuỳ chỉnh riêng biệt
Bạn có thể thực hiện một số thay đổi đối với mã được trình bày ở trên và trong bảng điều khiển Firebase để cho phép bạn tắt tất cả hoạt động giám sát tự động (có sẵn) riêng biệt với hoạt động giám sát tuỳ chỉnh.
Thêm mã sau vào các câu lệnh
launchOptionstrong phương thức thực thểapplication:didFinishLaunchingWithOptions:(thay vì nội dung được trình bày ở trên cho cùng một phương thức thực thể):Swift
Lưu ý: Sản phẩm Firebase này không có trên các mục tiêu 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
Lưu ý: Sản phẩm Firebase này không có trên các mục tiêu 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];Hoàn tất các bước sau trong bảng điều khiển Firebase:
- Để tắt tất cả hoạt động giám sát tự động (có sẵn), hãy tạo tham số
perf_disable_auto trong dự án của ứng dụng, sau đó đặt giá trị
của tham số này thành
true. - Để tắt tất cả hoạt động giám sát tuỳ chỉnh, hãy tạo tham số perf_disable_manual
trong dự án của ứng dụng, sau đó đặt giá trị của tham số này thành
true.
- Để tắt tất cả hoạt động giám sát tự động (có sẵn), hãy tạo tham số
perf_disable_auto trong dự án của ứng dụng, sau đó đặt giá trị
của tham số này thành