SwiftUI MVVM Programming with ObservableObject @Published @ObservedObject

  Переглядів 72,476

Lets Build That App

Lets Build That App

День тому

MVVM programming with View Models is the new pattern that Apple is recommending developers follow after WWDC this year. I want to go over a very quick example of setting up an ObservableObject with Published properties to trigger UI refreshes in your app.
Maps UIKit SwiftUI:
www.letsbuildthatapp.com/cour...
Fullstack Social NodeJS iOS HTML:
www.letsbuildthatapp.com/cour...
AppStore JSON APIs
www.letsbuildthatapp.com/cour...
Tinder Firestore Swipe and Match
www.letsbuildthatapp.com/cour...
Twitter Slide Out Menu Course
www.letsbuildthatapp.com/cour...
Podcasts Course
www.letsbuildthatapp.com/cour...
Intermediate Training Core Data
www.letsbuildthatapp.com/cour...
Facebook Group
/ 1240636442694543
Completed Source Code
www.letsbuildthatapp.com/guides
Instagram: / buildthatapp
Twitter: / buildthatapp

КОМЕНТАРІ: 141
@yixe2253
@yixe2253 4 роки тому
Brian wtf, you're on fire! These couple of videos were great. I like how you don't expect UIKit knowledge, great for someone like me coming from web development.
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
We haven't gotten to UIKit component integration yet but once we do it'll be a ton of fun.
@SwiftySanders
@SwiftySanders 4 роки тому
Been struggling with this over the last week. Thanks for this.
@asseeger
@asseeger 3 роки тому
Super concise and exactly what I needed being at the knowledge state I am at right now. Thanks a lot!
@tak68tak
@tak68tak 4 роки тому
Awesome as usual. Your tutorials never let me down.
@ruslanvs
@ruslanvs 3 роки тому
So simple and straightforward. Thanks a ton.
@durgaprasads3710
@durgaprasads3710 3 роки тому
Thanks Brian. Thats a very helpful tutorial and exactly wat I wanted. Will be looking forward for video which loads images.
@testyacy2600
@testyacy2600 4 роки тому
Crisp and clear thank you so much Brain for this video :)
@neelamdheeraj
@neelamdheeraj 4 роки тому
Super useful tutorial. Thank you for making this.
@merouanebboy
@merouanebboy 4 роки тому
Thank you for this video, it would be awesome if you make another video explaining how to fetch the images. Great youtube channel content!!
@MrVipulLal
@MrVipulLal 3 роки тому
Way to go, mate. Thanks for the video
@iOSTechie
@iOSTechie 4 роки тому
Thank you Brian for explaining MVVM 🙂👍 and please make video for images also.
@ashleydube3710
@ashleydube3710 2 роки тому
How'd you know I was watching this in the afternoon.😯😯. Thanx for the great lesson
@MohammadImranimufun
@MohammadImranimufun 4 роки тому
Awesome!!! waiting next part!!
@yusuke7335
@yusuke7335 4 роки тому
im one of japanese viewer. even though not familiar with english, your channel is totally helpful for me always. really thanks a lot:)
@namelastname4077
@namelastname4077 4 роки тому
cute. now go kill some whales...
@ClintonSiegel
@ClintonSiegel Рік тому
Super helpful!! Thank you!!!!
@MrBlaq
@MrBlaq 4 роки тому
A SwiftUI tutorial on image loading and caching would be greatly appreciated.
@shawntroy5327
@shawntroy5327 2 роки тому
i know im asking the wrong place but does anyone know of a trick to get back into an Instagram account..? I stupidly lost the account password. I would appreciate any tips you can offer me!
@nicholasarthur5525
@nicholasarthur5525 2 роки тому
@Shawn Troy instablaster ;)
@shawntroy5327
@shawntroy5327 2 роки тому
@Nicholas Arthur i really appreciate your reply. I got to the site thru google and I'm trying it out atm. Takes quite some time so I will reply here later when my account password hopefully is recovered.
@shawntroy5327
@shawntroy5327 2 роки тому
@Nicholas Arthur it did the trick and I actually got access to my account again. Im so happy:D Thank you so much you saved my ass!
@nicholasarthur5525
@nicholasarthur5525 2 роки тому
@Shawn Troy glad I could help =)
@susupo111
@susupo111 4 роки тому
awesome tutorial! You are genius!
@ElGualdo
@ElGualdo 4 роки тому
Hi Brian, big fan here y truly love all your videos in general, please REMEMBER!!!! Image fetching please, please, please!!!
@davidbratton1804
@davidbratton1804 4 роки тому
Great stuff, please add tutorial on downloading images.
@mohamadtarabishi1722
@mohamadtarabishi1722 4 роки тому
Thank you brian ❤️
@sharkdeng9253
@sharkdeng9253 4 роки тому
Looks interesting. Gonna check it later.
@kevincandiav.795
@kevincandiav.795 4 роки тому
Thank you Brian
@viksviks7070
@viksviks7070 4 роки тому
Awesome tutorial. Thanks
@accountingakash5274
@accountingakash5274 4 роки тому
Thank's for this...
@silver10188
@silver10188 4 роки тому
Nice, I do Android Kotlin MvvM too, glad how logically both platforms are coming closer.
@ConernicusRex
@ConernicusRex 3 роки тому
Swift MVVM /= kotlin MVVM. Kotlin doesn’t have property wrappers which is how MVVM is put together in Swift.
@dawid.ramone
@dawid.ramone 4 роки тому
Thanks for another great video! Do you plan to make a tutorial about combine framework?
@rafaelattal
@rafaelattal 3 роки тому
thanks dude very helpful !!
@sandhun1
@sandhun1 4 роки тому
Thanks for the great content
@hoangson9872
@hoangson9872 4 роки тому
Great video. Thanks!
@vipullal7689
@vipullal7689 3 роки тому
Great tutorial! Thanks...
@obadadarkznly9599
@obadadarkznly9599 4 роки тому
My man Brian is rocking that black shirt!
@ikennaudokporo6061
@ikennaudokporo6061 2 роки тому
Great video. Can I do the reactive programming and data binding with just swift?
@subhampadhi2393
@subhampadhi2393 4 роки тому
Combine + Swift UI full app . I suspect combine will replace Rx
@thegeeksides
@thegeeksides 4 роки тому
better than paid udemy courses!!! Brian, you good good!
@johndemirci8748
@johndemirci8748 3 роки тому
much appreciated
@ameliasaomarcos
@ameliasaomarcos 2 роки тому
Best tutorial
@mkw7147
@mkw7147 4 роки тому
Thanks alot! Is there also an SwiftUI video about how to get a zoomable picture? Best would be a picture thag appears in the middle of the screen after klicking it and than is zoomable. Like on facebook.
@oneminuteswift1072
@oneminuteswift1072 4 роки тому
fetching is beautiful
@mevanslondon
@mevanslondon 4 роки тому
So if I wanted to fetch data from fire base /store, I would put that code in the observable object?
@joeerland7798
@joeerland7798 4 роки тому
Loved the video!! Would like some clarification, would you recommend using view models on the cells themselves as well? Or would you recommend injecting the model itself into the cell?
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
If the cell is purely display, I'd use a model injection. If handling taps probably use VM of some kind.
@cesmorf5398
@cesmorf5398 4 роки тому
@@LetsBuildThatApp modal injection for simple ?
@rrakesh05
@rrakesh05 4 роки тому
very nice tutorial
@md.touhidulislam6102
@md.touhidulislam6102 4 роки тому
Thanks
@bat-orshikhbaavgaikhuu4329
@bat-orshikhbaavgaikhuu4329 4 роки тому
Are you going to do your next videos and examples in this pattern? I hope it would be nice if you so.
@EimantasK
@EimantasK 4 роки тому
Very nice videos, thank you! One thing I'm truly missing is project structure, I'd really want you to write clean code and move all classes into separated folders. : ))
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
I would most likely separate files into their feature folders. Not sure how useful this is since it’s just personal preference at this point.
@fedemtz6
@fedemtz6 3 роки тому
How can I cache the data in core data? Do I just on load check if the cache is expired and if it is then get the data from the server, save it to core data, and update the cache expiration date. and if it is not expired return the data from core data? Great video btw
@megustaeljamonelchorizoylo560
@megustaeljamonelchorizoylo560 2 роки тому
Great video! What about MVVM and Coredata? Greetings
@ferrakkembhuiyan4585
@ferrakkembhuiyan4585 3 роки тому
Would you kindly make a video about the Realm? Saving JSON data to realm using MVVM?
@gastondipierro
@gastondipierro 3 роки тому
47/5000 Very good lesson, well explained ... Thank you
@jerryseigle2538
@jerryseigle2538 3 роки тому
Could you make a video on A SwiftUI tutorial on image loading as well as the grid layout please.
@richprice5434
@richprice5434 4 роки тому
Great vid I ll be honest if Xcode supported mvvm straight outta the box, I would use it every time so much better than mvc
@30guarino
@30guarino 4 роки тому
You can tell Brian is feeling better!
@joshuaboltz
@joshuaboltz 2 роки тому
Hey Brian, what’s the difference between @StateObject and @ObservedObject? I’ve seen both used with @ObservableObject
@habibdurodola5728
@habibdurodola5728 4 роки тому
You are the best brain ...
@begzodbakhriddinov585
@begzodbakhriddinov585 3 роки тому
ahah, I see u have built Landmarks app too) nice)
@liuqun
@liuqun 4 роки тому
Hi Brian, thanks for the video! I just started learning SwiftUI, so I have some questions on the implementation of MVVM with SwiftUI in this video, as I am not fully convinced this is a MVVM yet. I assume Course is the model, CoursesViewModel is the view model and then you have ContentView as the view. The SwiftUI glues the view model and view nicely with bindings, that's good. However, since you don't have a Courses model (the data array), this is done in the view model and it also does the data fetching via JSON and owns the data after that, which, IMO, should all be done in a Courses model. As such, in the view you also reference the model directly (eg: coursesVM.courses) to retrieve the data. This makes me feel it is breaking the rules for MVVM. Did I miss anything? Thanks!
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
Yes the courses array should be transformed into a separate view model object for your view to render. I can only show so much in these super long videos. The focus of this lesson is how to use bindings.
@liuqun
@liuqun 4 роки тому
@@LetsBuildThatApp Thanks Brian, good to know!
@indiekiduk
@indiekiduk 4 роки тому
Might I suggest decoding the JSON in the data task block rather than on the main thread.
@badakml7366
@badakml7366 4 роки тому
soo, the question is, do we still need rxswift anyway?
@dejanmalbasic4483
@dejanmalbasic4483 4 роки тому
How to call func fetchCourses() on first load? If you want to update ui before first showing. Thanks
@nicodioso
@nicodioso 3 роки тому
Great tutorial! However I have a question. Is it good practice to put the network related functions to the View Model class? Hope you can answer. Thanks!
@LetsBuildThatApp
@LetsBuildThatApp 3 роки тому
It’s really up to you. For these simple POC apps I’ll never include a separate network layer.
@andrejkling3886
@andrejkling3886 3 роки тому
Gratitude.....
@tarekmustafa998
@tarekmustafa998 4 роки тому
Great Video! I am looking for a way in SwiftUI to call a function once a Picker value has changed. Can you create a tutorial about that?
@ConernicusRex
@ConernicusRex 3 роки тому
Simple delegate pattern. Comes for free with the picker.
@grenvil8414
@grenvil8414 3 роки тому
Please teach us to show the loading indicator
@sugiono2801
@sugiono2801 4 роки тому
why its not changing if i want to change property value?
@gabbrissimo
@gabbrissimo 4 роки тому
I don't have Catalina yet but i'm curious about memory management in SwiftUI
@ConernicusRex
@ConernicusRex 3 роки тому
Still handled by arc like all of Swift.
@ayushgogna9732
@ayushgogna9732 3 роки тому
what If the data is not array how to set a empty @publisher to courses and do self.courses = try JSONDecoder().decode(Course.self, from : data!). this is showing error that cannot set the value to courses = array
@dheerajbhavsar9769
@dheerajbhavsar9769 4 роки тому
Hello There, I have been developing some of my UIKit Apps with SwiftUI. The problem that I've faced so far is that there's no way to show an activity indicator. Apart from that, I am able to develop the applications very fluently as I've also worked with Flutter and React Native. I want to hear back from you if there's any possibility of having an activity indicator or alternative in SwiftUI. Thanks and keep uploading these videos as they have helped me a lot with the basic paradigms of iOS App Development.
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
Yes its a simple process of wrapping your UIActivityIndicatorView inside a UIViewRepresentable. There are already many tutorials for this online.
@anmolrajpalofficial
@anmolrajpalofficial 4 роки тому
Hi Brian, you need a correction in your calculator section page on your website. It says - "Last updated 2019 years ago" for this one
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
I wrote that guide in 0 BC
@smackaroony
@smackaroony 4 роки тому
yes
@nurlanakylbekov7266
@nurlanakylbekov7266 3 роки тому
I think you should create another class for networking layer. And ViewModel must only update the view.
@NonchalantSquid
@NonchalantSquid 4 роки тому
Have you done a video about the equivalent to UIKit lifecycle methods in swiftUI ? Having a hard time conceptualizing where these API calls should be placed.. I know there's onAppear/onDisappear methods, but that seems like a bad place for API calls as every single time a view controller appeared (as opposed to loaded?) a request would be made
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
Yes the maps course on the website goes over view model structure in SwiftUI. Also we use environment vars to keep track of globals.
@user-lz8lr8gz2u
@user-lz8lr8gz2u 3 роки тому
sorry i have a question why your var name = courses and u can call course on line47
@MrDonhekel
@MrDonhekel 4 роки тому
any out of the box swiftui ‘pull to refresh’ ?
@NonchalantSquid
@NonchalantSquid 4 роки тому
I've been struggling with this.. did you ever find a reasonable workaround? Not sure that this exists in the framework out of the box yet
@JavierMares
@JavierMares 4 роки тому
black polo shirt. it's business time
@JohnAlanWoods
@JohnAlanWoods 4 роки тому
Brian, strictly, shouldn't the JSON be in the model, and the view model read it from the model?
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
My stance is to write whatever fits your style.
@JohnAlanWoods
@JohnAlanWoods 4 роки тому
Oh sure and great video. Just trying to understand MVVM academically
@sub040
@sub040 4 роки тому
I want to see how you layout those images
@LeonirAlves
@LeonirAlves 3 роки тому
Me too 👀
@tonyhawk123
@tonyhawk123 3 роки тому
In the video why is it “@ObservedObject var coursesVM = CoursesViewModel()”, and not “@StateObject var coursesVM...”? Is that object not going to get recreated every time the view hierarchy gets rendered? Thanks.
@LetsBuildThatApp
@LetsBuildThatApp 3 роки тому
Yes you would use injection or sorts for a real project
@wyattb3138
@wyattb3138 4 роки тому
How do you make a dynamic UI making and removing elements based on website HTML? Just like how the Apple News app works.
@ConernicusRex
@ConernicusRex 3 роки тому
Simple! That isn’t how the Apple News app works. It populates a custom collection view in UIKit with data from an internal RESTful API.
@wyattb3138
@wyattb3138 3 роки тому
Cody Morley, thanks for answering.
@tamasnemeth4054
@tamasnemeth4054 3 роки тому
Hi there, After clicking Fetch Courses i've got an error "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" at line "self.courses = try! JSONDecoder().decode([Course].self, from: data!)" however i followed your instructions. Could you please help me?
@billyanderson2218
@billyanderson2218 3 роки тому
i have same problem
@qili5149
@qili5149 4 роки тому
it seems Apple has published an MVVM architecture just like what Android Jetpack did
@smackaroony
@smackaroony 4 роки тому
collared shirt
@sujathareddy870
@sujathareddy870 4 роки тому
I've changed CoursesViewModel as singleton. How do I use singleton object in Contentview since i cannot make singleton object as observedobject?
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
You probably don't want to use Singletons in SwiftUI.
@seunghunyang4584
@seunghunyang4584 2 роки тому
API error occured..
@tantran1109
@tantran1109 4 роки тому
The application seems not to run if i change the course initialization to @Published var courses = [Course]( ), can you explain it, i’m just a beginner in this swiftui
@tantran1109
@tantran1109 4 роки тому
Same issue also with @Published var courses: [Course] = [ ]
@MasterYix
@MasterYix 4 роки тому
Where would the service layer go in mvvm? Would I dependency inject a service into the viewmodel, using the new @EnvironmentObject? Do you have any course on how to make that so it's "production ready" and testable? Having the network request in the ViewModel seems like a bad practice.
@Rita-rs5qi
@Rita-rs5qi 3 роки тому
8:37 just do it. kill that itch xd
@danielwatson6529
@danielwatson6529 4 роки тому
api or those playing along at home api.letsbuildthatapp.com/static/courses.json
@johnbogil2087
@johnbogil2087 4 роки тому
why does the viewModel have to be a class? Why can't it be a struct?
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
Because of references. Structs don’t support this
@kumarshivang4431
@kumarshivang4431 4 роки тому
Try github.com/rever-ai/CombineFirebase to use combine with firebase
@zhuzi_2037
@zhuzi_2037 4 роки тому
你英语怎么学得,我想找stack overflow的remote工作,可是口语实在太烂,听不懂也不会说。
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
我石头爆出来就会英语了
@vladusa
@vladusa 4 роки тому
Instructions not clear. I blew up my microwave.
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
That’s unfortunate. Did the food remain edible?
@vladusa
@vladusa 4 роки тому
@@LetsBuildThatApp You bet! Just a hell of a wait on the toilet. Decided to watch more of you.
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
The toilet as it turns out is one of the best place to watch this channel. I welcome all users.
@vladusa
@vladusa 4 роки тому
@@LetsBuildThatApp Indeed. Welcome them alright.
@indiekiduk
@indiekiduk 3 роки тому
Why not just use SwiftUI's own pattern? 6:24 you shouldn't init an @ObservedObject like that. I don't think MVVM is compatible with SwiftUI at all just use the View struct the way it was designed.
@andywong3095
@andywong3095 4 роки тому
First of all, no disagreement or disrespect of MVVM or coders (who preferred MVVM). I HATE MVVM, hehe. WHY? When I was little, (for 1 year), I was forced to learn the RED_BOOK, I hate it too, these BIG companies want to force me to read their RED_BOOK, I say NO. One more thing, GOOGLE IMAGE, search MVVM===> you will think? how Many version of MVVM are out there. The word "DerivativeS", we used it for almost everything in 70's, like the ALGO word, now.
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
The idea of MVVM has been explained quite horribly in the past, once you see how easy it is in SwiftUI you will adopt in in a heartbeat.
@andywong3095
@andywong3095 4 роки тому
@@LetsBuildThatApp Like I said, NOTHING WRONG with MVVM. I am OLD, do NOT want to be dictated, how I should implement anything. Example, Apple's UI Guidelines, monochrome UI, Microsoft's naming conventions, Cap and No Underscore, plus a lot of other nonsense, hehe. Example, UWP implementation of Observable Collections (XAML): My implementation, I have a Main Grid, that contains 1,2,3, or more views. One 1 will be visible at anytime. Each View's DataContext is binded to an Observable-Class, and associated with a DATA-TEMPLATE. (as Page Resources). The DataTemplate will define, "how the data will be presented", (the data is the Model) To show any view, just set Visibility to Visible, and fetch the data(the observable). No need to time it, wait for it, just depend the observable to trigger the UI update. Is that more simple than MVVM?
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
Not sure, but it seems you just a described version of MVVM that can be implemented via Observable object. Yes, new patterns emerge and claim dominance, you are definitely free to choose whatever you want. However if other folks can't read your code easily due to obtuse patterns then it becomes a problem .
@andywong3095
@andywong3095 4 роки тому
@@LetsBuildThatApp Exactly, from a point of view of any MVVM coder. hehe. Derivative, that Is the word. I can insist, that is NOT MVVM. (Katy Perry parallel, hahaha MVVM ->WPF (which is based traditional platform => method is by default await-async, blocked.) Now MS -> UWP (methods are by default non-blocking => concurrent by default => await async is optional, only if needed to block it.) MVVM apply to UWP => different approach missing something (listeners) If one thinks, this is MVVM, 50% correct. But, the same one can also say C# is (C C++)'s derivative. I may add, XAML, since WPF has 1 way or 2 ways binding. Same as UWP. try to copy your WPF code to UWP project, it may compile, but WILL CRASH all over. One more thing. DART and UWP has a lot in common. XAML and Widget are rendered on "VIEW-BOX" approach, you can load 10,000 item, and while adding the items to the collections, your 1st page with 10 items, it will be immediately visible, you may continue to scroll down, while 9,990 items still loading. If you coded your App, correctly, while scrolling to the last 10,000 items, your RAM usage will remain More, or less the same. ( I have little doubts, SWIFTUI will be the same as UWP and DART.)
@LetsBuildThatApp
@LetsBuildThatApp 4 роки тому
Again you're free to choose whatever pattern you like, thats what so great about coding.
@ConernicusRex
@ConernicusRex 3 роки тому
Why are there so many Java jockeys and 🤮 web devs in the comments of this iOS video?
@LetsBuildThatApp
@LetsBuildThatApp 3 роки тому
The internet is a wild place to be.
@SteveHiemstraAKAspeg
@SteveHiemstraAKAspeg 3 роки тому
ViewModel is such a gross name.
@LetsBuildThatApp
@LetsBuildThatApp 3 роки тому
At first it seems like a perverse name, but like anything perverse you get used to it after a while.
Swift API Calls for Beginners (Networking) - Async Await & JSON
25:35
MVVM Combine Swift (2022) | UIKit | Transform Input & Output
40:56
Kelvin Fok
Переглядів 10 тис.
Лизка заплакала смотря видео котиков🙀😭
00:33
iOS Roadmap to Professional Developer: Skills you MUST have!
19:42
Lets Build That App
Переглядів 136 тис.
Everything You NEED to Know About Client Architecture Patterns
5:51
ByteByteGo
Переглядів 197 тис.
SwiftUI - Intro to MVVM | Example Refactor | Model View ViewModel
11:55
Weak and Unowned Self Closure Memory Leak Fixes
12:21
Lets Build That App
Переглядів 55 тис.
MVVM vs. MVI - Understand the Difference Once and for All
18:40
Philipp Lackner
Переглядів 22 тис.
SwiftUI MVVM Tutorial: Simple Example with ObservableObject
12:43
Logan Koshenka
Переглядів 18 тис.