My model class has to get some data from the internet. So I decided to run it on another thread so the ui doesn't freeze. So when an object wants some data it first asks the model using a method of this type:
- (void)giveMeSomeData:(id)object withLabel:(id)label {
objectAsking= object;
theLabel= label;
NSThread* thread= [[NSThread alloc] initWithTarget:self selector:@selector(getTheDataFromInternet) object:nil];
[thread start];
}
- (void)getTheDataFromInternet {
//getting data...
theData= [dataFromInternet retain]; //this is the data the object asked for
[self returnObjectToAsker];
}
- (void)returnObjectToAsker {
[objectAsking receiveData:theData withLabel:theLabel];
}
As I'm still a newbie, can you tell me if it's a good pattern?
Thanks!
thread
andtheData
. – ShauntagetTheDataFromInternet
does so synchronously. Don't do that—you'll block your UI for however many milliseconds/seconds/minutes/hours/days it takes to get the data. It doesn't matter how small it is, or how awesome your own internet connection is—your users will see your app lock up while it waits for the data. Instead, create (and own) an NSURLConnection to receive asynchronously: developer.apple.com/mac/library/documentation/Cocoa/Reference/… Report the progress using (at least) an NSProgressIndicator. – Shaunta