إيقاف مراقبة أداء Firebase


للسماح للمستخدمين بالموافقة على استخدام Firebase Performance Monitoring أو رفضها، قد تحتاج إلى ضبط تطبيقك بحيث يمكنك تفعيل Performance Monitoring وإيقافها. قد تجد هذه الإمكانية مفيدة أيضًا أثناء تطوير التطبيق واختباره.

في ما يلي بعض الخيارات التي يجب أخذها في الاعتبار:

  • يمكنك إيقاف حزمة تطوير البرامج (SDK) لميزة "Performance Monitoring" عند إنشاء تطبيقك، مع خيار إعادة تفعيلها في وقت التشغيل.

  • يمكنك إنشاء تطبيقك مع تفعيل حزمة تطوير البرامج (SDK) لميزة Performance Monitoring، ولكن لديك خيار إيقافها في وقت التشغيل باستخدام Firebase Remote Config.

  • يمكنك إيقاف حزمة تطوير البرامج (SDK) لـ Performance Monitoring تمامًا، بدون خيار تفعيلها في وقت التشغيل.

إيقاف Performance Monitoring أثناء عملية التصميم لتطبيقك

أحد المواقف التي قد يكون فيها إيقاف ميزة Performance Monitoring أثناء عملية تصميم تطبيقك مفيدًا هو تجنُّب الإبلاغ عن بيانات الأداء من إصدار ما قبل الإصدار العلني من تطبيقك أثناء تطوير التطبيق واختباره.

لإيقاف ميزة Performance Monitoring أو إلغاء تفعيلها، يمكنك إضافة أحد المفتاحَين التاليَين إلى ملف قائمة المواقع (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.