On 2009 Jul 17, at 09:26, Squ Aire wrote:

Throughout my application I have to do fetching. I have simplified my code by making a helper class called CoreDataHelper...

In fact, I can think of another variation of my method. Namely, to not have class methods in CoreDataHelper, but rather instance methods and initialize the CoreDataHelper with the MOC. The advantage of this would be that I would not have to pass the MOC into each method call. The drawback is that I have to create a CoreDataHelper instance each time I want to use its methods.

This is what I've done, except I've taken it a step further. I've created helpers like this for several different entities. Each entity is a subclass of SSYMojo. SSYMojo has a moc and an entity. See my @interface for SSYMojo below.

A third variation would be to simply do either of the two variations above, but not pass in MOC anywhere and instead just use [[NSApp delegate] managedObjectContext] whenever I need it within CoreDataHelper.

I don't think you want to do that because all ^real^ Core Data apps, sooner or later, end up having multiple mocs.

A more general closing question: How do you make your code more organized? ... Any antidotes?

I believe you mean anecdotes.

To avoid disorganized code, sit down with a pencil and big piece of paper, or a diagramming tool like OmniOutliner, and draw some class diagrams and flow charts. I've heard that some people even do this before they start on a project :)) The Holy Grail is to write a software specification which lays out everything so perfectly that you can hand it to a chimpanzee to write the code. In practice, there's always alot of back-and-forth between the spec and the code, and in the interest of getting the job done before the market moves on, you'll tend to write specs and diagrams for the more difficult parts, but "just write code" for the easy parts that you've had prior experience with. It's always a tradeoff between spending the time up front to plan and diagram, versus the risk of having to go back and do it later and rewrite code if your intuition turned out to be insufficient.

Maybe reading some good design principles book will help?

Great.

Or getting a degree in CS?

Wonderful if you've got the time.

How did you guys learn this stuff?

The hard way, except it's not "learn", it's "learning"  ;)


---------------------------------------

#import <Cocoa/Cocoa.h>
#import "SSYErrorHandler.h"


/*!
@brief A class for putting and fetching -- "managing" -- managed objects of
 a particular entity in a particular managed object context.

@details I wanted to make this a subclass of NSManagedObjectContext. However,
 the documentation for NSManagedObjectContext says that "you are highly
discouraged from subclassing" it. So, what I did instead was to make this class be a wrapper around an managed object context which is an instance variable.
 */
@interface SSYMojo : NSObject {
    NSManagedObjectContext* managedObjectContext ;
    NSString* entityName ;
    NSObject <SSYErrorHandler> * errorHandler ;
}

/*!
@brief The built-in managed object context which the receiver will use.
 */
@property (retain) NSManagedObjectContext* managedObjectContext ;

/*!
 @brief    The name of the entity of the managed objects which the
 receiver is expected to manage.
*/
@property (copy) NSString* entityName ;

/*!
@brief The object to which a setError:message will be sent if an error occurs.&nbsp;
 A weak reference.&nbsp; Be careful!
 */
@property (assign) NSObject <SSYErrorHandler> * errorHandler ;

/*!
 @brief    Designated Initializer for Mojo instances

@details managedObjectContext and entityName are required. If either is nil, this
 method will return nil.
@param managedObjectContext The managed object context into which objects
 will be managed.
 @param    entityName  The name of the entity of the objects which
 will be managed.
 @result  The receiver, or nil if something failed.
 */
- (id)initWithManagedObjectContext: (NSManagedObjectContext*)managedObjectContext_
                        entityName:(NSString*)entityName_
errorHandler:(NSObject <SSYErrorHandler> *) errorHandler_ ;


/*!
 @brief    Returns all objects in the receiver's managed object context
 satisfying given predicates, if any, compounded OR or AND.
 @param    subpredicates  The array of subpredicates to be compounded
 @param    type  Either NSAndPredicateType or NSOrPredicateType
@result An array of results, or an empty array if no results are found
 */
- (NSArray*)objectsWithSubpredicates:(NSArray*)subpredicates
                               type:(NSCompoundPredicateType)type ;

/*!
@brief Gets all of the objects in the receiver's managed object context
 satisfying a given predicate, if any.

@details Pass predicate = nil to get all objects related to this extore.
 @param    predicate  The predicate required of results, or nil.
@result An array of results, or an empty array if no results are found
 */
- (NSArray*)objectsWithPredicate:(NSPredicate*)predicate ;

/*!
@brief Same as -objectsWithPredicate, but only returns the first item found

 @details  Returns nil if no results are found
 */
- (NSManagedObject*)objectWithPredicate:(NSPredicate*)predicate ;

/*!
@brief Returns all objects satisfying a predicate created "directly in code"
 */
- (NSArray*)objectsWithDirectPredicateLeftExpression:(NSExpression*)lhs
                                    rightExpression:(NSExpression*)rhs
operatorType: (NSPredicateOperatorType)operatorType ;

/*!
@brief Returns the first object satisfying a predicate created "directly in code"
 */
- (NSManagedObject*)objectWithDirectPredicateLeftExpression: (NSExpression*)lhs
                                 rightExpression:(NSExpression*)rhs
operatorType: (NSPredicateOperatorType)operatorType ;

/*!
@brief Returns all objects of the receiver's entity in the receiver's
 managed object context.
 */
- (NSArray*)allObjects ;

/*!
@brief Counts all objects of the receiver's entity in the receiver's
 managed object context.
 */
- (NSInteger)count ;

/*!
 @brief    Inserts a new object with the receiver's entityName into the
 receiver's managed object context
 @result   The newly-inserted object
 */
- (NSManagedObject*)newObject ;

/*!
@brief Returns the object from the receiver's managed object context
 that has a given object identifier.
*/
- (NSManagedObject*)objectWithID:(NSManagedObjectID*)objectID ;

/*!
 @brief    Inserts an object from the receiver's managed object context
 */
- (void)insertObject:(NSManagedObject*)object ;

/*!
 @brief    Deletes an object from the receiver's managed object context
*/
- (void)deleteObject:(NSManagedObject*)object ;

/*!
@brief Deletes all objects of the receiver's entity from the receiver's
 managed object context
 */
- (void)deleteAllObjects ;

@end

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to