What patterns book for iOS development contains this specific information? [closed]

I’ve read several books on iOS development and Objective-C, however what a lot of them teach is how to work with interfaces and all contain the model inside the view controller, i.e. a UITableViewController based view will simply have an NSArray as it’s model. I’m interested in what the best practices are for designing the structure of an application.

Specifically I’m interested in best practices for the following:

  • How to separate a model from the view controller. I think I know how to do this by simply replacing the NSArray style example with a specific model object, however what I do not know how to do is alert the view when the model changes. For example in .NET I would solve this by conforming to INotifyPropertyChanged and databinding, and similarly with Java I would use PropertyChangeListener.
  • How to create a service model for my domain objects. For example I want to learn the best way to create a service for a hypothetical Widget object to manage an internal DB and also services for communicating with remote endpoints. I need to learn the best ways to do this in a way that interface components can subscribe to events such as widgetUpdated. These services should be singleton classes and some how dependency injected into model/controller objects.

Books I’ve read so far are:

  • Programming in Objective-C (4th Edition)
  • Beginning iOS 5 Development: Exploring the iOS SDK
  • The iOS 5 Developer’s Cookbook: Expanded Electronic Edition: Essentials and Advanced Recipes for iOS Programmers
  • Learn Objective-C on the Mac: For OS X and iOS

I’ve also purchased the following updated books but not yet read them.

  • The Core iOS 6 Developer’s Cookbook (4th edition
  • Programming in Objective-C (5th Edition)

I come from a Java and C# background with 15 years experience, I understand that many of the ways I would do things in these languages may not fit to the ObjC way of developing applications.

Would someone be able to provide me with the book on this topic containing this specific subject matter?

3

Cocoa Design Patterns by Erik Buck and Donald Yacktman is a good resource for understanding how various design patterns are used in Cocoa. Although the book is a couple years old and written for Cocoa rather than Cocoa Touch, the two frameworks are conceptually very similar, and the additions in iOS 5 and 6 do little to change the underlying ideas, especially the ones you’re asking about.

How to separate a model from the view controller. I think I know how
to do this by simply replacing the NSArray style example with a
specific model object, however what I do not know how to do is alert
the view when the model changes.

The model generally won’t talk to the view directly — it should go through the controller. You have a number of options in this respect, including but not limited to:

  • Have the view controller observe relevant parts of the model using Key Value Observing.

  • Use notifications.

  • If the model is changing due to user input, the view controller may know to expect some aspects of the model to change.

  • Use delegation. Define a delegate protocol for your model and adopt it in your view controller. Set the view controller as the model’s delegate.

How to create a service model for my domain objects…

From your comment I see that you’re looking for a way to manage your graph of model objects. The Core Data framework provides object graph management and persistence — it makes it easy to work with your data as objects without having to worry about how to store it all in database tables. It’s fast and performs well even with relatively large amounts of data.

3

Great question. I’ve struggled with finding the best way to build objective-c apps which adhering to MVC and I find that the using delegates is often the cleanest approach to synchronize views with models.

So, if your model is loading something over the network, the delegate might look like this:

@protocol XXItemLoadDelegate <NSObject>
- (void) willLoadItemWithIdentifier:(NSString*)identifier;
- (void) didLoadItem:(XXItem*)item;
- (void) didFailToLoadWithError:(NSError*)error;
@end

And I think it’s clear how a view might show/hide a busy indicator and then update once the item loaded.

The main disadvantages to the delegate pattern are:

  • forgetting to set the delegate
  • forgetting to call the delegate events in the model
  • overwriting the delegate (when multiple views want to be notified)
  • reseting the delegate (e.g. if a view is reused, the delegate should be nil first)
  • painful to refactor if you decide you need multiple observers
  • giving the the protocol and delegate methods consistent names

Despite all these drawbacks, I prefer the explicitness of delegates to other approaches.

NSNotifications are great for keeping completely unrelated pieces of code synchronized. But I’ve found they should be used sparingly, as it makes code incredibly hard to trace and debug.

KVO is also an option for syncing views to models, but I don’t have much experience with using it in larger projects.

I didn’t much care for the Cocoa Design Patterns book. It seemed out-of-date, and didn’t have as much practical information as I would have hoped.

Instead of books, I’d also recommend reading through larger iOS projects on github. You can always see some design patters in action – and learn a few good tricks. For example:

App Sales Mobile

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật