IOS MVC: where is the model suitable for the CoreData application?

I have just started building applications with CoreData, but I am familiar with the concepts of MVC because I did a lot of web development work using (and developing) MVC frameworks.

From what I am compiling, CoreData automatically generates classes that inherit from NSManagedObject. Objects are created by querying a selection in a context or by inserting a new one into a context. In the applications that I saw, objects are empty, except for properties that match their attributes in the database, which makes them objects that mimic a row in an entity table.

It makes sense that auto-generated classes and CoreData make up the application model. In the applications that I have done traditionally, instead there is a Model class that is responsible for processing all the data. This is often a Singleton class, and every controller that needs a model can simply use self.model = [Model sharedInstance]; . For large applications, there may be several models, rather than one giant. You will get a picture. I think my first question is: am I right? Does CoreData and its associated NSManagedObject make up the entire model for the application?

I would suggest that this is wrong, because the application might require other functionality that deals with data that does not have an assigned CoreData object. For example: what if a CoreData application needs to populate a table view with data that is retrieved through an HTTP request from foo.com/test (say, this is JSON data). This data does not need to be stored in CoreData, but at the same time, I do not think that obtaining and analyzing data is a task for the controller. If there is an object, FooDataManager (or something like that) that processes HTTP requests to manage data from foo.com (it can extend AFHTTPClient). And then the controllers dealing with foo.com have a property for FooDataManager that acts like a model for this controller? And then the controllers will call [self.fooDataManager retrieveAndParseData]; ?

I want to check this information before starting to develop CoreData applications so that I do it right from the very beginning. In web development, I'm used to having one model for each controller, but it seems like on iOS, there can be many models that do everything themselves, many controllers that use these models, and all these models are in addition to CoreData and NSManagedObjects.

+4
source share
1 answer

From what I am compiling, CoreData automatically generates classes that inherit from NSManagedObject.

No. Unless you specify that this object uses the NSManagedObject subclass that you write, the objects returned by Core Data are instances of NSManagedObject.

I think my first question is: am I right? Does CoreData and its associated NSManagedObject compose the entire model for the application?

It depends on the application. As stated above, if you want this object to map to the NSManagedObject subclass that you provide, you can do this. This would be useful if you would like your data objects to include methods that go beyond just accessing the objects. For example, you can implement the -compare: method to facilitate sorting in a specific order or the -getCurrentImage method, which retrieves a product image from a web server. In addition, your model may include one or more classes of the model controller that deal with such things as sample queries, so the rest of the program does not even need to know that the model uses basic data.

the application may require other functionality that deals with data that does not have an assigned CoreData object ... what if a CoreData application needs to populate the table view with data that is retrieved through an HTTP request from foo.com/test

You can absolutely put this functionality in a separate class and think of it as your model, and I would say that this is probably a good idea. Many iOS programmers would probably place it in the view controller just because it would be appropriate, but I would agree that making it part of your model is the best plan.

+2
source

Source: https://habr.com/ru/post/1433029/


All Articles