1 00:00:01,240 --> 00:00:03,460 Well we've got a 50 percent solution to our problem. 2 00:00:03,460 --> 00:00:08,230 So in this video I want to explain what the remaining problem is like the remaining 50 percent and then 3 00:00:08,230 --> 00:00:09,580 describe how to fix it. 4 00:00:09,580 --> 00:00:11,710 So as usual a quick diagram. 5 00:00:11,710 --> 00:00:14,740 So here's what's now going inside of use location. 6 00:00:14,740 --> 00:00:19,390 The very first time we call it are using fact took its ran inside there we call start watching and of 7 00:00:19,390 --> 00:00:24,370 course inside of start watching at some point time we call watch position async right here. 8 00:00:24,550 --> 00:00:29,800 So that essentially sets up some listener in our application something and says Hey Expo tell me whenever 9 00:00:29,980 --> 00:00:35,600 are users location changes we can then imagine that at some point time use location gets called a second 10 00:00:35,600 --> 00:00:40,250 time maybe this time there's no change in the callback because maybe it was not the recording piece 11 00:00:40,250 --> 00:00:41,570 of state that changed. 12 00:00:41,570 --> 00:00:46,920 So maybe use effect won't be called that particular time maybe then it gets re rendered or called a 13 00:00:46,920 --> 00:00:50,840 third time and maybe during this time the callback function does change. 14 00:00:50,880 --> 00:00:56,240 So use effectively then run a second time and inside there of course we will call start watching. 15 00:00:56,340 --> 00:00:59,640 Now here's the issue we have now called start watching twice. 16 00:01:00,240 --> 00:01:05,160 And between those two calls to it we have invoked watch position async twice. 17 00:01:05,400 --> 00:01:11,310 So I don't necessarily know what Expo location does behind the scenes but without a doubt we have essentially 18 00:01:11,310 --> 00:01:16,690 told it that we want to watch for changes in the user's location two separate times. 19 00:01:16,740 --> 00:01:20,730 So does that mean that we're going to essentially get two separate updates. 20 00:01:20,730 --> 00:01:24,690 I don't know but it could possibly mean that's the case it might mean that every single time that we 21 00:01:24,690 --> 00:01:29,130 call watch position async we're always going to say give me another listener. 22 00:01:29,130 --> 00:01:31,100 Another listener another listener. 23 00:01:31,100 --> 00:01:35,790 And if we end up Collin's function like ten times we might eventually get like 10 different callbacks 24 00:01:35,790 --> 00:01:42,980 registered in everyone those 10 might be invoked every single time a user's location gets updated. 25 00:01:42,990 --> 00:01:47,550 Now again that might not be the case with Expo location but essentially with any other library that 26 00:01:47,550 --> 00:01:53,050 would definitely be a risk maybe this function doesn't have that kind of vulnerability or I should say 27 00:01:53,070 --> 00:01:57,130 vulnerability but that issue but it might be that we have some other very similar function that we're 28 00:01:57,130 --> 00:01:59,910 making use of that has some similar problem. 29 00:01:59,950 --> 00:02:05,200 So regardless of how watch position async actually works behind the scenes we should definitely make 30 00:02:05,200 --> 00:02:11,260 sure that when we do a follow up call we somehow stop the previous time we were watching for changes 31 00:02:11,260 --> 00:02:13,540 to the user's location change. 32 00:02:13,540 --> 00:02:15,100 So how are we going to do that. 33 00:02:15,100 --> 00:02:22,020 Well here's the idea the very first time that we call use location we are of course running use effect 34 00:02:22,560 --> 00:02:27,510 whenever we call a U.S. fact and we pass in a function our inner functionally passing has the option 35 00:02:27,840 --> 00:02:31,290 to return something called a clean up function. 36 00:02:31,290 --> 00:02:34,990 This is a function that is meant to solve the exact scenario we are in. 37 00:02:35,190 --> 00:02:40,920 Essentially if we have a use effect took as we do right here that starts up a listener or starts watching 38 00:02:40,920 --> 00:02:47,190 something or starts some process then we can return a function from use effect that we can use to stop 39 00:02:47,190 --> 00:02:51,030 that watching automatically series what goes on behind the scenes. 40 00:02:51,080 --> 00:02:53,960 The very first time we call a location we'll call these effect. 41 00:02:53,960 --> 00:02:58,810 We start watching then we're going to return a cleanup function inside that cleanup function we're going 42 00:02:58,810 --> 00:03:03,020 to put in some amount of code to stop listening to the user's location. 43 00:03:03,410 --> 00:03:07,670 We can then imagine that use location gets called a second time maybe there's no change to a callback 44 00:03:07,670 --> 00:03:14,020 so use effect does not get called and then the third time where we do call use effect when we call use 45 00:03:14,060 --> 00:03:19,910 effect it's going to automatically see that the last time we ran this use affect function like way back 46 00:03:19,910 --> 00:03:20,600 over here. 47 00:03:20,600 --> 00:03:24,990 It's going to see that we returned a cleanup function from the first time. 48 00:03:25,160 --> 00:03:30,800 So before doing anything inside of our use affect function it's going to first call the cleanup function 49 00:03:31,250 --> 00:03:36,900 that we returned from the very first call back over here so we can use that as our opportunity to do 50 00:03:36,900 --> 00:03:41,820 a little bit of cleanup and make sure that we stop listening for changes to the user's location before 51 00:03:41,820 --> 00:03:44,880 we start listening a second time. 52 00:03:44,880 --> 00:03:49,860 So this essentially gives us the ability to easily clean up after ourselves and it's perfectly suited 53 00:03:49,860 --> 00:03:52,180 for what we're trying to do Gates. 54 00:03:52,190 --> 00:03:53,230 Let's make a little change. 55 00:03:53,240 --> 00:03:57,510 We're going to return a cleanup function from our use effect function. 56 00:03:57,560 --> 00:04:01,250 We're going to make sure that inside that cleanup function we stop listening to the user's location 57 00:04:01,460 --> 00:04:03,520 before we start to listen to it. 58 00:04:03,530 --> 00:04:05,550 Once again all right. 59 00:04:05,560 --> 00:04:13,220 So inside of use location I'm going to add in a return statement and return a function like so. 60 00:04:13,420 --> 00:04:16,050 So inside of here we're going to do some amount of cleanup. 61 00:04:16,360 --> 00:04:20,070 For us our cleanup is going to be to call subscriber dot remove. 62 00:04:20,110 --> 00:04:24,430 Remember that stops us ladies listening to updates from the user's location. 63 00:04:24,620 --> 00:04:30,230 So inside of here we're going to first check to make sure that we have a subscriber and then we'll do 64 00:04:30,230 --> 00:04:34,840 a subscriber dot remove like so. 65 00:04:34,870 --> 00:04:37,320 Now the reason that we're checking to make sure that we have a subscriber. 66 00:04:37,390 --> 00:04:42,100 Remember when we first initialize that piece of state we started off with the default value of null. 67 00:04:42,220 --> 00:04:47,830 So there might be some kind of crazy scenario where we do not actually start listening to users location 68 00:04:47,950 --> 00:04:50,080 and then actually run that function right there. 69 00:04:50,080 --> 00:04:55,110 And that's actually very much possible if we ever call use location with a should track default value 70 00:04:55,120 --> 00:04:55,930 of false. 71 00:04:55,930 --> 00:05:00,250 That means we're going to fall into this case right here and we're essentially not going to have any 72 00:05:00,250 --> 00:05:01,740 subscriber around. 73 00:05:02,020 --> 00:05:08,450 So we might have a subscriber value of null in which case we would not want to call remove okay. 74 00:05:08,480 --> 00:05:15,500 Let's try saving this I'll go to track rate again start recording. 75 00:05:15,500 --> 00:05:21,130 Go to my terminal and once again just verify that I'm not like completely spanning this thing with updates. 76 00:05:21,290 --> 00:05:26,520 And once again I should be able to tap on stop and see a very consistent number of locations have now 77 00:05:26,520 --> 00:05:27,850 been recorded. 78 00:05:27,870 --> 00:05:29,190 So this is definitely a good improvement. 79 00:05:29,190 --> 00:05:33,630 We are now cleaning up after ourselves and making sure that we're not just constantly calling start 80 00:05:33,630 --> 00:05:37,660 watching start watching start watching again and again and again. 81 00:05:37,680 --> 00:05:38,020 All right. 82 00:05:38,100 --> 00:05:42,300 So at this point we've got some code that definitely works but there's one last little thing inside 83 00:05:42,300 --> 00:05:44,240 of your maybe one or two things. 84 00:05:44,580 --> 00:05:49,890 I want to make sure it's really clear how we can kind of fix this issue and not run into it in the future. 85 00:05:49,980 --> 00:05:55,050 And I also want to touch on the topic of the fact that we've got this subscriber piece of state inside 86 00:05:55,050 --> 00:05:55,520 of here. 87 00:05:55,620 --> 00:06:00,530 Maybe there would be a better way to handle it subscriber than storing it inside of a piece of state. 88 00:06:00,540 --> 00:06:03,870 So let's take a quick pause and wrap up on use location in the next video.