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

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

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

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

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

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

ปิดใช้การตรวจสอบประสิทธิภาพระหว่างขั้นตอนการสร้างแอป

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

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

  • หากต้องการปิดใช้การตรวจสอบประสิทธิภาพ แต่อนุญาตให้แอปเปิดใช้ขณะรันไทม์ ให้ตั้งค่า firebase_performance_collection_enabled เป็น false ในไฟล์ Info.plist ของแอป

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

ปิดใช้แอปขณะรันไทม์โดยใช้การกำหนดค่าระยะไกล

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

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

  1. ตรวจสอบว่าใช้การกำหนดค่าระยะไกลใน 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 หรือไฟล์การใช้งานอื่นที่แอปของคุณใช้ ให้เพิ่มโค้ดต่อไปนี้เพื่อดึงข้อมูลและเปิดใช้งานค่าการกำหนดค่าระยะไกล

    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. หากต้องการปิดการตรวจสอบประสิทธิภาพในคอนโซล Firebase ให้สร้างพารามิเตอร์ perf_disable ในโปรเจ็กต์ของแอป แล้วกำหนดค่าเป็น true

    หากคุณตั้งค่า perf_disable เป็น false การตรวจสอบประสิทธิภาพจะยังคงเปิดใช้ต่อไป

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

คุณสามารถเปลี่ยนแปลงโค้ดที่แสดงด้านบนและในคอนโซล 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