user3140296
user3140296

Reputation: 169

Retrieving data from entity

i'm trying to retrieve data from my entity by placing it in a array. After testing it i can see that the objects are added to my array. The problem is that they wont be added to the tableview.

when i use device(the array) count. it says that there are 20 objects, which means that the saveTap works.

entityenter image description here

Why wont they fetch to the tableview?

.h

@interface PrivateViewController : UIViewController<UITableViewDataSource,     UITableViewDelegate, NSFetchedResultsControllerDelegate>{


}
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) IBOutlet UITableView *tableViewData;
@property int selectedRowValue;

-(IBAction)saveTap:(id)sender;

.m

Array property

@interface PrivateViewController ()

@property (strong) NSMutableArray *devices;

@end

rest of the .m

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Entity"];
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

    [self.tableViewData reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.devices.count;
}

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

// Configure the cell...
    NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"%@", [device     valueForKey:@"playlistName"]];

    return cell;
}

.m saveTap

}

-(void)saveTap:(id)sender{
    Entity *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:_managedObjectContext];


    [newManagedObject setValue:textfield.text forKey:@"playlistName"];
    [newManagedObject setValue:[NSNumber numberWithInt:selectedRowValue] forKey:@"idnumber"];


    // Save the context.
    NSError *error = nil;
    if (![_managedObjectContext save:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    } else {

    }
     [self.tableViewData reloadData];

    NSLog(@"%d", [_devices count]);
}

Upvotes: 0

Views: 63

Answers (1)

tilo
tilo

Reputation: 14169

There are multiple possible reasons for your problem:

  • Does your tableView display 20 empty rows? If so: did you really set a value for playlistName?
  • There are no rows at all: did you set the dataSource of your tableViewData?

Edit: you are doing the fetch on your managedObjectContext in viewDidAppear:, so self.devices will not be updated afterwards. Try to move this to a separate method (or to saveTap:) and call this when reloading the tableView.

Edit2: As your tableView is now correctly displaying the (number of) cells: did you make sure that textfield.text in saveTap: holds a proper value?

Upvotes: 1

Related Questions