Inserting data within our Core Data data model

In this recipe we will learn how to write to the data fields contained within our core data model.

Getting ready

Following on from our previous recipe, we will learn how simple it is to update the contents of our Core Data database fields with some data.

How to do it...

To begin, follow these simple steps as outlined in the following order:

  1. Open the BooksViewController.m implementation file from the Project Navigator window.
  2. Modify the btnAdd: method as shown in the following code snippet.
    #pragma mark method called when the user presses the Add button
    -(IBAction)btnAdd:(id)sender{
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add 
       Book Details" message:@"
    
    
    
    " delegate:self
       cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
       bookTitle = [[UITextField alloc]
       initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
       [bookTitle setPlaceholder:@"Book Title:"];
       [bookTitle setBackgroundColor:[UIColor whiteColor]];
       [alert addSubview:bookTitle];
       bookAuthor = [[UITextField alloc] 
       initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)];
       [bookAuthor setPlaceholder:@"Author:"];
       [bookAuthor setBackgroundColor:[UIColor whiteColor]];
       [alert addSubview:bookAuthor];
       bookPublisher = [[UITextField alloc] 
       initWithFrame:CGRectMake(12.0, 80.0+35.0, 260.0, 25.0)];
       [bookPublisher setPlaceholder:@"Publisher:"];
       [bookPublisher setBackgroundColor:[UIColor whiteColor]];
       [alert addSubview:bookPublisher];
    
       // Show our Alert Dialog
       [alert show];
    }
  3. Next, create the following code sections, as specified in the code snippet.
    #pragma mark method called and uses the data entered by the btnAdd method
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
       NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
       if ([title isEqualToString:@"OK"]) {
          // Set a pointer to our Books database table schema
          Books *book = (Books *)[NSEntityDescription
          insertNewObjectForEntityForName:@"Books" 
          inManagedObjectContext:managedObjectContext];
    
          NSMutableArray *array = [[fetchedResultsController
          fetchedObjects] mutableCopy];
          
          // Assign our text fields to each of their attributes
          [book setTitle:bookTitle.text];
          [book setAuthor:bookAuthor.text];
          [book setPublisher:bookPublisher.text];
          [book setValue:[NSNumber numberWithInt:[array count] == 0 ? 
          0 : ([array count] + 1)] forKey:@"displayOrder"];
          
          NSError *error;
          if (![managedObjectContext save:&error]) {
             // Record could not be saved error message
             UIAlertView *alertView = [[UIAlertView alloc] 
             initWithTitle:@"Book Details"
             message:@"There was a problem saving the book details."
             delegate:self
             cancelButtonTitle:@"OK"
             otherButtonTitles:nil];
             
             [alertView show];
          }
          [self populateBookDetails];
       }
    }

How it works...

In this recipe, we start by dynamically creating each of our fields that will be accepting user input and positioning these within a UIAlertView dialog, and then adding this as a subview to the initial view controller. Next, we check to see if we have pressed the OK button before we create a managed object context that is used to create a new managed object using the Books entity description.

We use the getters and setters method for each of the schema fields of the managed object to set each of the attributes values of the managed object based on what has been entered by the user, earlier. Finally the context is instructed to save the changes to the persistent store, with a call to the context's save method. Any errors detected during the save operation to our Core Data data model will be displayed within the UIAlertView dialog box. To conclude, we refresh the table view to show that the new record was added.

See also

  • The Delete an item from the Table View using Core Data recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset