|
Hello everyone, I have a question about the SwiftUI app architecture. This week I went through MVVM. I created an app that uses Core Data based on the MVVM architecture (Sample code 1). But I failed to use this architecture with CloudKit as I could not get my code to display the data on one device at the same time when I was adding data to the same database on another device. It only updates the view when I manually trigger it. However, the normal architecture (sample code 2) worked perfectly and supported simultaneous updates. In both examples, I used "NSPersistentCloudKitContainer" to create the connection with CloudKit. In the sample code 1, I used "NSFetchRequest". Even though I failed, I am sure that there is a way to use MVVM with CloudKit. But in the second example, I used ""@fetchrequest" and the system did all the work for me. What I see is that SwiftUI is not MVVM by default. To make it MVVM, you have to write a lot of code. In my opinion, we simply fight with the system when using MVVM in some cases, such as the CloudKit example. I also think that fighting with the system could be an advantage in some cases, but not in all cases. However, the disadvantage of the normal architecture of SwiftUI (I am not sure what the name is) is that it creates massive views. When it comes to updating views, this tends to create issues. So, my question is, what kind of architecture do you use? How do you take full advantage of SwiftUI and the other frameworks without changing much while keeping the architecture clean? Also, I came up with a solution for my app (Sample Code 3). But I am not sure about the disadvantages of this method (such as memory usage). What I do is I declare all the variables within the view but I move all the functions (that are not shared) to an extension of the same view. I can call the variables from those functions directly without using methods such as @publish. The rest are models, and I call them directly from the view. In this way, I can keep the view clean. I would call this Model-View-ViewExtension :). Sample Code 1
Sample Code 2
Sample Code 3
|
|
hi Tomato, please don't be terribly concerned if you're a little confused about whether it's MVVM or uses a i have used all of the above; i'm never been fully pleased with any one of them. but i have found plenty of places where, say for a simple View, a my suggestion: start with i'll also suggest you take a look at this page by Mohammad Azam, who is currently all in on "don't use MVVM," despite having spent two years telling everyone (with articles and even a book) exactly the opposite. it's been a very interesting jorney for him, with lots to think about. lastly, on some programming issues: -- your Sample Code 2 updates automatically because -- your Sample Code 1 does not update automatically because your view model does not monitor changes in Core Data ... it simply reads the data at start up, but never sees any external changes after that (as i said in your recent HWS post). for your view model to monitor Core Data, you would likely need to use an if you're interested, i also have some code out on GitHub, which is really my "fail-in-public playground" implementation of a simple shopping list app. there are three branches that use only hope some of this helps, DMG |
|
Hi DMG Thanks a lot for the reply. I think I am going to stay with I am also going to experiment with the points that you mentioned, such as Also, thanks for the Git repo. I had a look at that and it is quite useful. Tomato |
|
I forgot to mention another issue associated with my approach but a feature in MVVM. Basically, if you are developing a multi-platform app (IOS, MacOS, and WatchOS), you will have to write the same |
|
Hello Everyone, I thought I would publish an update as a comment as I assume that this code may help someone else in the future. Apologies if I am spamming the forum. I managed to fix the code. Now it does support simultaneous updates. Thanks to DMG for pointing out to me what needed to be done. Please let me know if you think that the code can be more optimised. Also, I referred to the following posts:
I tested the code on IOS 15.6 and 16.0. I used an IPad and an IPhone for testing. CoreData Entity Details
Test_App_2.swift
Persistence.swift
DogViewModel.swift
ContentView.swift
ViewDogs.swift
AddDog.swift
UpdateDog.swift
|
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.