1 00:00:00,360 --> 00:00:08,340 We're finally ready to actually send our message, so I have this little stub, this little call to 2 00:00:08,340 --> 00:00:11,680 send message ready to go, which gets the data. 3 00:00:11,730 --> 00:00:15,510 So we've already written this, which gets the data on the text fields. 4 00:00:16,080 --> 00:00:19,050 What I'm going to do is paste in the send message. 5 00:00:19,140 --> 00:00:27,270 So pause and copy this if you need to, where we set up a packet, which is equal to the data and that 6 00:00:27,270 --> 00:00:35,310 packet, which is just basically the data we're sending to Firebase, we're going to add in constants 7 00:00:35,310 --> 00:00:36,240 date time. 8 00:00:36,600 --> 00:00:41,670 OK, so we're giving our FIREBASE database a date time that this was created. 9 00:00:42,060 --> 00:00:45,410 This is on the current device for this particular user. 10 00:00:45,810 --> 00:00:51,810 If I was going to run this, if I was going to create this in a commercial sense, I would use the date 11 00:00:51,810 --> 00:01:00,180 time of the server and just pick up the time zone of the user so I can add plus minus whatever it is, 12 00:01:00,360 --> 00:01:03,120 depending on who's viewing that time. 13 00:01:03,300 --> 00:01:07,830 OK, of course that depends if you actually want to do it that way or not. 14 00:01:08,400 --> 00:01:12,900 So what I have to do is just copy in my get date code from utilities. 15 00:01:15,690 --> 00:01:21,180 All I'm doing with the get date is formatting a date into a string. 16 00:01:21,180 --> 00:01:24,070 So let's copy this then you can have a look at that. 17 00:01:24,570 --> 00:01:30,090 This is just some standard code that I have written out in my my quick snippets. 18 00:01:30,480 --> 00:01:36,110 So when I need the dates in iOS, this is what I go for and I know how to do it quickly, instantly. 19 00:01:36,510 --> 00:01:42,960 In fact, with most apps, I just copy my utilities class because it's all of the things like show alert, 20 00:01:43,590 --> 00:01:48,510 get date and clear TextField that you're likely to use all the time. 21 00:01:49,170 --> 00:01:50,250 So utilities. 22 00:01:50,250 --> 00:01:51,810 Well, I've got tons of those. 23 00:01:52,910 --> 00:01:59,850 OK, so that's going to now insert the date time from utilities and then we're going to pick up this 24 00:01:59,850 --> 00:02:07,530 child messages on the database side in Firebase and we're going to say, I want you to create another 25 00:02:07,530 --> 00:02:13,380 child within messages, as in a data point that has an automatic ID. 26 00:02:13,530 --> 00:02:18,780 And I want you to set the value of that point to as the data that I'm passing you here. 27 00:02:19,630 --> 00:02:25,200 OK, so what I could actually do here is make all of this a bit more compact. 28 00:02:27,870 --> 00:02:28,740 Is that going to work? 29 00:02:30,180 --> 00:02:32,230 Oh, data is a let constant. 30 00:02:32,490 --> 00:02:35,400 That's why it's up here somewhere. 31 00:02:35,800 --> 00:02:39,110 Yeah, I said let see what we can do here instead of, say, far. 32 00:02:41,090 --> 00:02:42,890 And now the error should go away. 33 00:02:46,630 --> 00:02:47,960 I say it should go away. 34 00:02:48,580 --> 00:02:50,850 Let's just try and build it and see what happens, shall we? 35 00:02:54,200 --> 00:03:02,630 Something weird is happening here, so I'm going to revert all of this just to hit Command Z and now 36 00:03:02,630 --> 00:03:07,130 I see why I had packett, it's funny, as a developer, you look through your code and you think that's 37 00:03:07,130 --> 00:03:07,430 weird. 38 00:03:07,440 --> 00:03:10,540 You change it and you go, Oh, yeah, I remember what I did there now. 39 00:03:11,210 --> 00:03:13,820 So that's going to send the message. 40 00:03:14,240 --> 00:03:18,470 Now, there is a very important thing that we've missed out here, Arava. 41 00:03:18,500 --> 00:03:19,250 I've missed out. 42 00:03:19,910 --> 00:03:22,550 Is the D in it OK? 43 00:03:22,790 --> 00:03:31,850 Because and this is related to the pulling down of data when our view controller exits, we want to 44 00:03:31,850 --> 00:03:36,660 remove the reference to that particular child in the database. 45 00:03:36,690 --> 00:03:43,190 OK, I should have mentioned this when we actually set up the database because that's where it applies 46 00:03:43,190 --> 00:03:43,460 to. 47 00:03:43,760 --> 00:03:45,140 We're setting up an observer. 48 00:03:45,590 --> 00:03:50,360 When we remove this view controller, we want to get rid of the observer. 49 00:03:50,730 --> 00:03:55,400 OK, now back to sending things. 50 00:03:55,820 --> 00:04:00,950 So here we have send message and send message is called in. 51 00:04:00,950 --> 00:04:02,350 Text fields should return. 52 00:04:02,840 --> 00:04:07,910 So I'm going to run this in our neighborhood simulator. 53 00:04:10,850 --> 00:04:12,690 Let's see, what are we going to get? 54 00:04:14,520 --> 00:04:22,230 So we have all the data that we should have from before we scroll to the bottom and I'm going to say 55 00:04:22,230 --> 00:04:30,000 hi there, I'm going to hit enter, and that's the clue for it to populate in the database. 56 00:04:30,390 --> 00:04:38,730 Now, if we go over to our firebase console, if we look at the very last section, we will see our 57 00:04:38,730 --> 00:04:40,010 particular message. 58 00:04:40,530 --> 00:04:43,100 And now if I'm fast enough, let's move this here. 59 00:04:44,700 --> 00:04:49,560 If I type something random here and I'm going to press, enter and quickly change. 60 00:04:50,580 --> 00:04:57,420 See that green that appears that indicates that that is a new addition to the real time database. 61 00:04:57,990 --> 00:05:03,540 Now, this console that we're using is only really useful when the data is small like this. 62 00:05:03,540 --> 00:05:06,280 When the data expands, it's pretty useless. 63 00:05:06,480 --> 00:05:08,160 Let me tell you from experience. 64 00:05:08,850 --> 00:05:16,680 But that aside, we have now plumbed in the ability to send messages from our app as well as receive 65 00:05:16,680 --> 00:05:19,410 them and add them in to our table view.