ปิดใช้การตรวจสอบประสิทธิภาพของ Firebase


หากต้องการให้ผู้ใช้เลือกใช้หรือไม่ใช้ Firebase Performance Monitoring คุณอาจต้องกำหนดค่าแอปเพื่อให้เปิดใช้และปิดใช้ Performance Monitoring ได้ นอกจากนี้ คุณอาจพบว่าความสามารถนี้มีประโยชน์ในระหว่างการพัฒนาและการทดสอบแอปด้วย

ตัวเลือกบางส่วนที่ควรพิจารณามีดังนี้

  • คุณสามารถปิดใช้ SDK ของ Performance Monitoring เมื่อสร้างแอป โดยมีตัวเลือกให้เปิดใช้อีกครั้งที่รันไทม์

  • คุณสามารถสร้างแอปโดยเปิดใช้ SDK ของ Performance Monitoring แต่มีตัวเลือกให้ปิดใช้ SDK ดังกล่าวที่รันไทม์โดยใช้ Firebase Remote Config

  • คุณสามารถปิดใช้งาน SDK Performance Monitoring โดยสมบูรณ์ โดยไม่มีตัวเลือกให้เปิดใช้ SDK ดังกล่าวในรันไทม์

ปิดใช้ Performance Monitoring ระหว่างกระบวนการสร้างแอป

สถานการณ์หนึ่งที่การปิดใช้ Performance Monitoring ในระหว่างกระบวนการสร้างแอปอาจเป็นประโยชน์คือเพื่อหลีกเลี่ยงการรายงานข้อมูลประสิทธิภาพจากแอปเวอร์ชันทดลองในระหว่างการพัฒนาและการทดสอบแอป

หากต้องการปิดใช้หรือปิดใช้งาน Performance Monitoring คุณสามารถเพิ่มคีย์ใดคีย์หนึ่งจาก 2 คีย์ลงในไฟล์รายการพร็อพเพอร์ตี้ (Info.plist) สำหรับแอป Apple ได้ ดังนี้

  • หากต้องการปิดใช้ Performance Monitoring แต่อนุญาตให้แอปเปิดใช้ได้ที่รันไทม์ ให้ตั้งค่า firebase_performance_collection_enabled เป็น false ในไฟล์ Info.plist ของแอป

  • หากต้องการปิดใช้งาน Performance Monitoring โดยสมบูรณ์โดยไม่มีตัวเลือกให้เปิดใช้ขณะรันไทม์ ให้ตั้งค่า firebase_performance_collection_deactivated เป็น true ในไฟล์ Info.plist ของแอป

ปิดใช้แอปขณะรันไทม์โดยใช้ Remote Config

Firebase Remote Config ช่วยให้คุณเปลี่ยนแปลงลักษณะการทำงานและรูปลักษณ์ของแอปได้ จึงเป็นวิธีที่เหมาะอย่างยิ่งในการปิดใช้ Performance Monitoring ในอินสแตนซ์ที่ติดตั้งใช้งานของแอป

หากต้องการปิดใช้การเก็บรวบรวมข้อมูล Performance Monitoring ในครั้งถัดไปที่แอป Apple เริ่มทำงาน ให้ใช้โค้ดตัวอย่างที่แสดงด้านล่าง ดูข้อมูลเพิ่มเติมเกี่ยวกับการใช้ Remote Config ในแอป Apple ได้ที่หัวข้อใช้ Firebase Remote Config ในแพลตฟอร์ม Apple

  1. ตรวจสอบว่ามีการใช้ Remote Config ใน Podfile ของคุณ

    pod 'Firebase/RemoteConfig'
    
  2. เพิ่มโค้ดต่อไปนี้ที่ด้านบนของไฟล์ AppDelegate ของแอป

    Swift

    หมายเหตุ: ผลิตภัณฑ์ Firebase นี้ไม่พร้อมใช้งานใน macOS, Mac Catalyst, watchOS
    import FirebaseRemoteConfig
    

    Objective-C

    หมายเหตุ: ผลิตภัณฑ์ Firebase นี้ใช้ไม่ได้ใน macOS, Mac Catalyst, watchOS
    @import FirebaseRemoteConfig;
    
  3. ในไฟล์ AppDelegate ให้เพิ่มโค้ดต่อไปนี้ลงในคำสั่ง launchOptions ในเมธอดอินสแตนซ์ application:didFinishLaunchingWithOptions:

    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];
    
  4. ใน ViewController.m หรือไฟล์การติดตั้งใช้งานอื่นที่แอปใช้ ให้เพิ่มโค้ดต่อไปนี้เพื่อดึงข้อมูลและเปิดใช้งานค่า Remote Config

    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);
      }
    }];
    
  5. หากต้องการปิดใช้ Performance Monitoring ในคอนโซล Firebase ให้สร้างพารามิเตอร์ perf_disable ในโปรเจ็กต์ของแอป แล้วตั้งค่าเป็น true

    หากคุณตั้งค่า perf_disable เป็น false ระบบจะยังคงเปิดใช้ Performance Monitoring ไว้

ปิดใช้การเก็บรวบรวมข้อมูลอัตโนมัติหรือข้อมูลที่กำหนดเองแยกกัน

คุณสามารถทำการเปลี่ยนแปลงบางอย่างกับโค้ดที่แสดงด้านบนและในคอนโซล Firebase เพื่อให้คุณปิดใช้การตรวจสอบอัตโนมัติ (พร้อมใช้งานทันที) ทั้งหมดแยกจากการตรวจสอบที่กำหนดเองได้

  1. เพิ่มโค้ดต่อไปนี้ลงในคำสั่ง launchOptions ในเมธอดอินสแตนซ์ application:didFinishLaunchingWithOptions: (แทนสิ่งที่แสดงด้านบนสำหรับเมธอดอินสแตนซ์เดียวกัน)

    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];
    
  2. ดำเนินการต่อไปนี้ในคอนโซล Firebase ให้เสร็จสมบูรณ์

    • หากต้องการปิดใช้การตรวจสอบอัตโนมัติ (พร้อมใช้งานทันที) ทั้งหมด ให้สร้างพารามิเตอร์ perf_disable_auto ในโปรเจ็กต์ของแอป แล้วตั้งค่าเป็น true
    • หากต้องการปิดใช้การตรวจสอบที่กำหนดเองทั้งหมด ให้สร้างพารามิเตอร์ perf_disable_manual ในโปรเจ็กต์ของแอป แล้วตั้งค่าเป็น true