تعطيل مراقبة أداء Firebase

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

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

  • يمكنك تعطيل SDK لمراقبة الأداء عند إنشاء تطبيقك، مع خيار إعادة تمكينه في وقت التشغيل.

  • يمكنك إنشاء تطبيقك مع تمكين SDK لمراقبة الأداء ولكن لديك خيار تعطيله في وقت التشغيل باستخدام Firebase Remote Config.

  • يمكنك إلغاء تنشيط SDK لمراقبة الأداء بشكل كامل، مع عدم وجود خيار لتمكينه في وقت التشغيل.

قم بتعطيل مراقبة الأداء أثناء عملية إنشاء التطبيق الخاص بك

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

لتعطيل مراقبة الأداء أو إلغاء تنشيطها، يمكنك إضافة أحد المفتاحين إلى ملف قائمة الخصائص ( Info.plist ) لتطبيق Apple الخاص بك:

  • لتعطيل مراقبة الأداء، مع السماح لتطبيقك بتمكينها في وقت التشغيل، قم بتعيين firebase_performance_collection_enabled على false في ملف Info.plist الخاص بتطبيقك.

  • لإلغاء تنشيط مراقبة الأداء بشكل كامل، مع عدم وجود خيار لتمكينها في وقت التشغيل، قم بتعيين firebase_performance_collection_deactivated إلى true في ملف Info.plist الخاص بتطبيقك.

قم بتعطيل تطبيقك في وقت التشغيل باستخدام Remote Config

يتيح لك Firebase Remote Config إجراء تغييرات على سلوك تطبيقك ومظهره، لذا فهو يوفر طريقة مثالية للسماح لك بتعطيل مراقبة الأداء في المثيلات المنشورة لتطبيقك.

لتعطيل جمع بيانات مراقبة الأداء في المرة التالية التي يبدأ فيها تشغيل تطبيق Apple، استخدم رمز المثال الموضح أدناه. لمزيد من المعلومات حول استخدام Remote Config في تطبيق Apple، راجع استخدام Firebase Remote Config على أنظمة Apple الأساسية .

  1. تأكد من استخدام Remote Config في Podfile الخاص بك:

    pod 'Firebase/RemoteConfig'
    
  2. أضف ما يلي إلى أعلى ملف AppDelegate الخاص بتطبيقك:

    سويفت

    ملاحظة: منتج Firebase هذا غير متوفر على أنظمة التشغيل macOS، وMac Catalyst، وwatchOS.
    import FirebaseRemoteConfig
    

    ج موضوعية

    ملاحظة: منتج Firebase هذا غير متوفر على أنظمة التشغيل macOS، وMac Catalyst، وwatchOS.
    @import FirebaseRemoteConfig;
    
  3. في ملف AppDelegate الخاص بك، أضف التعليمة البرمجية التالية إلى عبارات launchOptions في طريقة المثيل application:didFinishLaunchingWithOptions: ::

    سويفت

    ملاحظة: هذا المنتج غير متوفر على أنظمة التشغيل 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()
    

    ج موضوعية

    ملاحظة: منتج 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 ، أو ملف تنفيذ آخر يستخدمه تطبيقك، أضف الكود التالي لجلب قيم التكوين عن بعد وتنشيطها:

    سويفت

    ملاحظة: منتج 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)")
      }
    }
    

    ج موضوعية

    ملاحظة: منتج 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: طريقة المثيل (بدلاً من ما هو موضح أعلاه لنفس طريقة المثيل):

    سويفت

    ملاحظة: منتج 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()
    

    ج موضوعية

    ملاحظة: منتج 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 .