Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:17:21+00:00 2026-06-04T17:17:21+00:00

We have made a repository layer for interacting with Core Data that have methods

  • 0

We have made a repository layer for interacting with Core Data that have methods such as allItems(), addItem:(Item*)item where item being the NSManagedObject subclass. When we need to save an item we invoke the method on the repository passing the subclass instance as an argument. However, that does not work because we can’t use the initinitializer and the context is hidden inside the repository.

What is the best way to transfer objects when you have an architecture like this? Is making a ItemDTO an passing that around an option? Or are there better ways to solve this such as not using subclassed NSManagedObject at all and just use key/value that works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:17:22+00:00Added an answer on June 4, 2026 at 5:17 pm

    I wrotecopy-pasted a sample project that hides the context from model custom classes: branch 10583736.

    (it’s not final production code, just a quick example, don’t expect it to deal with multithreading or weird errors)

    Hiding the context to custom classes is just a matter of defining custom methods to deal with every situation where you normally will request the context and use it.

    You can define a class for the store layer without exposing the context:

    @interface DataStore : NSObject
    
    + (id)shared;
    
    - (void)saveAll;
    - (NSEntityDescription *)entityNamed:(NSString *)name;
    /* more custom methods ... */
    - (NSManagedObject *)fetchEntity:(NSEntityDescription *)entity withPredicate:(NSPredicate *)predicate;
    
    @end
    

    I suggest to use a common ancestor for all your custom model classes to save some typing. This class can be the only one that interacts with DataStore directly. It doesn’t have access to the context.

    @interface DataObject : NSManagedObject
    
    + (NSString *)entityName;
    + (NSEntityDescription *)entity;
    - (void)save;
    /* more custom methods ... */
    
    @end
    

    Finally your model custom classes defines any method you need probably taking advantage of whatever is provided by the superclass:

    @interface Card : DataObject
    
    @property (nonatomic, retain) NSString * question;
    @property (nonatomic, retain) NSString * answer;
    @property (nonatomic, retain) Deck *deck;
    
    /* return a new card */
    + (Card *)card; 
    
    /* more custom methods ... */
    
    @end
    

    The master branch has a more usual approach where model classes obtain the context and work with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base controller that I made so I can pass data to
I have made a deploy with capistrano in a repository remote. I'm novice with
I have made a small program in C# that I want to run in
I have made a list that uses a list of dates as an index,
I have a repository in Git. I made a branch, then did some changes
I have made a simple batch file for backing up my repository changes and
suppose that I have made some changes since last update (revert) to my a.b
I have an svn server that I checkout the repository in my computer. The
I have made a bare repository (MY_LOCAL_REP) out of a public git repository hosted
I have cloned a remote repository and made a few commits. I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.