I am a little confused on the proper way to implement an NSObject model. I am creating a Book class that will story some data (NSString, UIImage, etc.) but I am storing the data on a server. Should I create a method for Book like -(void)loadDataFromServerWithID:(NSString *)bookID then when I create a Book like this:
Book *book = [Book alloc] init];
[book loadDataFromServerWithID:@"1234"];
and within the loadDataFromServer method download the JSON data and assign the instance variables to the object?
I’ve been thinking of many ways I can do this but I’m not really sure anyway is efficient/smart way to do it.
Thanks for any help.
One way to do this is to have only 1 class responsible for talking to the server, then have a method in the class called something like
bookFromServerWithID:that returns a new book object ready to go. Other variations include some form of callback to avoid blocking the main thread.To do asynchronous loading, you would create a method similar to
[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]and would probably call the mentioned method somewhere in your loader. On the callback, you would parse the data from the connection and give the callback to your method theBookobject.If you haven’t worked with queues before, the Concurrency Programming Guide is a helpful (if slightly long and in-depth) read.