Updating a table using master data

I am having problems reloading my master data after inserting a new item that I tried, but without joy. I was wondering if this could be due to the way I name the data? Im very new to this and have been following various tutorials to go this far, but I'm stuck now. I know that viewdidappear is called as nslog claims this, however the table is not updated

`- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
     [self.tableView reloadData];

    NSLog (@"did appear");
}`

Any ideas?

//  ViewController.m
//  Core Data

#import "ViewController.h"
#import "AppDelegate.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UINavigationItem *back;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *name;
@property (strong, nonatomic) NSMutableArray *phone;

@end

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];



    self.name = [[NSMutableArray alloc] init];
    self.phone = [[NSMutableArray alloc] init];

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];
//    NSPredicate *pred =[NSPredicate predicateWithFormat:@"(name = %@)", @"Vea Software"];
//    [request setPredicate:pred];
    NSManagedObject *matches = nil;

    NSError *error;
    NSArray *objects = [context executeFetchRequest:request
                                              error:&error];

    if ([objects count] == 0)
    {
        NSLog(@"No matches");
    }
    else
    {
        for (int i = 0; i < [objects count]; i++)
        {
            matches = objects[i];
            [self.name addObject:[matches valueForKey:@"name"]];
            [self.phone addObject:[matches valueForKey:@"phone"]];
        }
    }

}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
     [self.tableView reloadData];

    NSLog (@"did appear");
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)add:(id)sender
{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSManagedObject *newContact;
    newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:context];

    addentry = self.addtextbox.text;

    [newContact setValue: addentry forKey:@"name"];
    [self.name addObject: addentry];
    [newContact setValue: @"(555) 555 - 5555" forKey:@"phone"];
    [self.phone addObject:@"(555) 555 - 5555"];
    NSError *error;
    [context save:&error];
    [self.tableView reloadData];
}

#pragma Table View

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.name.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = [self.name objectAtIndex:indexPath.row];
    return cell;
}



@end
+3
source share
1 answer

Take a look at this tutorial http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller

You need to rethink the architecture of your view controller. In general, you want:

  • " ", , , . " ", .

  • . Grand Central Dispatch , , NSOperation, ..

  • NSFetchedResultsController . Core Data​​strong > .

, , . .

!

+2

All Articles