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

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

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

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

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

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

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

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

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

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

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

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

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

หากต้องการปิดใช้การเก็บรวบรวมข้อมูลการตรวจสอบประสิทธิภาพในครั้งถัดไปที่แอป 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