ios - Firebase: when to call removeObserverWithHandle in swift -


documentation says need call observeeventtype:withblock remove observer if no longer need it.

i've seen samples called within viewdiddisappear. find obj-c code called method within deinit, not ncessary in swift.

in simple app, however, want data synced long in app. if case, have call observeeventtype:withblock ever?

i checked chat-swift sample code on firebase website, , did not find observeeventtype:withblock.

does mean it's ok not call observeeventtype:withblock:. if want observer on when app in use?

thank you.

update

thanks jay , david. see makes sense observe in viewwillappear , remove in viewdiddisappear.

however, using observeeventtype monitor value change node , update ui if there any. if put in viewwillappear:

 override func viewwillappear(animated: bool) {     super.viewwillappear(animated)      ref.observeeventtype(.value, withblock: { snap in {       // **update ui if there value change**      })   } 

the problem putting in viewwillappear that, gets called every time view appears, regardless of value change or not. because of this, snapshot downloaded , ui gets refreshed every time return view. becomes counterproductive.

i have tried childadded/childremoved, however, returns last node, not path ref:

for instance, if add ref/child1/child2/child3/value, childadded return child3/value.

so if have observe value, seems putting in viewdidload better? in way, gets snapshot 1 time when view loaded, , repeat whenever there change, not obtain snapshot because view appears.

to build upon @jay's excellent answer:

in uiviewcontroller, create reference property. initialize reference in viewdidload. observe events in viewwillappear. remove observers in viewdiddisappear.

class myviewcontroller: uiviewcontroller {    var ref: firebase!    // called on load, great place initialize   override func viewdidload() {     super.viewdidload()     ref = firebase(url: "https://<your-firebase-app>.firebaseio.com/updates")   }    // can called many times go on screen   // syncing should occur when on view conserve memory   override func viewwillappear(animated: bool) {     super.viewwillappear(animated)      ref.observeeventtype(.value, withblock: { snap in {       // data      })   }    // can called many times off screen   // remove observers on ref conserve memory   override func viewdiddisappear(animated: bool) {     super.viewdiddisappear(animated)     ref.removeallobservers()    }  } 

per edit:

the problem putting in viewwillappear that, gets called every time view appears, regardless of value change or not. because of this, snapshot downloaded , ui gets refreshed every time return view. becomes counterproductive. 

firebase built speed. these kind of things leave client because has several features handle these situations.

the firebase client has built-in caching. unless you're downloading megabyte of data in viewdidappear update nominal. when observer fires on viewdidappear doesn't mean it's downloading data again. viewdidappear function observers belong.

fyi, firebase employee works on ios.


Comments