Note that there are some explanatory texts on larger screens.

plurals
  1. POhow can I put all fetch requests in a Core Data DAL?
    text
    copied!<p>Totally new to Objective-C and Core Data, coming from a .net background I really want to put all of my fetch requests into some sort of class that I can call, preferably statically to get my objects, something like:</p> <p>ObjectType *myObject = [CoreDataDAL GetObject:ID];</p> <p>Anyone have a pattern to implement this?</p> <p>I am hacking my way through one right now but it's probably not quite right, will post code when I have it.</p> <p>EIDT: Here is my code as it stands right now - seems to work great - please rip it part if I am going down the wrong road - here is the basic DAL:</p> <pre><code>#import "CoreDataDAL.h" #import "CoreDataAppDelegate.h" @implementation CoreDataDAL @synthesize managedObjectContext; -(id)init { if (self=[super init]) { CoreDataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; self.managedObjectContext = appDelegate.managedObjectContext; } return self; } -(Client *) GetClient:(NSString *) ClientID{ /* Client Fetch Request */ NSFetchRequest *request = [[NSFetchRequest alloc]init]; NSEntityDescription *entityType = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:managedObjectContext]; [request setEntity:entityType]; NSPredicate *predicate =[NSPredicate predicateWithFormat:@"ClientID==%@",ClientID]; [request setPredicate:predicate]; NSError *error; NSArray *entities = [[managedObjectContext executeFetchRequest:request error:&amp;error] mutableCopy]; return [entities objectAtIndex:0]; } @end </code></pre> <p>And here is how it is used in my view controllers:</p> <pre><code>CoreDataDAL *dal = [[CoreDataDAL alloc]init]; Client *client = [dal GetClient:clientID]; [dal release]; </code></pre> <p>Seems straight forward enough, thoughts?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload