1

Every time I try to save my NSManagedObjectContex, it takes 1-8 seconds each time. Here is my code:

- (IBAction)save
{    
    if (self.managedObjectContext == nil) {
        self.managedObjectContext = [(RootAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    }

    if (self.brandText.text.length == 0 || self.modelText.text.length == 0) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please fill out the required fields" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(125, 40, 31, 7)];

        NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bullet.png"]];
        UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
        [imageView setImage:bkgImg];

        [alertView addSubview:imageView];

        [alertView show];

    } else {
        Hand *a = [NSEntityDescription insertNewObjectForEntityForName:@"Hand" inManagedObjectContext:self.managedObjectContext];
        a.brand = self.brandText.text;
        a.caliber = self.cText.text;
        self.managedObjectContext = self.app.managedObjectContext;
        a.notes = self.notesView.text;
        a.serialNumber = self.serialNumberText.text;
        a.nickname = self.nicknameText.text;
        a.model = self.modelText.text;
        a.gunImage = self.image.image;
        a.roundsFired = [NSString stringWithFormat:@"0"];
        a.cleanRounds = [NSString stringWithFormat:@"500"];
        a.showBadge = [NSNumber numberWithBool:YES];
        [self dismissViewControllerAnimated:YES completion:nil];

        NSError *error;     
        if (![self.managedObjectContext save:&error]) {
            UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"There was an internal error. \n Please restart the app and try again, Thank You" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
            [errorAlert show];
        }
    }
}

The code just saves the all of the textFields text. What is the reason its so slow? Any help is greatly appreciated.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Bad_APPZ
  • 432
  • 1
  • 3
  • 12

1 Answers1

2

Based on your question is quite difficult to understand what is going on but I would modify the else statement as follows...

else {
    Hand *a = [NSEntityDescription insertNewObjectForEntityForName:@"Hand" inManagedObjectContext:self.managedObjectContext];
    a.brand = self.brandText.text;
    a.caliber = self.cText.text;
    // why this?
    // self.managedObjectContext = self.app.managedObjectContext;
    a.notes = self.notesView.text;
    a.serialNumber = self.serialNumberText.text;
    a.nickname = self.nicknameText.text;
    a.model = self.modelText.text;
    a.gunImage = self.image.image;
    a.roundsFired = [NSString stringWithFormat:@"0"];
    a.cleanRounds = [NSString stringWithFormat:@"500"];
    a.showBadge = [NSNumber numberWithBool:YES];        

    NSError *error;     
    if (![self.managedObjectContext save:&error]) { // error during saving
        UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"There was an internal error. \n Please restart the app and try again, Thank You" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
        [errorAlert show];
    } else { // the save completes correctly, dismiss the view controller
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

If you want to monitor Core Data you should do it through Instrument. In addition you could set up Xcode as follows: XCode4 and Core Data: How to enable SQL Debugging.

Edit

Since the slow saving is due (based on your comment) to images, you should relies on these rules.

Core Data - Storing Images (iPhone)

Edit 2

Ok, since you don't know in advance about the size (in bytes) of your image, I really suggest to store the image in the filesystem and not in the Core Data store. In the db save only the path to your image. The image will be saved in background. This to prevent the UI to block.

Otherwise, if iOS 5 is the minimum requirement, use the External Storage flag. How to enable External Storage for Binary Data in Core Data

Hope that helps.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • thank you for the help, and i have boiled the reason as to the saving being slow is because of the `a.gunImage = self.image.image;` it is having a problem saving the image. I have the `gunImage` attribute in my data model as `transformable` and I am using a NSValueTransformer class to serialize/deserialize the image and save it to core data. But for some reason it is going very slow! – Bad_APPZ Jan 01 '13 at 21:40
  • 1
    @Bad_APPZ You're welcome. How big the image is? I've added an edit for you. Also fixed the grammar. ;) – Lorenzo B Jan 01 '13 at 21:45
  • How can i figure out how big it is? I just allow the user to select it from the photo library, or take it with the camera! – Bad_APPZ Jan 01 '13 at 22:03