Reputation: 2013
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];
}
- (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];
}
Upvotes: 1
Views: 138
Reputation: 19869
You should use NSFetchedResultsController, It will to the job for you, feed and update your table view carefully.
Look here:Core Data Tutorial for iOS: How To Use NSFetchedResultsController
Edit
I still think that you should use above but for now, I can understand that you are not implementing to the delegate and the data source methods of the table view?
Basicly you need to impliment the methods that tells the table view:
Data source
The data source is what you need to give to the table view, for example:
There are more but those are the basic methods.
Delegate
WHen you are conforming as the delegate of the table view, you simply give the table view a way to inform you about things like:
and so on...
It is long explanation for here tough it is quite simple, and there are a lot of tutorials that will help you.
try looking here:
Apple example project for tableViews
And here
Upvotes: 1