1

I am new in iOS. Can anyone tell about how I can map my BO with Core Data so that I may reuse mapping in my project. Here is my code

- (void) saveData
{       
    CoredataAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    NSManagedObject *newContact;

    newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];

    [newContact setValue:firstName.text forKey:@"firstName"];
    [newContact setValue:lastName.text forKey:@"lastName"];
    [newContact setValue:driverLicenceno.text forKey:@"driverLicenceNumber"];
    [newContact setValue:state.text forKey:@"state"];
    [newContact setValue:phoneNO.text forKey:@"phoneNumber"];
    [newContact setValue:injuryStatus.text forKey:@"injuryStatus"];
    [newContact setValue:emailAddress.text forKey:@"emailAddress"];

    NSLog(@"fName%@",firstName.text);
    firstName.text = @"";
    NSLog(@"fName%@",firstName.text);
    lastName.text = @"";
    driverLicenceno.text = @"";
    state.text = @"";
    phoneNO.text = @"";
    injuryStatus.text = @"";
    emailAddress.text = @"";

    NSError *error;
    [context save:&error];
    status.text = @"Person saved";
}

Is there any othere way to map my BO with coredata other than this?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Tahir Mehmood
  • 266
  • 2
  • 9
  • What do you mean to map a BO in Core Data? Do you have already a data model? As I see from your code, you are using a Person entity. So, what's your goal? Thanks. – Lorenzo B May 16 '12 at 14:26
  • My goal is to map my BO to coredata so it will populate my personInfoBO automatically by mapping model object, but cannot able to create mapping model.Here is link i am following http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmMappingOverview.html – Tahir Mehmood May 17 '12 at 05:24
  • With *mapping model* do you mean the Data Model? – Lorenzo B May 17 '12 at 10:45
  • just tell me how to apply sql queries like update ,delete,find second max,joins,view etc in core data ? – Tahir Mehmood May 21 '12 at 05:40

2 Answers2

2

Tahir first you must create Coredata db which you already create. Next select table in Entity panel in coredata model.Right click model in project hirarchy tree. Select new file , select coredata and NSManagedObjectSubclass and than save it. It create model class for coredata table which you can use in your methods.enter image description here

This image show selection of db and table to right click and select NSManagedObjectSubClass

enter image description here

This image show selection of first coredata than NSManagedObjectSubClass and after this when you click next it show you panal to give name and save this in your project. I hope this will help you thank you!

user2128531
  • 153
  • 2
  • 13
1

just tell me how to apply sql queries like update ,delete,find second max,joins,view etc in core data

Since Core Data covers a lot of stuff, I can give some hints.

First of all, dealing with Core Data means dealing with managed objects that "lives" in a sort of scratchpad that is called NSManagedObject. Think to it as a sort of container graph that contains the objects that are retrieved from the file (e.g. sql file) based on the Data Model you have created. Until you don't save a context, changes resides in memory and are not stored physically.

[moc save:&error];

With the NSManagedObject you can create, update, query or delete objects. And do other stuff, of course.

If you need to insert a new managed object (that is declared in your Data Model), you can use insertNewObjectForEntityForName:inManagedObjectContext:.

If you need to perform queries, you need to have a look to NSFetchRequest class. Setting up a NSFetchRequest is equivalent to a SELECT statetement in SQL language.

Here a simple example:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Person" inManagedObjectContext:moc]];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];

// error handling code

The array results contains all the managed objects (the people) contained within the sqlite file. If you want to grab a specific object (or more objects) you need to use a predicate with that request. For example:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstName == %@", @"John"];
[request setPredicate:predicate]; 

In this case results contains the objects (the people) where attribute is equal to John. Setting a predicate is equal to put the WHERE clause in a SQL statement.

If you want to set up joins, views, etc you need to perform correct NSPredicates among entities (based on relationships and attributes) and let Core Data do perform the row stuff for you.

If you need to delete an object, you need to do:

[moc deleteObject:managedObjectToDelete];

If you need to update a managed object, you have to create a fetch request and retrieve the object(s) you are looking for (e.g. you could provide to each object a specific id and set up a predicate with the id you want). If the fetch request retrieve some objects, then you can update them.

These are simple tips to guide you in using Core Data. I suggest you to read the Core Data Programming Guide. There you can find a lot of stuff. If you want you can start with a good tutorial on using Core Data take a look to www.raywenderlich.com.

If you want something else let me know.

Hope it helps.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190