核心数据 - 数据获取

时间:2014-12-16 12:15:29

标签: ios core-data storyboard xcode6

我在我的应用中使用了故事板和coredata。在主屏幕中,我想列出我的所有音频文件详细信息,并在按钮单击中我想导航到另一个屏幕并记录新文件并将该文件保存在数据库中。当按下后退按钮我到达主屏幕,我想在tableview中显示保存的音频文件。表行计数正确显示。但音频细节不正确。但是,当我停止并重建应用程序时,它正确显示。我编写了用于在viewWillApper中重新加载tableview的代码。

- (void)viewWillAppear:(BOOL)animated
{
    [tableview reloadData];
    [self loadFiles];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"AudioDetails" inManagedObjectContext:managedObjectContext]];

    [request setIncludesSubentities:NO];     NSError *err;
    NSUInteger count = [managedObjectContext countForFetchRequest:request error:&err];
    return count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"AudioCell";
    AudioCell *cell = (AudioCell *)[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
        cell = (AudioCell *)[nibArray objectAtIndex:0];
        [cell configurePlayerButton];
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"AudioDetails" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

    for (NSManagedObject *info in fetchedObjects) {
        if(audionames == nil)
            audionames = [[NSMutableArray alloc] init];
        [audionames addObject:[info valueForKey:@"audioName"]];


        //NSManagedObject *details = [info valueForKey:@"details"];
        //NSLog(@"Zip: %@", [details valueForKey:@"zip"]);
    }
     cell.titleLabel.text = [audionames objectAtIndex:indexPath.row];

    return cell;
}
- (IBAction)SaveRecordingBtnClicked:(id)sender {

       NSString *soundFilePath = [[self getDocumentDirectoryPath]
                               stringByAppendingPathComponent:OrginalAudioFileName];
    NSURL *path=[NSURL URLWithString:soundFilePath];
    NSError *error = nil;
    AVAudioPlayer* avAudioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:path error:&error];

    NSManagedObjectContext *context = [self managedObjectContext];
    AudioDetails *audioDetails = [NSEntityDescription
                                       insertNewObjectForEntityForName:@"AudioDetails"
                                       inManagedObjectContext:context];

    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:soundFilePath error:&error];
    NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];


    NSDate *fileCreationDate = [fileAttributes objectForKey:NSFileCreationDate];
    int duration = avAudioPlayer.duration;

    [audioDetails setValue:[NSString stringWithFormat:@"%@",OrginalAudioFileName] forKey:@"audioName"];

    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }

}

0 个答案:

没有答案