Michael M
Michael M

Reputation: 1034

Objective C iOS positioning of code?

Im very new to Objective-C and am currently learning how to use Xcode and create iOS applications. I am currently testing a Navigation based application. As part of my display I need to download web data, strip the html tags, format the string etc, which takes quite alot of code. I found when this code is inserted into the implementation file its quite slow in the iPhone simulator.
This may seem like a really obvious question but am I right putting the code in the View implementation file? or should I create a separate file to do all the data handling then pass the results to the view file? Once again, sorry if this is trivial and any help is appreciated.

Upvotes: 1

Views: 137

Answers (3)

Chris Long
Chris Long

Reputation: 3074

Hi and welcome to Stack Overflow! Technically, what you are doing now is fine. However, a common strategy for effective coding is called Model-View-Controller, where you separate components that do different things. For your example, the downloading and processing should take place in the model section, which is where content comes from. The controller is simply responsible for taking the model's data and passing it to the view. The view is already provided as part of UINavigationViewController. Again, this doesn't affect performance, it's just a style that makes later maintenance easier.

EDIT

In response to your second question, here's what you can do (assuming you have MyModel and MyController).

MyModel.h:

@interface MyModel : NSObject {
    NSMutableArray *_data;
}
@property (nonatomic, retain) NSMutableArray *data;
@end

MyModel.m:

@implementation MyModel

@synthesize data = _data;

- (id)init {
    if ((self = [super init])) {
        self.data = [[[NSArray alloc] init] autorelease];
        // do your processing here and add it to self.data
    }
    return self;
}

Then, in MyController.m, initialize an instance of MyModel and access model.data whenever you want that array.

Upvotes: 1

user756245
user756245

Reputation:

Hard question, I think you'd better write the code in some controller object rather than in your view object. A view would only worry about displaying the information, while the controller would retrieve the data and pass it to the view. And as your tests suggest, try retrieving the data in a way that doesn't block your app while doing so. In this case you should consider reading Threading Programming Guide from Apple documentation, which cover the subject.

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299345

You should not do any data fetching in a view class. View classes manage displaying data only. Start with Apple's Core Competencies document, and follow the links to The Model View Controller Design Pattern.

The summary is that you want a collection of Model classes that hold your data, independent of how it is displayed. You want View classes that display your data, independent of how it is gathered and calculated. And you want Controller classes that glue the two together.

Upvotes: 2

Related Questions