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


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

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

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

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

  • คุณสามารถปิดใช้งาน 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