{"id":18,"date":"2022-01-28T02:48:30","date_gmt":"2022-01-28T02:48:30","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/born2code\/?p=18"},"modified":"2022-01-28T02:51:22","modified_gmt":"2022-01-28T02:51:22","slug":"firebase-and-ios","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/born2code\/2022\/01\/28\/firebase-and-ios\/","title":{"rendered":"Firebase and iOS"},"content":{"rendered":"\n<p>When it comes down to it, mobile apps are all about data. Displaying data, manipulating data, adding data, and so on. For the iOS Bike Kollective app, that data involves users and bikes. In crafting our project plan, our group decided to store this data in a cloud utilizing <a href=\"https:\/\/firebase.google.com\/\">Google&#8217;s Firebase<\/a>.<br><\/p>\n\n\n\n<p>As I am implementing the List View for the app this week, I will need to find a way to retrieve and display bike data using a <a href=\"https:\/\/developer.apple.com\/design\/human-interface-guidelines\/ios\/views\/tables\/\">Table View<\/a>. That means understanding how Firebase and Swift can work together.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Getting Firebase Up and Running<br><\/h2>\n\n\n\n<p>Once the Firebase NoSQL database is set up, the proper Firebase SDKs will need to be in place in order for our app can interact with the database. That means installing the proper Swift Package Dependencies, such as the <a href=\"https:\/\/github.com\/firebase\/firebase-ios-sdk\">Firebase iOS SDK<\/a> via Cocoapods. <br><\/p>\n\n\n\n<p>Once that is complete, we will need to ensure Firebase is imported into the relevant Swift files, in particular, AppDelegate.swift. Then, an instance of FirebaseApp will need to be declared and configured using the following method in AppDelegate.swift:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">FirebaseApp.configure() \n<\/pre>\n\n\n\n<p>While Firebase is set up, our database will be running as a Cloud Firestore database. To get this set up, I will follow the various steps as outlined by the <a href=\"https:\/\/firebase.google.com\/docs\/firestore\/quickstart#ios+\">Firebase documentation<\/a>.<br><\/p>\n\n\n\n<p>To sum up, I&#8217;ll need to add Cloud Firestore as a pod to the Podfile and then add the following line to AppDelegate.swift:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let db = Firestore.firestore() \n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Retrieving Firebase Data<br><\/h2>\n\n\n\n<p>With Firebase and the Cloud Firestore database properly connected to the app, I will need to code the means of retrieving bike data from the database.<br><\/p>\n\n\n\n<p>Luckily, Google has provided a code example on Github providing a general outline of how this process may work <a href=\"https:\/\/github.com\/firebase\/snippets-ios\/blob\/6e0f6fb63dbd97f61c62522e09ffa16ce580fb9f\/firestore\/swift\/firestore-smoketest\/ViewController.swift#L156-L168\">here<\/a>. I&#8217;d first need to declare an instance of the database as so:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var db: Firestore!\n<\/pre>\n\n\n\n<p>And then adding the following line in the viewDidLoad() function:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db = Firestore.firestore()\n<\/pre>\n\n\n\n<p>Firebase refers to tables as Collections and entries as Documents. For the bike data, I would then need to access the Bike Collection to display individual Bike Documents. <br><\/p>\n\n\n\n<p>This means running a method called getDocuments() on the collection and then iterating through it to apply the relevant data from each Bike Document to each Table View Cell as such:<br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">db.collection(\"bikes\").getDocuments() {(snapshot, err) in \n    if let err = err {\n        print(\"Error getting documents: \\(err)\")\n    } else {\n        for document in snapshot!.documents {\n            \/\/ Add data to objects in Table View Cells\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>As we can see, this also provides the iteration needed to apply the retrieved data to each individual Table View Cell.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<br><\/h2>\n\n\n\n<p>With the above in mind, I can now work on simply implementing the above code into the relevant Swift files. While I will need to figure out the specifics of inserting the data into the various objects on the storyboard, I can at least rest easy that the Firebase data can be retrieved.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When it comes down to it, mobile apps are all about data. Displaying data, manipulating data, adding data, and so on. For the iOS Bike Kollective app, that data involves users and bikes. In crafting our project plan, our group decided to store this data in a cloud utilizing Google&#8217;s Firebase. As I am implementing&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.oregonstate.edu\/born2code\/2022\/01\/28\/firebase-and-ios\/\">Continue reading <span class=\"screen-reader-text\">Firebase and iOS<\/span><\/a><\/p>\n","protected":false},"author":11919,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/born2code\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/born2code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/born2code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/born2code\/wp-json\/wp\/v2\/users\/11919"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/born2code\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":5,"href":"https:\/\/blogs.oregonstate.edu\/born2code\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/blogs.oregonstate.edu\/born2code\/wp-json\/wp\/v2\/posts\/18\/revisions\/24"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/born2code\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/born2code\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/born2code\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}