I am trying to display items in a tableView, but my fetchResult Array does not show anything. I get this NSLog:
2013-09-13 13:02:01.088 Prototype[67018:c07] managed object ()
YPProjectListVC.h
@interface YPProjectListViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end
YPProjectListVC.m
@interface YPProjectListViewController () {
SelectionSuccessBlock successBlock;
}
@property (nonatomic,strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * data;
@property (nonatomic, strong) UIRefreshControl *spinner ;
@end
@implementation YPProjectListViewController
@synthesize tableView;
@synthesize data;
@synthesize spinner;
@synthesize managedObjectContext;
Here is my request:
- (void)viewDidLoad {
[super viewDidLoad];
spinner = [[UIRefreshControl alloc]initWithFrame:CGRectMake(130, 10, 40, 40)];
managedObjectContext = [YPDataSingleton context];
if(managedObjectContext){
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]];
NSError *error;
self.data = [[NSMutableArray alloc] init];
NSArray *fetchResults = [managedObjectContext executeFetchRequest:request error:&error];
if (error != nil) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
data=[fetchResults mutableCopy];
NSLog(@"managed object %@",fetchResults);
[self.tableView reloadData];
} else {
[self loadProjectsFromService];
[spinner addTarget:self action:@selector(loadProjectsFromService) forControlEvents:UIControlEventValueChanged];
[tableView addSubview:spinner];
}
}
Here is where I get my Projects from the services if NSManagedObjectContext is empty.
-(void)loadProjectsFromService{
[spinner beginRefreshing];
self.data = [[NSMutableArray alloc] init];
[self.tableView reloadData];
[self.view addSubview:self.tableView];
__weak typeof(self) weakSelf = self;
successBlock = ^(NSDictionary *newData) {
if ([newData count] > 0) {
[weakSelf refreshData:newData];
}
};
[spinner endRefreshing];
[ypNetManager getProjectListWithSuccessBlock:successBlock error:NULL];
}
pragma mark - Custom getter
- (UITableView *)tableView {
//custom init of the tableview
if (!tableView) {
// regular table view
tableView = [[UITableView alloc] initWithFrame:UIEdgeInsetsInsetRect(self.view.bounds, tableViewInsets) style:UITableViewStylePlain];
tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
return tableView;
}
return tableView;
}
#pragma mark - Private methods
- (void)refreshData:(NSDictionary *)newData {
for (NSDictionary *projectDic in newData) {
[data addObject:[Project createProjectWithDictionary: projectDic inManagedObjectContext:managedObjectContext]];
}
[self.tableView reloadData];
}