0

I have a case where I should insert object into an entity via UIViewController. I have designed my database model (Entity and attributes). I'm adding the entity through a UIViewController. What am I supposed to add in the didFinishLaunchingwithOptions method in appDelegate.m?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch   
    return YES;
}

And for the TUTViewController (My own view controller - UIViewController) I have used the below code for inserting object.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    NSString *stripped1 = [response stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    NSMutableArray *rows = [NSMutableArray arrayWithArray:[stripped1 componentsSeparatedByString:@"\n"]];
    NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:[rows count]]; 
    NSMutableArray *contentArray1 = [NSMutableArray arrayWithCapacity:[rows count]];

    NSArray *components;
    NSLog(@"count:%d",[rows count]);

    for (int i=0;i<[rows count]; i++) {

       if(i == 0 || [[rows objectAtIndex:i] isEqualToString:@""]){
          continue;
       }

       components = [[rows objectAtIndex:i] componentsSeparatedByString:@","];
       id x = [components objectAtIndex:0] ;
       id y = [components objectAtIndex:1];
       id z = [components objectAtIndex:2];

       [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@"X",y,@"Y", nil]];
       [contentArray1 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@"X",z,@"Y", nil]];

       [newManagedObject setValue:[x] forKey:@"timeStamp"];
       [newManagedObject setValue:[y] forKey:@"beat"];
       [newManagedObject setValue:[z] forKey:@"rate"];

       // Save the context.
       NSError *error = nil;
       if (![context save:&error]) {

           NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
           abort();

           NSLog(@"Contents of Uterus Contraction: %@",contentArray);
           NSLog(@"Contents of Heart Beat: %@",contentArray1);
       }    
    }
}

Is there anything that I'm missing? I'm ending up with the error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'FetalData''

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
DesperateLearner
  • 1,115
  • 3
  • 19
  • 45

1 Answers1

0

Did you set up the Core Data Stack or a UIManagedDocument object?

If you didn't set up the Managed Object Model this could be the problem. It means you're probably not loading Managed Object Model that defines FetalData entity. See insertnewobjectforentityforname for further info.

I really suggest to create an empty project and let Xcode to create Core Data stuff for you. In this manner you can see how the code works.

Some Notes

Why do you use a NSFetchResultsController?

Move the save call at the end of your method. In this manner you avoid multiple round trips to the disk.

If you want to start using Core Data, I suggest you core-data-on-ios-5-tutorial-getting-started.

Hope it helps.

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