-1

In my project I'm entering the same objects in two different Entities in the same data model with same MOC and Persistence Store. I don't want to duplicate the entity because I'm going to modify (remove) objects from the first entity. My question is How I'm going to fetch the objects form another entity? DO I need to have different persistence store and MOC? Well I already fetched the objects but its giving me null value and crashed telling that object can not be nil value.

I guess values are not saving in the core data. Following is the line of code I used to store the values in coreData

CommonEvent *commonEvent = (CommonEvent *)[NSEntityDescription insertNewObjectForEntityForName:@"CommonEvent" inManagedObjectContext:_managedObjectContext];


[commonEvent setValue:number forKey:@"commonNumber"];
[commonEvent setValue:name forKey:@"commonName"];

following is the line of code to fetch the data form core data

CommonEvent *event;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"CommonEvent" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

// define a sort descriptor
NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc]initWithKey:@"commonName" ascending:YES];
NSSortDescriptor *numberDescriptor = [[NSSortDescriptor alloc]initWithKey:@"commonNumber" ascending:YES];



NSArray *scArray = [[NSArray alloc]initWithObjects:nameDescriptor,numberDescriptor, nil];


// give sort descriptor array to the fetch request
fetchRequest.sortDescriptors = scArray;

// fetch all objects
NSError *error = nil;
NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

if (fetchedObjects == nil) {
    NSLog(@" we have a problem: %@", error);
}

NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
NSLog(@" %@  %@  for index:%d", [event.commonNumber description],[event.commonName description], indexPath.row);  // It shows that object values are null


// display all objects
for (CommonEvent *event in fetchedObjects) {

    [mutableArray addObject:[event.commonNumber description]];
    //
     NSLog(@" %@  %@  for index:%d", [event.commonNumber description],[event.commonName description], indexPath.row);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
GameBegins
  • 580
  • 7
  • 12
  • Your question is very hard to understand. But, I don't see any call to save data anywhere in the code you posted. – Tom Harrington Jul 15 '13 at 16:55
  • Sorry about that... I'm saving my data in a core data by using following code. CommonEvent *commonEvent = (CommonEvent *)[NSEntityDescription insertNewObjectForEntityForName:@"CommonEvent" inManagedObjectContext:_managedObjectContext]; [commonEvent setValue:number forKey:@"commonNumber"]; [commonEvent setValue:name forKey:@"commonName"]; – GameBegins Jul 15 '13 at 16:59
  • You included that in your question, but that does not save any data. To save data with Core Data you need to call `-[NSManagedObjectContext save:]`. – Tom Harrington Jul 15 '13 at 17:04
  • Do you have a code snippet for that – GameBegins Jul 15 '13 at 17:22

1 Answers1

1

You have to save your data after inserting it into the MOC:

NSError *error;
[_managedObjectContext save:&error]; 

Also, why are you using key-value coding when you already have created NSManagedObject subclasses? You can write with dot notation and avoid errors:

commonEvent.commonNumber = number;
commonEvent.commonName = name; 

Also, in your code you are declaring event and never initialize it. Of course it will be nil. You probably intended to use the fetched results, e.g.

CommonEvent *event = fetchedObjects.count ? fetchedObjects[0] : nil;

Your code will be easier to read if you make it more concise:

NSFetchRequest *request = 
    [NSFetchRequest fetchRequestWithEntityName:@"CommonEvent"];
request.sortDescriptors = @[
  [NSSortDescriptor sortDescriptorWithKey:@"commonName" ascending:YES],
  [NSSortDescriptor sortDescriptorWithKey:@"commonNumber" ascending:YES]];
NSError *error;
NSArray *fetchedObjects = [_managedObjectContext executeFetchRequest:request
                           error:&error];
Mundi
  • 79,884
  • 17
  • 117
  • 140