Improved Fetch Code Refactor
Instagram Firebase
In this video, I want to go ahead and introduce some additional code refactoring into our project that will clean up our current codebase. Two areas that we will be focusing on is the fetching of users and posts in our various screens. Instead of having all the code inside one giant method, we'll look at how to reduce the complexity by using a separate method for our fetch calls. Warning: If you haven't used completion blocks before, you might find the second refactoring a little difficult to understand.

Comments (18)
Fred van Rijswijk
6 years ago
Great!! Already looking forward to the search capabilities and the way how you refactor your code thru the whole project. Excellent learn today!
Brian Voong
6 years ago
weare99
6 years ago
Brian you are my coding hero :)
eilufoye
6 years ago
Thanks alot for working so hard to produce these videos. Looking forward to the search video
Brian Voong
6 years ago
Slava Nagornyak
6 years ago
Hi, Brian. Want to give you another thank for the tutorial and ask another question. :] With Swift 3 out there was introduced Grand Renaming. What do I mean? I mean all those methods like fetchPostWithUser(user: User). It's name is long and the word user repeats a lot of times. With Grand Renaming this function should look something like fetchPosts(for user: User) What do you think about it?
Brian Voong
6 years ago
Kenny Ho
6 years ago
Hey Brian, Love your vids and appreciate your time and commitment. I am having an error every time I click onto the User Tab. The app keeps crashing and I get this message. Would you mind deciphering this for me? Thanks! objc[96638]: Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x1204e6998) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x120308880). One of the two will be used. Which one is undefined. 2017-04-22 05:18:44.339 ModelMe5.0[96638:18763497] Unknown class _TtC10ModelMe5_08SignInVC in Interface Builder file. 2017-04-22 05:18:44.359 ModelMe5.0[96638] <Notice> [Firebase/Analytics][I-ACS023007] Firebase Analytics v.3700000 started 2017-04-22 05:18:44.360 ModelMe5.0[96638] <Notice> [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see http://goo.gl/RfcP7r) 2017-04-22 05:18:44.362 ModelMe5.0[96638] <Notice> [Firebase/Analytics][I-ACS003007] Successfully created Firebase Analytics App Delegate Proxy automatically. To disable the proxy, set the flag FirebaseAppDelegateProxyEnabled to NO in the Info.plist 2017-04-22 05:18:44.582 ModelMe5.0[96638:18763497] Falling back to loading access token from NSUserDefaults because of simulator bug 2017-04-22 05:18:44.583 ModelMe5.0[96638:18763497] Falling back to storing access token in NSUserDefaults because of simulator bug 2017-04-22 05:18:44.583 ModelMe5.0[96638:18763497] Falling back to loading access token from NSUserDefaults because of simulator bug 2017-04-22 05:18:44.583 ModelMe5.0[96638:18763497] Falling back to storing access token in NSUserDefaults because of simulator bug 2017-04-22 05:18:44.584 ModelMe5.0[96638:18763497] Falling back to loading access token from NSUserDefaults because of simulator bug 2017-04-22 05:18:44.584 ModelMe5.0[96638:18763497] Falling back to storing access token in NSUserDefaults because of simulator bug 2017-04-22 05:18:44.788 ModelMe5.0[96638] <Notice> [Firebase/Analytics][I-ACS023012] Firebase Analytics enabled
Brian Voong
6 years ago
rogerbayarea
6 years ago
Thank you for using Extension and Completion Block and I should review more further this to Apple Doc's
shender ramos
6 years ago
i just wanted to say THANK YOU Brian i know recording these videos takes a lot of time and efforts, i ve learned so much on how to refactor code specially using firebase....
TTTTgg
6 years ago
Hey Brain, I am not sure if you have shown how to automically update home feeds when we post a photo without reopen the app? If yes, could you please tell me where it is
Brian Voong
6 years ago
nikolai_georgie
6 years ago
Everytime the collectionview reloads the collectionviewcells, it shows mine first and then all the other users (creating a ugly flickering). Your project works but mine doesn't (even after copying your code).. I have no idea what else to do. Please help!
Antonio Di Francesco
6 years ago
Brian, the refactoring with Firebase extension and completion is poetry to me. I really enjoyed it, thanks!
David Wu
5 years ago
I don't know what should to say to describe about how useful this lesson series is. I've self-learnt Swift programming for 1.5 years and I learnt more in this lesson series than everything else combined from elsewhere. Specifically, the use of completion block, eliminated my confusion that had been existing all times. Brian, thank you.
Maxnelson997
5 years ago
Are there any direct resources going into depth on retain cycles that you suggest besides googling/stackoverflow? I feel like there are quite a few concepts I need a further understanding of to become a more competent iOS Developer. Where did you pick up on these more advanced topics?
diegobust4545
5 years ago
Damn that was so crispy
ksnnacar
5 years ago
Currently finished the app up to this episode. Man, I loved the fetchUserWithUID refactoring part. I appreciate this kinda deep knowledge tips and bits a lot. Thanks. This project course deserves more than its price.
meetrobel
5 years ago
Hey Mr. Voong, I was wondering if you had any information on, how to stop a collection view blinking on reload data() I setting the cell content to nil, before assigning a new post object, because it is recycling the cell's and giving it the previous cell's content before the async is complete for this cell that is being rendered.
Brian Voong
5 years ago
rickkettner
5 years ago
Yet another excellent video Brian! One thing I'm wondering about is why the static func is an extension on FIRDatabase instead of the "User" class itself. I'm very new to all of this and I was under the impression these kinds of methods would usually be tied to the object type, so that if the data source changes in the future, the code structure isn't tied to old/previous data source references. Was this approach used to keep things simple in this lesson or is this the standard approach you would take? Any other thoughts or advice to help bring clarity to this for me? I appreciate it!
Brian Voong
5 years ago
rickkettner
5 years ago
Thank you for the quick response! Just to make sure I understand, you wouldn't recommend turning User into a class, and then adding a static method enabling a call to something like this? let user = User.fetchUserWithID(uid) For some reason, I had concluded/assumed this was the right approach to separate structure out from source... just in case an app had to migrate from Firebase to Parse or something like that - all the calls could remain tied to the object class and only the code inside the static method would change. But there is always a good chance that I just misunderstood or am drawing the wrong assumptions from the few tips I've seen on the subject :).
Brian Voong
5 years ago
rickkettner
5 years ago
Ahh, good to know. Thanks!
astericky
4 years ago
Great question! I was thinking the same thing.
landahoy55
4 years ago
Awesome stuff!!
MaxApp
4 years ago
Hey Brian, a quick note. In the profile controller you have you insert every new post at the index of 0, but in the home controller you append the post. My question is do you need to insert the post at the index of 0 as well or not. Thank you
MaxApp
4 years ago
Also, in the home controller, every time I run the code, the images are not in order. The post shuffle every run.
Brian Voong
4 years ago
MaxApp
4 years ago
But why are you using an append on the home controller instead of the insert function. Also, what do you mean by a sort function.
Brian Voong
4 years ago
MaxApp
4 years ago
Okay, and thank you. But the thing is every time I run the app the photos in the HomeController shuffle for every run and they are in order for each run. Now if I use the sort function how I am going to sort the images but what? By the creationDate or what? Thank you and sorry for this issue.
Brian Voong
4 years ago
malrhex
4 years ago
Hey Brian, do you know why sometimes the scroll doesn't keep scrolling while finger up on a UIImageView?
malrhex
4 years ago
scrolling is lost while touching an UIImageView
Brian Voong
4 years ago
malrhex
4 years ago
Thanks :D
HELP & SUPPORT