<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/best-practices-for-the-ios-uiviewcontroller-and-firebase-guhsbzjvo" -->

---
title: Best practices for the iOS UIViewController and Firebase
description: Best practices for integrating Firebase with UIViewController lifecycle methods on iOS. Covers initializing Firebase references in viewDidLoad, using...
canonical: https://daily.dev/posts/best-practices-for-the-ios-uiviewcontroller-and-firebase-guhsbzjvo
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Best practices for the iOS UIViewController and Firebase | daily.dev
og:description: Best practices for integrating Firebase with UIViewController lifecycle methods on iOS. Covers initializing Firebase references in viewDidLoad, using...
og:url: https://daily.dev/posts/best-practices-for-the-ios-uiviewcontroller-and-firebase-guhsbzjvo
og:image: https://api.daily.dev/og/posts/GUHsBzjVO.png
og:image:alt: Best practices for the iOS UIViewController and Firebase
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Best practices for the iOS UIViewController and Firebase

**[Firebase](https://daily.dev/sources/firebase-blog)** · 6 min read · 0 upvotes · 0 comments

## Summary

Best practices for integrating Firebase with UIViewController lifecycle methods on iOS. Covers initializing Firebase references in viewDidLoad, using implicitly unwrapped optionals in Swift, creating listeners in viewWillAppear and removing them in viewDidDisappear using a FirebaseHandle, avoiding leaky listeners that waste bandwidth and memory, and enabling offline persistence in the AppDelegate's initializer.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://firebase.blog/posts/2015/10/best-practices-for-ios-uiviewcontroller_6>

## Questions this post answers

### Where should I initialize a Firebase reference in a UIViewController?

Initialize Firebase references in viewDidLoad rather than the initializer, because UIViewControllers created from a Storyboard don't have their initializer called. viewDidLoad runs exactly once and after outlets and properties are set, making it a reliable place for both static and dynamic reference creation in Swift or Objective-C.

_daily.dev surfaces practical iOS and Firebase integration patterns like this for developers wiring up data sync._

### When should I attach and remove a Firebase listener in a UIViewController to avoid leaks?

Attach the listener in viewWillAppear and remove it in viewDidDisappear using a stored FirebaseHandle, a UInt typealias that tracks the observer. This keeps data syncing only while the view is visible, preventing leaky listeners that waste bandwidth and memory, especially with UINavigationController where the root controller stays in memory.

_Developers debugging memory and battery issues in data-synced apps can find patterns like this on daily.dev._

### Why does enabling Firebase offline persistence in didFinishLaunchingWithOptions sometimes throw an exception?

It fails when a Firebase property is inlined in the root UIViewController, because that value gets set before application:didFinishLaunchingWithOptions runs, and offline persistence must be configured before any other Firebase code executes. Setting persistenceEnabled inside the AppDelegate's own initializer avoids the ordering issue entirely.

_daily.dev helps developers troubleshoot tricky initialization-order bugs like Firebase offline setup._

## Similar posts on daily.dev

- [Medium](https://daily.dev/posts/medium-znyx0jpph) · Medium · 0 upvotes · 0 comments
- [Using Flutter with Firebase: A Developer's Guide](https://daily.dev/posts/using-flutter-with-firebase-a-developer-s-guide-jbpavk3vl) · Flutter Community · 0 upvotes · 0 comments

---

Tags: [#ios](https://daily.dev/tags/ios), [#swift](https://daily.dev/tags/swift), [#firebase](https://daily.dev/tags/firebase), [#objective-c](https://daily.dev/tags/objective-c)

[View this post on daily.dev](https://daily.dev/posts/best-practices-for-the-ios-uiviewcontroller-and-firebase-guhsbzjvo)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Best practices for the iOS UIViewController and Firebase","url":"https://daily.dev/posts/best-practices-for-the-ios-uiviewcontroller-and-firebase-guhsbzjvo","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/best-practices-for-the-ios-uiviewcontroller-and-firebase-guhsbzjvo"},"datePublished":"2026-08-23T12:21:00.964Z","dateModified":"2026-08-23T12:44:43.740Z","description":"Best practices for integrating Firebase with UIViewController lifecycle methods on iOS. Covers initializing Firebase references in viewDidLoad, using...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/7ed721de96163685d26ab7e314be3ebc?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/7ed721de96163685d26ab7e314be3ebc?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Firebase","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Firebase","logo":"https://media.daily.dev/image/upload/s--i395oqZH--/f_auto,q_auto/v1787487448/logos/firebase-blog?_a=BAMAMicg0","url":"https://daily.dev/sources/firebase-blog"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/best-practices-for-the-ios-uiviewcontroller-and-firebase-guhsbzjvo","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"ios,swift,firebase,objective-c","timeRequired":"PT6M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Firebase","item":"https://daily.dev/sources/firebase-blog"},{"@type":"ListItem","position":3,"name":"Best practices for the iOS UIViewController and Firebase"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/best-practices-for-the-ios-uiviewcontroller-and-firebase-guhsbzjvo#faq","mainEntity":[{"@type":"Question","name":"Where should I initialize a Firebase reference in a UIViewController?","acceptedAnswer":{"@type":"Answer","text":"Initialize Firebase references in viewDidLoad rather than the initializer, because UIViewControllers created from a Storyboard don't have their initializer called. viewDidLoad runs exactly once and after outlets and properties are set, making it a reliable place for both static and dynamic reference creation in Swift or Objective-C. daily.dev surfaces practical iOS and Firebase integration patterns like this for developers wiring up data sync."}},{"@type":"Question","name":"When should I attach and remove a Firebase listener in a UIViewController to avoid leaks?","acceptedAnswer":{"@type":"Answer","text":"Attach the listener in viewWillAppear and remove it in viewDidDisappear using a stored FirebaseHandle, a UInt typealias that tracks the observer. This keeps data syncing only while the view is visible, preventing leaky listeners that waste bandwidth and memory, especially with UINavigationController where the root controller stays in memory. Developers debugging memory and battery issues in data-synced apps can find patterns like this on daily.dev."}},{"@type":"Question","name":"Why does enabling Firebase offline persistence in didFinishLaunchingWithOptions sometimes throw an exception?","acceptedAnswer":{"@type":"Answer","text":"It fails when a Firebase property is inlined in the root UIViewController, because that value gets set before application:didFinishLaunchingWithOptions runs, and offline persistence must be configured before any other Firebase code executes. Setting persistenceEnabled inside the AppDelegate's own initializer avoids the ordering issue entirely. daily.dev helps developers troubleshoot tricky initialization-order bugs like Firebase offline setup."}}]}
```

