在核心数据中创建和编辑对象 - 一些最着名的书籍的3种不同方法

时间:2013-09-06 16:45:12

标签: ios objective-c core-data parameter-passing

我一直在阅读一些关于使用核心数据进行保存的帖子。我注意到大多数时候每个人都忘记了最困难的部分是编辑对象而不是创建。

我阅读了3本关于核心数据的不同书籍,我将与大家分享这些书籍使用的所有方法。我很想知道你会为自己的申请申请哪种方法。



核心数据iOS Essentials - Packt Publishing

是一个RootVC,一个AddCustomerVC和一个EditCustomerVC。我们想要创建或编辑客户

1)用户单击RootVC中的ADD按钮 2)add按钮方法创建一个对象并将其设置为DetailVC的Object * myobject变量

-(IBAction) addCustomer { 
    AddCustomerVC *addVC = [AddCustomerVC alloc]init];
    addVC.customer = (Customer *) [NSEntityDescription insertNewObjectForEntityForName:@"Customer" inManagedObjectContext:self.managedObjectContext];
    addVC.delegate = self;
    [self.navigationController presentModalViewController:addVC animated:YES];
}


3)detailVC设置Customer实例属性并调用委托方法

-(IBAction) save {
    customer.name = newName.text;
    customer.surname = newSurname.text;
    [delegate addVC:self selectedSave:YES];
}


4)如果用户在addVC中按“取消”,则操作将调用委托方法

-(IBAction) cancel {
    [delegate addVC:self selectedSave:NO];
}


5)RootVC委托实现检查用户是否保存并保存上下文

-(void) addVC:(UIViewController *)controller selectedSave:(BOOL)save {
    if(!save) {
        [self.managedObjectContext deleteObject:controller.customer];
    }
    NSError *error = nil;
    if( ! [self.managedObjectContext save:&error] ) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }
    [self dismissModalViewControllerAnimated:YES];
}


1编辑)用户单击单元格并调用单元格选择的方法,在该方法中创建具有所选值的Customer并打开EditVC

-(void)tablewView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    EditCustomerVC editVC = [[EditCustomerVC alloc]init];
    Customer *selectedCustomer = (Customer *) [self.fetchedResultsController objectAtIndexPath:indexPath];
    editVC.customer = selectedCustomer;
    [self.navigationController pushViewController:editVC animated:YES];
}


2编辑)用户按下保存按钮,设置客户的值,并调用代理

-(IBAction) save {
    customer.name = name.text;
    customer.surname = surname.text;
    [delegate editVC:self selectedSave:YES];
}


3b)如果用户按下取消,则调用代理

-(IBAction) cancel {
    [delegate editVC:self selectedSave:NO];
}


4b)委托保存已编辑的对象

.(void)editVC:(UIViewController *)controller selectedSave:(BOOL)save {
    if(save) {
        NSError *error = nil;
        if( ! [self.managedObjectContext save:&error] ) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        }
    [self dismissModalViewControllerAnimated:YES];
}


适用于iOS的专业核心数据 - Apress

我们有MasterVC和TeamVC,我们可以在其中创建和编辑团队

1)当用户点击ADD按钮时,它会调用showTeamVC将MasterVC和团队传递给它

-(void)showTeamVC {
    TeamVC *teamVC = [[TeamVC alloc]initWithController:self andTeam:nil];
    [self presentModalViewController:teamVC animated:YES];
}


2)TeamVC的init将传递的值设置为其iVar

-(id)initWithController:(UIViewController *)controller andTeam:(Team *)team {
    self.controller = controller;
    self.team = team;
}


3)viewDidLoad使用从对象获得的值来设置视图的字段 4)当用户按下取消时,控制器刚被解雇 5)当用户点击保存时,它会调用保存操作,检查团队是否为零(以查看您是否通过了团队,因此,如果您要添加或编辑)并调用主控制器的保存或插入方法

-(IBAction)save {
    if ( team != nil ){
        team.name = nameTextField.text;
        team.colors = colorsTextField.text;
        [controller saveContext];
    } else {
        [controller insertTeamWithName: nameTextField.text andColors:colorsTextField.text];
    }
    [self dismissModalViewControllerAnimated:YES];
}


6)如果您保存了一个新团队,它将被称为MasterVC中的insertTeamWithName:andColors方法

-(void)insertTeamWithName:(NSString *)name andColors:(NSString *)colors {
    NSManagedObjectContext *contextx = [self.fetchedResultsController managedObjectContext];
    Team *newTeam = [NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:context];
    [self saveContext];
} 


7)如果您编辑了一个团队,它将在MasterVC中被称为saveContext方法

-(void)saveContext {
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectcontext];
    NSError *error = nil;
    if ( ! [context save:&error] ) {
        NSLog(@"Error");
    }
}  


iOS核心数据 - 核心框架系列

这被认为是核心数据的最佳书之一,即使它已经过时了 这有一个完全不同的方法

我们有一个RootVC和一个PersonEditorVC,我们可以添加和编辑Person

1)当按下rootVC按钮add时,它调用PersonEditorVC上的setCurrentPerson:nil方法并打开该视图

- (void)addNewPerson {
    [self.personEditorViewController setCurrentPerson:nil];

    [self.navigationController pushViewController:self.personEditorViewController animated:YES];
}


2)PersonEditorVC方法setCurrentPerson由前一个方法调用。如果person为nil则调用Person初始值设定项,如果person不是nil,则获取其objectID

- (void)setCurrentPerson:(AWPerson *)aPerson {
    if( !aPerson )
    {
        self.title = @"Add person";
        aPerson = [AWPerson 
                   personInManagedObjectContext:self.editingContext];
    }
    else if( [aPerson managedObjectContext] != self.editingContext ) {
        self.title = @"Edit person";

        aPerson = (id)[self.editingContext 
                       objectWithID:[aPerson objectID]];
    }

    if( currentPerson != aPerson ) {
        [currentPerson release];
        currentPerson = [aPerson retain];
    }
} 


3)PersonEditorVC打开n设置其文本字段如果该人不是nil,则从每个textFieldShouldReturn

的文本字段中获取编辑的值
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if( textField == self.firstNameTextField )
        [self.currentPerson setFirstName:textField.text];
    else if( textField == self.lastNameTextField )
        [self.currentPerson setLastName:textField.text];

    [self becomeFirstResponder];
}


4)当用户按下取消时,它只会返回旧控制器,如果按下保存,则只保存上下文

- (IBAction)savePerson:(id)sender {
    NSError *anyError = nil;
    BOOL success = [[currentPerson managedObjectContext] 
                    save:&anyError];
    if( !success ) {
        UIAlertView *errorAlert = [[UIAlertView alloc] 
                                   initWithTitle:@"Couldn't save this person"
                                   message:[anyError localizedDescription] 
                                   delegate:self cancelButtonTitle:@"OK" 
                                   otherButtonTitles:nil];

        [errorAlert show];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


5)重要的是要注意它在自动生成的模型类中编写的初始化程序

+ (id)personInManagedObjectContext:(NSManagedObjectContext *)moc {
    return [NSEntityDescription 
            insertNewObjectForEntityForName:@"Person"
            inManagedObjectContext:moc];
}


1编辑)如果用户想要编辑一个人并点击表格中的人物行,它就会被称为tableview方法,它只需要一个人调用setCurrentPerson

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AWPerson *selectedPerson = [self.fetchedResultsController objectAtIndexPath:indexPath];

    [self.personEditorViewController setCurrentPerson:selectedPerson];

    [self.navigationController pushViewController:self.personEditorViewController animated:YES];
}



结论

这里我们有3种完全不同的方法,来自3本关于核心数据的最佳书籍。

您最喜欢的方法是什么?你用另一个吗?为什么你比其他人更喜欢?

我个人认为最后一个是最好的,即使它可以更容易编码,它肯定是最好的语法,更可重复使用等等。 但对于一个小应用程序,我可能会使用第一个。

0 个答案:

没有答案