代码库> 数据持久化> coredata的增删改查 -MyCoreData
coredata的增删改查 -MyCoreData
关注
查看次数:2594 下载次数:533
上传时间:2015-09-28 大小:867 B


- (IBAction)add:(id)sender {
    Player *playerObject = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:self.cdViewController.managedObjectContext];
    playerObject.name = self.nameTF.text;
    playerObject.age = [NSNumber numberWithInteger:[self.ageTF.text integerValue]];
    playerObject.team = self.team;
    
    [self.cdViewController saveContext];
    [self dismissViewControllerAnimated:YES completion:nil];
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        NSManagedObjectContext *tempContext = [[NSManagedObjectContext alloc]init];
//        [tempContext setPersistentStoreCoordinator:sharedPersistenStoreCoordinator];
//        
//        Player *playerObject = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:tempContext];
//        playerObject.name = self.nameTF.text;
//        playerObject.age = [NSNumber numberWithInteger:[self.ageTF.text integerValue]];
//        //playerObject.team = self.team;
//        playerObject.team = (Team *)[tempContext objectWithID:self.teamID];
//        NSError *error = nil;
//        if (tempContext && [tempContext hasChanges] && ![tempContext save:&error]) {
//            NSLog(@"Error %@, %@",error,[error localizedDescription]);
//            abort();
//        }
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [self dismissViewControllerAnimated:YES completion:nil];
//        });
//        
//    });
    
}
- (void)mocDidSaveNotification:(NSNotification *)notification
{
    NSManagedObjectContext *saveContext = [notification object];
    
    if (saveContext == self.managedObjectContext ) {
        return;
    }
    if (saveContext.persistentStoreCoordinator != self.persistentStoreCoordinator) {
        return;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Merge changes from other contex.
");
        [self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
    });

}




测试环境:Xcode 6.2,iOS 6.0 以上
收藏
我来说两句
发表评论
您还没有登录!请登录注册
所有评论(0
提示
sina weixin mail 回到顶部