Kodeco Forums

Firebase Tutorial: Getting Started

In this Firebase tutorial, you'll learn about the fundamentals, including saving data, real-time sync, authentication, user status, and offline support.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1610-firebase-tutorial-getting-started
1 Like

Wonderful job!

Hope you upload some tutorials on multi-media processing about Firebase in swift.
Such as image/video upload/download, which is essential for a good app.

Nice tutorial David, very nice way to get started with Firebase. Would you have or would you know where I could find a very similar tutorial for Android?

Since you guys cover iOS and Android, I think it would be very beneficial to the community if you could provide your tutorials in SWIFT and Java (Android).

Thanks

Check out udacity. They have a full on FREE andorid-firebase course on a Shopping list app which is a lot more involved than this.

When ā€œObserving Authentication Stateā€ we just check if the authData is nil, the thing is that it already has data every time the app starts, so every time I re-build it automatically goes to the grocery list view, shouldnā€™t the app always ask for credentials? we are not saving any token or credentials on the device, so if authData is retrieved from Firebase at start, how does the Firebase know which user is asking for authentication? or am I missing something?

HI there!

This tutorial is exactly what I need, but Iā€™m getting errors and warnings when trying to compile the starter project. Am I the only one or is it because of the latest updates to OS X and Xcode (I have Version 7.3 (7D175))? Iā€™m a bit of a newbie here so bear with me.

There are 3 errors, all of which have the message:
Downcast from ā€˜UITableViewCell?ā€™ to ā€˜UITableViewCellā€™ only unwraps optionals; did you mean to use ā€˜!ā€™?

There are also 8 warnings but I didnā€™t really pay attention to themā€¦

Any help would be greatly appreciated :smiley:

Happy coding!

I think this is because the tutorial was written before Swift 2.0 was implemented within Xcode, so, just change the ā€œ?ā€ for ā€œ!ā€ which is what I did and it worked correctly

Iā€™m getting the following error ā€œthread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0)ā€ at line 44 in the GroceryItem struct file ā€œname = snapshot.value[ā€œnameā€] as! Stringā€ it says in the console that ā€œfatal error: unexpectedly found nil while unwrapping an Optional valueā€ any ideas? I just downloaded the complete project and it is not working neither

This didnā€™t seem to do the full trick for me. See my response below.

Hello!

These are the changes I made for the errors from Swift 1 to Swift 2 and also additional notes on the tutorial.

In GroceryListTableViewController.swift
For the warning in line 45, change the selector of userCountBarButtonItem from
userCountBarButtonItem = UIBarButtonItem(title: ā€œ1ā€, style: UIBarButtonItemStyle.Plain, target: self, action: Selector(ā€œuserCountButtonDidTouchā€))
to
userCountBarButtonItem = UIBarButtonItem(title: ā€œ1ā€, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(GroceryListTableViewController.userCountButtonDidTouch))

For the error in line 68, change the cell declaration from
let cell = tableView.dequeueReusableCellWithIdentifier(ā€œItemCellā€) as! UITableViewCell
to
let cell = tableView.dequeueReusableCellWithIdentifier(ā€œItemCellā€, forIndexPath: indexPath)

For the error in line 126 change the textField declaration in line 126 from
let textField = alert.textFields![0] as! UITextField
to
let textField = alert.textFields![0]

For the error in the following line (127) add ! in the groceryItem declaration:
let groceryItem = GroceryItem(name: textField.text, addedByUser: self.user.email, completed: false)
to
let groceryItem = GroceryItem(name: textField.text!, addedByUser: self.user.email, completed: false)

In OnlineUsersTableViewController.swift
Change cell declaration in line 46 to clear the error from Swift 1 to Swift 2 from
let cell = tableView.dequeueReusableCellWithIdentifier(UserCell) as! UITableViewCell
to
let cell = tableView.dequeueReusableCellWithIdentifier(UserCell, forIndexPath: indexPath)

Additionally, In LoginViewController.swift
I changed the alert in line 48 from var to let since it does seem to only have one value.
I also changed the emailField and passwordField declarations by removing ā€œas! UITextFieldā€ so that it looks like this
let emailField = alert.textFields![0]
let passwordField = alert.textFields![1]
but I donā€™t recall if this was absolutely necessary.

As for the tutorial itself, I have some notes:
In section Displaying a List of Online Users

  • The tutorial says to change viewDidAppear but there is no viewDidAppear code, do we add it or was viewDidLoad the intended function? (both seem to work)
  • In OnlineUsersTableViewController.swift there is no reference to usersRef like in GroceryListTableViewController.swift, yet the tutorial assumes you already do or you already imported it. So, does it need to be added in the same way we did in the other file or should it be imported? (I just added it)
  • For the last piece of code to be added in this section, change enumerate(self.currentUsers) and instead use self.currentUsers.enumerate() as the former gives error (I guess due to the Swift 2 changes).
  • The tutorial says ā€œUsing a bit of trickery, itā€™s possible to add a user to Onlineā€¦ā€ if this sounded a bit ambiguous as there is no explicit mention of what that trickery means, the actual trick is to press the + button in the online data tree

Lastly, in the section Enabling Offline
The code for AppDelegate.swift that weā€™re supposed to add is already added there in the starter project.

Hope this helps
Happy coding!

1 Like

Thanks, this removed most errors. I still canā€™t compile though, I get a ā€œlinker command failed with exit code 1ā€ error. Anyone know what I can do about this?

edit: never mind, I wasnā€™t working in the workspace :sweat_smile:

Hi,

I had the same problem and the way I solved it was making the totally installation of the Firebase pod from terminal.

-First, Xcode must be closed.

-Make all the installation of Firebase pod from Terminal:

cd your-xcode-project-directory
pod init
open -a Xcode Podfile

-Write the following in your Podfile

platform :ios, ā€œ8.0ā€
use_frameworks!
target ā€˜the name of your Xcode-projectā€™ do
pod ā€˜Firebaseā€™, ā€˜>= 2.5.0ā€™
end

-The run the following from Terminal to install pod

pod install

-Once time you have finished with the installation of Firebase launch Xcode from Terminal:

open your-project.xcworkspace

Remember: The key here is that after the installation of Firebase Pod you have to launch your project from terminal and not from Xcode.

I hope it help you

Regards

The tutorial works great (with some minor fixes from outdated syntax) and builds fine on the simulator, however, if you want to test it out on the iPhone make sure you do the following:

  • Go to Build Settings

  • Underneath Build Settings scroll down to Build Options

  • In Build Options, switch the Enable Bitcode setting to No

That should allow you to build and test the app on your iPhone,

I have a minor query about this tutorial; how come thereā€™s no way for users to sign-out? While the sign up and login function work fine, itā€™s very difficult to test without having to delete and re-install the app because thereā€™s absolutely no way for new users to sign-up as the build automatically segues directly to the Table View where the grocery list is.

The only way I have been able to get back into the LoginViewController to use the Sign Up function is if I build the app on different iOS Simulators (eg: iPhone 4s, 5, 5s etcā€¦). :thinking:

Thatā€™s expected behavior, when users sign in, they get their user stored in the app so they donā€™t need to sign in again.
What I did to complete the features and be able to change users is a Sign out button in the first screen. What this does is simply a Exit segue to the LoginViewController, which calls ref.unauth() and Firebase.goOffline() so the user gets erased and the online monitoring consider the user offline on the other running apps.

Check my answer below about sign out functionality

Trying to do this tutorial and I got to the first implementation of the first call to save an item and I get this:

2016-05-09 02:28:35.792 Grocr[26579:1592969] _BSMachError: (os/kern) invalid capability (20)
2016-05-09 02:28:35.792 Grocr[26579:1593630] _BSMachError: (os/kern) invalid name (15)

Anyone have any ideas why I am getting this?

I tried all the usual things - Google, removing line by line, adding line by line backā€¦ I get nothing!

Would love to do this tutorial for Firebase but it doesnā€™t seem to work anymore.

J

I have the same problem.

Have you find a solution?

Is there an updated version of this Tutorial since Swift 2.0 was released and Firebase is now powered by Google?

[Firebase] setValue: or removeValue: at /grocery-items/test failed: permission_denied

I canā€™t even add an Item, just added the ref line, and updated the save function and fixed the errorsā€¦ Is there a step Iā€™m missing?

Anyway you can update this tutorial with the new firebase release? Thanks!