Core Data Objective C

Hello everyone, I have an issue that I cannot figure out, been at it for quite some time now.


My goal is to populate a NSMutableArray and save it to core data and retrieve it. My tableview is being populated correctly , i.e. I am using Group and populating the titleForHeaderInSection. But when I terminate the app and try to do a Fetch and cast to an NSMutableArray the data is unreadable and my app is crashing.


This is my NSMutableArray *sectionNames , what it looks like after being populated.

The NewObject has this in it <Car_Doc_Safe_Plus.CarName: 0x7fc2226cf080> (entity: CarName; id: 0xd000000000080000 <x-coredata:/

carsname = "(\n Ram,\n Jeep\n)";

relationship = nil;




This is my Termination when I try to Fetch. *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Car_Doc_Safe_Plus.CarName length]: unrecognized selector sent to instance 0x7fd783ce3130'



This is my save to Core Data; My entity is set up to receive NSString , I also tried - transformable - with no luck.


-(void) saveToCoreData  {

  NSError *error;
  AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication]delegate];
  NSManagedObjectContext *context =  appDel.managedObjectContext;
  _entity = [NSEntityDescription entityForName:@"CarName" inManagedObjectContext:context];
  NSManagedObject *newObject =[[NSManagedObject alloc]initWithEntity:_entity insertIntoManagedObjectContext:context];
  
  NSString *saveString = [[NSString stringWithFormat:@"%@",sectionName]mutableCopy];

  [newObject setValue:saveString.stringByStandardizingPath forKeyPath:@"carsname"];
  [context save:&error];

}




This is my Fetch in ViewDidLoad...


AppDelegate *appDel = [UIApplication sharedApplication].delegate;
  NSManagedObjectContext *context = appDel.managedObjectContext;
  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"CarName" inManagedObjectContext:context];
  [request setEntity:entity];
  NSError *error = nil;
  
  NSMutableArray *results = [[context executeFetchRequest:request error:&error]mutableCopy];
  NSString *tempString = [NSString stringWithFormat:@"%@",results];
  sectionName = [[NSMutableArray arrayWithArray:result]mutableCopy;]


If I do an NSLog it looks like this for the tempString ....

The Temp-Data-String has this in it (

"<Car_Doc_Safe_Plus.CarName: 0x7fd783cbd000> (entity: CarName; id: 0xd000000000040000 <x-coredata:/

"<Car_Doc_Safe_Plus.CarName: 0x7fd783ce3130> (entity: CarName; id: 0xd000000000080000 <x-coredata://5F052E60-F5D2-4129-9955-537074450B8B/CarName/p2> ; data: <fault>)"






This is works perfectly on first population of my NSMutableArray *sectionName , but when the Fetch request happens the app crashes.


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

  return  [sectionName objectAtIndex:section];
}




Any help / guidance is greatly appreciated.


JZ

When you want to create a new NSManagedObject, you should be doing it like so:


NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:@"CarName"
                                                           inManagedObjectContext:managedObjectContext];


There's also no reason to do all the string manipulation you are to save it. Just directly save sectionName. What you should *really* be doing though is creating an NSManagedObject subclass. In your code data model file, select all your entities and go to Editor -> Create NSManagedObject subclass. Then you can just do this:


CarName *car = [NSEntityDescription insertNewObjectForEntityForName:@"CarName"
                                             inManagedObjectContext:managedObjectContext];
car.carsname = [sectionName stringByStandardizingPath];



Similarly, your fetch would look like so. Notice how I changed to initWithEntityName and got rid of all your extra array allocations.


AppDelegate *appDel = [UIApplication sharedApplication].delegate; 
NSManagedObjectContext *context = appDel.managedObjectContext; 
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:"CarName"]; 


NSError *error = nil; 
NSMutableArray *results = [context executeFetchRequest:request error:&error]; 
if (error) {
   NSLog(@"Failed to fetch: %@", error);
} else if (results.count > 0) {
   CarName *car = results[0];
   sectionName = car.carsname;
}

You said you're using an NSMutableArray, but you really aren't at all, so I'm not sure what you mean. You're storing a CarName *entity* into Core Data, and so that's also what you fetch.

It might be results.length instead of results.count....I can't remember which off the top of my head.

This is great , thank you so much for your help.


Regards

JZ

Core Data Objective C
 
 
Q