1 00:00:00,390 --> 00:00:06,870 It's time to start implementing Firebase in Fury, and we're going to do all of this inside of The View 2 00:00:06,870 --> 00:00:07,380 controller. 3 00:00:07,410 --> 00:00:13,170 Now, if you are making a production app, a commercial app of some sort, then you probably want to 4 00:00:13,170 --> 00:00:20,160 abstract the firebase stuff away into its own class and then listen to that class from the particular 5 00:00:20,160 --> 00:00:22,800 view controller you're looking at for two reasons. 6 00:00:22,860 --> 00:00:26,310 One, so it's maintainable, you know what you're changing and where. 7 00:00:26,730 --> 00:00:34,080 And two, you don't confuse your view controller code or have to repeat your code when you create another 8 00:00:34,080 --> 00:00:37,120 view controller that displays the data slightly differently. 9 00:00:37,350 --> 00:00:40,050 You just want to access one class for your data. 10 00:00:40,680 --> 00:00:43,760 OK, so we are not going to reinvent the wheel. 11 00:00:44,070 --> 00:00:46,770 We're going to look at the way that I've done this before. 12 00:00:47,550 --> 00:00:51,360 So we need, first of all, to declare three objects. 13 00:00:54,390 --> 00:01:03,330 The first object is a variable for messages and we're going to get a data snapshot array from Firebase. 14 00:01:03,600 --> 00:01:05,580 That's a snapshot of the data. 15 00:01:05,820 --> 00:01:12,360 At a particular point in time, we are going to get a database reference from Firebase. 16 00:01:12,900 --> 00:01:14,460 That's pretty self-explanatory. 17 00:01:14,730 --> 00:01:21,330 And we're going to get a database handle on the database objects so we can go and access that database 18 00:01:21,330 --> 00:01:22,110 whenever we need to. 19 00:01:22,950 --> 00:01:30,750 OK, and that's giving us an error because we need to import these two items because we only have Firebase 20 00:01:30,750 --> 00:01:32,140 Auth in here right now. 21 00:01:32,910 --> 00:01:36,150 OK, so we've got our database ready to go now. 22 00:01:36,150 --> 00:01:37,840 We need to go and set that up. 23 00:01:38,400 --> 00:01:44,790 So the first thing that we're going to do is create a method called a set up firebase. 24 00:01:45,130 --> 00:01:47,130 So I'm just going to copy this straight over. 25 00:01:50,130 --> 00:01:59,910 Just below did load and then I'm going to call that down here now, in the end, this might have an 26 00:01:59,910 --> 00:02:04,140 effect on our UI because we're checking the user and setting up Firebase. 27 00:02:04,680 --> 00:02:09,870 We might want to set up Firebase only if the current user is logged in. 28 00:02:10,090 --> 00:02:12,150 So that's a problem we can fix later on. 29 00:02:12,420 --> 00:02:15,900 But for now, our app is set up, so our user is always logged in. 30 00:02:16,300 --> 00:02:19,850 OK, so we can worry about that little bug later on. 31 00:02:20,130 --> 00:02:24,810 And I only know that's a bug because when I was preparing the update for this course, I discovered 32 00:02:24,810 --> 00:02:25,530 my own bug. 33 00:02:26,490 --> 00:02:26,860 Right. 34 00:02:26,880 --> 00:02:35,640 So in our set up firebase and post this, if you need to copy the code, we have that reference which 35 00:02:35,640 --> 00:02:38,940 we get from the FIREBASE database object. 36 00:02:39,480 --> 00:02:41,870 And the current instance of that object is here. 37 00:02:42,510 --> 00:02:45,000 And then we grab the reference of that. 38 00:02:46,050 --> 00:02:50,670 Then we have a reference handle on a particular part of that database. 39 00:02:51,150 --> 00:02:52,560 So we need the ref for that. 40 00:02:53,100 --> 00:02:57,680 And we tell it, I want you to go and look at the child called messages. 41 00:02:58,260 --> 00:03:00,690 So in the firebase database. 42 00:03:00,690 --> 00:03:03,420 And I don't think I actually have any here. 43 00:03:03,840 --> 00:03:04,730 Not yet anyway. 44 00:03:06,480 --> 00:03:09,780 We are going to have a sub child called messages now. 45 00:03:10,510 --> 00:03:16,080 Important distinction if you've done SQL database before or any kind of databases. 46 00:03:16,530 --> 00:03:21,020 Firebase is based on a document database like Mongo, for example. 47 00:03:21,830 --> 00:03:24,530 In fact, I think it does use Mongar, but don't quote me on that. 48 00:03:25,320 --> 00:03:28,230 That is different to how we structure other databases. 49 00:03:28,440 --> 00:03:31,890 So don't think of a child as a table. 50 00:03:32,010 --> 00:03:35,130 It's not a table of messages, it's just a subcategory. 51 00:03:35,550 --> 00:03:42,090 The subcategories are very, very limited in the number of them you can implement in Firebase. 52 00:03:42,090 --> 00:03:44,700 So don't arrange your data that way. 53 00:03:45,270 --> 00:03:46,340 I'm doing that here. 54 00:03:46,560 --> 00:03:48,570 So our app looks clear. 55 00:03:49,260 --> 00:03:54,600 A lot of people would be tempted to go, you know, create a table for messages, create a table for 56 00:03:54,600 --> 00:03:57,930 messages between these two people and these two people and these two people. 57 00:03:58,110 --> 00:04:02,400 And you could see how that's going to expand crazily over time. 58 00:04:02,730 --> 00:04:04,140 So you don't want to do it like that. 59 00:04:04,320 --> 00:04:10,830 Instead, in the table of messages, you put everybody's message, you get your authentication rules 60 00:04:10,830 --> 00:04:14,670 right so everyone can access everyone else's messages just their own. 61 00:04:15,480 --> 00:04:17,580 And what's the final thing you have to do? 62 00:04:17,730 --> 00:04:24,900 If you want to categorize the messages, you add an object into each message document so we know what 63 00:04:24,900 --> 00:04:26,160 category that's in. 64 00:04:26,850 --> 00:04:30,360 So that's just a warning for you from someone who's done it the wrong way before. 65 00:04:32,020 --> 00:04:32,870 Then what do we do? 66 00:04:33,130 --> 00:04:39,520 We're going to add an observer, so whenever this changes, Firebase will pay us a message and say it's 67 00:04:39,520 --> 00:04:40,050 changed. 68 00:04:40,570 --> 00:04:44,280 What kind of things are we going to listen to or we're only going to listen to? 69 00:04:44,290 --> 00:04:49,310 Child added as in someone's added a new message to the stack. 70 00:04:49,960 --> 00:04:56,350 Now, of course, you can customize this because you don't want everyone to be listening for a child 71 00:04:56,350 --> 00:04:59,680 added when it's a private conversation between two people. 72 00:05:00,010 --> 00:05:04,090 I'm just doing this quick and dirty for us so we can understand what's going on. 73 00:05:04,180 --> 00:05:05,950 But you can, of course, customize that. 74 00:05:06,760 --> 00:05:08,590 There are other ways. 75 00:05:09,310 --> 00:05:12,400 Child added, child change, child moved, a child removed. 76 00:05:12,880 --> 00:05:15,670 OK, so lots of things you can listen to. 77 00:05:17,020 --> 00:05:23,620 Then if that event fires off, it's going to return a data snapshot from Firebase. 78 00:05:24,190 --> 00:05:28,690 At that point, we're going to get that data snapshot and add it to our messages object. 79 00:05:30,710 --> 00:05:36,950 Now, this is only going to give us the difference, it's not going to send over the whole data table, 80 00:05:36,950 --> 00:05:39,650 if you like, it's just going to send us the difference. 81 00:05:39,650 --> 00:05:46,640 The one that was added, if you have if you're listening to something like child changed, then you're 82 00:05:46,640 --> 00:05:52,970 going to have to have a method that finds that one in your list of messages replaces it with the new 83 00:05:52,970 --> 00:05:53,420 data. 84 00:05:53,570 --> 00:05:54,920 OK, so that's just a warning. 85 00:05:55,160 --> 00:06:00,050 But what we're doing here is just appending because it's the easiest way of doing things and showing 86 00:06:00,050 --> 00:06:06,230 you and keeping you interested, then what we're going to do is go to the table view and we're going 87 00:06:06,230 --> 00:06:11,990 to insert a row at the current index paff. 88 00:06:13,490 --> 00:06:13,760 End. 89 00:06:14,360 --> 00:06:23,420 OK, so this code gets the index path and it finds the end cell and we do the minus one because of the 90 00:06:23,420 --> 00:06:29,840 zero based indexing there and it inserts that particular row. 91 00:06:30,310 --> 00:06:33,680 OK, and then down here is just a nice little thing. 92 00:06:33,680 --> 00:06:39,170 You don't need it, but it will automatically scroll the table up to the end of that row. 93 00:06:39,380 --> 00:06:45,440 So when you get a message, just like in a message or WhatsApp, it scrolls to the bottom automatically. 94 00:06:45,990 --> 00:06:49,690 OK, so that's just a nice little thing now. 95 00:06:50,000 --> 00:06:51,740 So we've we've set up Firebase. 96 00:06:51,740 --> 00:06:53,890 We've got all that reference. 97 00:06:54,380 --> 00:06:57,350 Currently, there is no data in Firebase. 98 00:06:57,650 --> 00:07:04,430 So what I'm going to do is, first of all, actually scrolling down the code, we're not going to see 99 00:07:04,430 --> 00:07:12,440 anything away because the table view just has 100 items and the table view just returns a bog standard. 100 00:07:12,440 --> 00:07:13,970 So we haven't changed it yet. 101 00:07:14,450 --> 00:07:17,390 So let's return the number of rows in section. 102 00:07:17,900 --> 00:07:19,550 Let's see what I put in the code here. 103 00:07:21,740 --> 00:07:24,080 Just the messages don't count. 104 00:07:26,320 --> 00:07:31,040 OK, and that will automatically update if we have more messages. 105 00:07:32,290 --> 00:07:40,270 Now, I'm going to copy this code that's going to implement our cell faurot act. 106 00:07:40,270 --> 00:07:43,240 So feel free to pause and copy this code. 107 00:07:46,120 --> 00:07:51,220 Right, so first of all, we're going to grab the message, then we're going to get the message content 108 00:07:51,400 --> 00:07:56,920 from the FIREBASE database as a dictionary of string and string. 109 00:07:56,950 --> 00:07:58,200 That's just how it comes out. 110 00:08:00,220 --> 00:08:06,790 Then we say, if you can get some text from those message fields, what message fields? 111 00:08:07,030 --> 00:08:10,320 Well, we haven't actually set that up, so that's what we should do now. 112 00:08:10,330 --> 00:08:13,870 We should have constant stop message fields set up. 113 00:08:14,110 --> 00:08:17,070 And in my other code here, here we have it. 114 00:08:17,380 --> 00:08:20,800 So I'm just going to copy this, paste it and explain. 115 00:08:21,580 --> 00:08:31,430 All I'm doing is setting a file with some constants so we don't mess up what things are called. 116 00:08:33,670 --> 00:08:37,450 So it's going to go, hey, if I want the date time, it knows to use the string. 117 00:08:37,450 --> 00:08:39,430 If I want the text, it knows to use this string. 118 00:08:39,670 --> 00:08:47,680 And these are the fields that we're going to use in Firebase to define the data that we're going to 119 00:08:47,680 --> 00:08:48,220 pull through. 120 00:08:48,880 --> 00:08:52,810 So once I've put that in, our silly error should go away here. 121 00:08:54,730 --> 00:08:56,530 You can find it, oh, I know you can. 122 00:08:59,290 --> 00:09:01,590 What if I press play it should find it then. 123 00:09:02,380 --> 00:09:03,190 Yeah, there we go. 124 00:09:03,280 --> 00:09:04,660 It's found it. 125 00:09:05,890 --> 00:09:06,660 Yes, it has. 126 00:09:08,470 --> 00:09:16,300 OK, so that's of doing, going and building itself and where we have our app again with an empty list, 127 00:09:16,540 --> 00:09:19,900 because it's pulled nothing down from Firebase yet. 128 00:09:20,290 --> 00:09:25,510 But if I insert some dummy data, then we're going to see some messages coming through.