Firebase observeSingleEvent stays in memory

前端 未结 2 427
一整个雨季
一整个雨季 2021-02-05 04:20

My app uses firebase\'s observeSingleEventOfType quite a fair bit and I came to realise that my app\'s memory increase over time. I have commented out all my code except a test

相关标签:
2条回答
  • 2021-02-05 04:27

    I think this way it will help in xcode too. I have done in my android application.

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("BLAH_BLAH_STRING");
    
                // Attach a listener to read the data at our posts reference
                ref.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        // Do Some Stuff
                        ref.removeEventListener(this);
                        ref = null;
    
                    }
    
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
    

    Or in iOS , I guess they have different methods to be used.

    removeAllObservers and removeObserverWithHandle:

    Please try with the given two methods above.

    0 讨论(0)
  • 2021-02-05 04:45

    This is likely only happening because you are running on debug build. My results, after ~128 clicks on device with a debug build:

    As you can see, the vast majority of memory consumption is due to performance tools related to debugging. You can disable this feature if you'd like to confirm it, by editing your scheme:

    And then disabling backtrace recording:

    With it disabled, the same ~128 clicks shows essentially no growth:

    Granted, you may not want to leave that disabled, as you may find it useful for diagnosing crashes and other issues in your debug builds. It shouldn't be a problem in a release build though!

    For reference, this was the code I was running:

    import UIKit
    import FirebaseDatabase
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var label: UILabel!
        private var count: Int = 0
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        @IBAction func testBtnPressed(sender: AnyObject) {
            let db = FIRDatabase.database().reference()
            db.child("posts").observeSingleEventOfType(.Value) { [weak self] (snap: FIRDataSnapshot!) in
                guard let this = self else { return }
                this.count = this.count + 1
                this.label.text = "\(this.count)"
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题