Basic data design patterns

In my first iOS application, I plan to enable CoreData, but I'm a little unsure of the correct design pattern I should use.

In my application, I have the following 3 properties.

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

I believe that the best way to use these objects is to embed them in the constructor of all relevant view controllers that need to access data from CoreData.

The cleanest way I can think of is to create some kind of repository class for each table that I have in my data model, which will host the above 3 objects and provide helper methods for accessing the table data, for example. fetchAllTeams (). These repository classes can then be injected into the appropriate view controllers instead of entering all 3 objects above.

Does this look like what needs to be done in the world of CoreData and Objective-c?

Also, the best way to create these repository classes is to remove the code and properties of the default kernel core data from appDelegate (automatically generated) and place them in the abstract repository class.

NSPersistentStoreCoordinator, NSManagedObjectModel NSManagedObjectContext , appDelegate.

+4
2

-, , Core-Data, . , Core-Data - (sqlite, binary, xml), , , . , , NSManagedObjectContext. underling, , , , NSManagedObjectContext . , , , , . , , , , .

:

  • NSManagedObjectContext
  • , , OR,
  • , , , .
  • , NSManagedObjectContext ... kinda... , , start - , ( NSManagedObjectContext NSManagedObject)
  • , . NSManagedObject Core-Data - .
  • NSFetchRequest NSPredicate, NSManagedObjectContext.
  • NSFetchedResultsController, UI, UITableView.

, Core-Data - , . , (, NSManagedObjectContext , , ).

+3

, ...

, , appDelegate ( ) .

, Xcode . : http://commandshift.co.uk/blog/2013/09/07/the-core-data-stack/ ( @jrturton).

NSPersistentStoreCoordinator, NSManagedObjectModel NSManagedObjectContext , appDelegate.

( ).

, . , .

-, ( ), . . , managedObjectContext.

, - , , , , .

, . , , , , , ( ) . (, , , - , , ).

, , mogenerator, .

0

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


All Articles