0

I have a modal view controller, where the user can enter or edit data. There is a save and a cancel-button. So, i created a new NSManagedObjectContext for this viewController and store it in property.

self.controllerContext = [NSManagedObjectContext MR_context];

Then, while the user edits or enters data, i create entites and values for these entites into the controllerContext.

When the user hits the cancel-button i do the following:

- (void)cancelButtonClicked {
    [self.controllerContext rollback];
    [self.controllerContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
        if (success || error == nil) {
            [self dismissViewControllerAnimated:YES completion:nil];
        } else {
            if (error) {
                [ErrorUtil logError:error fromAction:@"TagebuchDetailView Cancel"];
            }
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}

This works pretty good and none of the values is saved. when the user hits the save-button, i do the following:

- (void)saveButtonClicked {
     //Last modified setzen
    self.selectedEintrag.lastModified = [NSDate date];
    self.selectedEintrag.isDirty = [NSNumber numberWithBool:YES];

    //Save Context and dismiss
    self.subTitleView.navigationBarSubtitle = NSLocalizedString(@"view.subtitle.saving", nil);
    [self.controllerContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
        if (success || error == nil) {
            [self dismissViewControllerAnimated:YES completion:nil];
            self.subTitleView.navigationBarTitle = nil;
        } else {
            if (error) {
                [ErrorUtil logError:error fromAction:@"TagebuchDetailView Save"];
            }
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}

That works fine as well, but the save for 2-10 entites takes about 5-10 seconds on an iPhone4 and 3-5 on an iPhone5. This seems to be pretty long for such a small amount of saved data. I recently updated to MagicalRecord 2.2 Release.

Any ideas what this could be and how to improve the saving performance? Maybe a bug in 2.2? I used previous versions of Magicalrecord and the saving performace got bad lately. I am not sure if its magicalrecord update or testing on iOS7 lately.

4

1 回答 1

0

So i found this out myself. the problem was, that the controllerContext was a child-context of the default context. In another view, i had a fetchedresultcontroller listening on the default context. When saving the controller context, it seems like they blocked each other. My fix was, to set controllers parent context to the root saving context.

self.controllerContext = [NSManagedObjectContext MR_contextWithParent:[NSManagedObjectContext MR_rootSavingContext]];

Then, everything works like charm.

于 2013-11-04T22:03:33.623 回答