It is interesting that iOS apps suffer the same sort of runaway complexity that often plagues Rails apps, whereby the controller absorbs too much responsibility and becomes unmanageable. This is usually the collateral damage of an app growing within the overly tight conceptual confines of model/controller. This points to a lack of developer education surrounding architecture and design. Most projects have too few abs…
iOS application architecture: MVVM, behaviors, singletons, subclassing
21–30 of 36 posts
Re: iOS application architecture: MVVM, behaviors, singletons, subclassing
#22Earlier quoted context omitted.
You are thinking of the traditional conception of MVC, where the model notifies the views of updates and changes (i.e. they can talk to each other) Cocoa does not use the traditional notion of MVC. All documented here very clearly MVC as a Compound Design Pattern: https://developer.apple.com/library/ios/documentation/genera... "The controller object in this compound design pattern incorporates the Mediator pattern as…
There's something else to architecting a Cocoa app that I want to add, mainly because I had heard this a while ago but only just recently figured out how to incorporate it. There are Cocoa programmers (my understanding is that it is mainly the O.G.'s) that make good use of class extensions and categories to split the functionality of a class into different source files. For example, if you have a view controller hand…
Really, this is just a weird halfway house reminiscent of Rails' ActiveSupport::Concern. Much like that implementation, it encourages developers to address SRP by chopping the class up and calling it done. Difficulties in testing and reasoning are still in place, and the class is likely still too coupled.
If the local units are truly independent concepts, then they should be split out into their own objects. Either make smaller objects that either model the domain closer, or abstract away needless technical details. For example, any sort of serialization (web service/DB) should be hidden behind a high-level class that obscures how it's done if only to firewall the dependency itself from leaking it's conceptual garbage over the rest of your code.
This can be applied gradually with good effect. It's not an all-or-nothing proposition.
Re: iOS application architecture: MVVM, behaviors, singletons, subclassing
#23Earlier quoted context omitted.
You are thinking of the traditional conception of MVC, where the model notifies the views of updates and changes (i.e. they can talk to each other) Cocoa does not use the traditional notion of MVC. All documented here very clearly MVC as a Compound Design Pattern: https://developer.apple.com/library/ios/documentation/genera... "The controller object in this compound design pattern incorporates the Mediator pattern as…
There's something else to architecting a Cocoa app that I want to add, mainly because I had heard this a while ago but only just recently figured out how to incorporate it. There are Cocoa programmers (my understanding is that it is mainly the O.G.'s) that make good use of class extensions and categories to split the functionality of a class into different source files. For example, if you have a view controller hand…
In this project I have one reusable ViewController (among others, but this example is about this one) that had nothing but a UITableView and a search box (a UITextField for searching by text, and a segmented control that sorts/filters the data by preset values like A-Z or distance).
About halfway through this project I found myself with a controller with several if/else blocks and other weird things to stop code re-use and make the controller abstract in the sense that I could init it with whatever data object I needed and it would figure everything else out from there. This accomplished my goal of limiting code re-use but it absolutely sucked when a change had to happen. Conceptually, it was terrible to go down and find the correct "else if {}" to make my amendments to and then make sure that didn't take anything else down with it.
Then I said to heck with code re-use. I'm going to make this sucker clean and easy to find/fix/edit/create a new one. I pulled all of my UITableViewDelegate and UITableViewDataSource code into separate files depending on the data model that was necessary for this controller. I still init the Controller the same way, but now all of the searching/sorting/table actions that were horribly done earlier are in their own separate classes - specific to the model for each.
This means that I have 12 files that have a slightly similar structure - all of the numerOfRows and cellForRowAtIndexPath type methods as well as my custom search view's delegates. And yes, an earlier self would of been a little weirded out by such blatant disregard for reuse. But yesterday, when a change request came in to add another similar type view with a different API and data model it literally took me 5 minutes to subclass my XXDataSource class and hook it up to a model, and the whole thing worked perfectly. 5 minutes. I can't tell you how good it felt to not have to look through the cruft and add another "else if" check and make sure my other stuff would still work.
I hate to call it beautiful since I'm still a noob, but damn is it beautiful. My controller has 40 lines. Each XXDataSource class has about 100-150ish. I highly recommend this approach, especially when it looks like you're going to reuse a tableview-esque controller.
Now my XXNavigationBrain solution - that's a whole other topic. ;)
Re: iOS application architecture: MVVM, behaviors, singletons, subclassing
#24If you're enamoured with MVVM and know C# you might consider trying out Xamarin + MvvmCross [1]. I'm using it on a few projects and the code shared across platforms is (ballpark) 70-90% depending on the size of the project. Xamarin's come out with Xamarin.Forms which looks like it could supersede MvvmCross but I gave it a shot and it's pretty immature right now. Plus its dependency injection leaves a little to be des…
It might be an unfair comparison as MvX is a much older project but it felt more like something cobbled together over time than something really thought out. I also really like the reactive approach - your view model describes the relationships between its properties as they change rather than have a morass of interlinked event handlers.
Paul Betts is pretty active within the portable-C# community, with multiple active projects: https://github.com/paulcbetts and has a pretty good blog, where at the moment he's going back through the RxUI docs: http://log.paulbetts.org/
Re: iOS application architecture: MVVM, behaviors, singletons, subclassing
#25The examples that are available on SO, the apple docs, and IB generated/influenced code work great for the simple case but fall down a bit when your app grows. there are much better and simpler ways to handle some of the common patterns in large apps, for example the great thin view controllers article.
Objc.io is a fantastic resource.
Re: iOS application architecture: MVVM, behaviors, singletons, subclassing
#26Earlier quoted context omitted.
You are thinking of the traditional conception of MVC, where the model notifies the views of updates and changes (i.e. they can talk to each other) Cocoa does not use the traditional notion of MVC. All documented here very clearly MVC as a Compound Design Pattern: https://developer.apple.com/library/ios/documentation/genera... "The controller object in this compound design pattern incorporates the Mediator pattern as…
There's something else to architecting a Cocoa app that I want to add, mainly because I had heard this a while ago but only just recently figured out how to incorporate it. There are Cocoa programmers (my understanding is that it is mainly the O.G.'s) that make good use of class extensions and categories to split the functionality of a class into different source files. For example, if you have a view controller hand…
If you put the delegate and datasource in a separate file at least makes your controller look a lot cleaner but still there's still so much stuff going on in them that doesn't separate well. If you feel the need to add #pragma, categories and that kind of things to make your code more manageable the underlying code violates the single responsibility principle.
I tend to not use UITableView unless I need either the performance or I am using a lot of built in stuff and there's no fancy stuff going on. Having to know up front what the height will be of a yet to be rendered cell really leads to a lot of ugly code quickly. I will not use it if I simply want to repeat some items vertically.
Re: iOS application architecture: MVVM, behaviors, singletons, subclassing
#27Show me a singleton and I'll show you a doubleton, no wait, its a tripleton....
Re: iOS application architecture: MVVM, behaviors, singletons, subclassing
#28because that would turn off the readers.
Re: iOS application architecture: MVVM, behaviors, singletons, subclassing
#29Earlier quoted context omitted.
There's something else to architecting a Cocoa app that I want to add, mainly because I had heard this a while ago but only just recently figured out how to incorporate it. There are Cocoa programmers (my understanding is that it is mainly the O.G.'s) that make good use of class extensions and categories to split the functionality of a class into different source files. For example, if you have a view controller hand…
I have experience doing something similar in a project at work. In this project I have one reusable ViewController (among others, but this example is about this one) that had nothing but a UITableView and a search box (a UITextField for searching by text, and a segmented control that sorts/filters the data by preset values like A-Z or distance). About halfway through this project I found myself with a controller with…
My average controller is between 200 and 400 lines of code. This might be a lot of code in Rails of other frameworks but I find it acceptable in Objective-C (considering it's an extremely verbose language).
To reduce the lines of code in my controllers I usually use categories or subclasses for custom views (I don't use Interface Builder). You can also make manager objects to do most of the workload and reduce the lines of code per method. I think reducing the lines of code per method is more important than reducing the lines of code overall.
Things that I configure once and don't plan to touch again I put in separate files like subclasses or categories. Things that can change or are necessary for understanding the big picture stay in the controller. And guess what? Data Sources and Delegate Methods are necessary for understanding how a UITableView works. In my case 200 to 400 lines of code (on average) seems like a good compromise.
Re: iOS application architecture: MVVM, behaviors, singletons, subclassing
#30If you're enamoured with MVVM and know C# you might consider trying out Xamarin + MvvmCross [1]. I'm using it on a few projects and the code shared across platforms is (ballpark) 70-90% depending on the size of the project. Xamarin's come out with Xamarin.Forms which looks like it could supersede MvvmCross but I gave it a shot and it's pretty immature right now. Plus its dependency injection leaves a little to be des…