I have an UITableView with menu items, and when the menu changes the server pushes the new list to the devices. The table view which shows the list automatically updates when I add menu items or change them when the changes are committed.
However, when I delete an object then this is not reflected in the list (and neither when I change the order, which means a 'level' field of the object).
These changes only show after I completely close (kill) the App and restart.
Is there a simple reason why the update is not triggered?
for(DOMenuItem* item in _oldMenuIdList){
[[self managedObjectContext] deleteObject:item];
}
NSError *error;
if(![[self managedObjectContext] save:&error]) {
NSLog(#"Error!");
}
[EDIT:]
#interface DOMenuItem : NSManagedObject {
}
#property (nonatomic, strong) NSNumber * id;
#property (nonatomic, strong) NSString * action;
#property (nonatomic, strong) NSString * title;
#property (nonatomic, strong) NSString * section;
#property (nonatomic, strong) NSNumber * level;
#property (nonatomic, strong) NSNumber * published;
#property (nonatomic, strong) NSString * icon;
#property (nonatomic, strong) NSDate * lastUpdate;
#property (nonatomic, strong) DOMenuItem * parent;
#property (nonatomic, strong) DOLang * language;
#property (nonatomic, strong) NSSet* children;
#property (nonatomic, strong) NSSet* groups;
#end
#interface DOMenuItem (CoreDataGeneratedAccessors)
- (void)addChildren:(NSSet *)value;
- (void)removeChildren:(NSSet *)value;
- (void)addGroups:(NSSet *)value;
- (void)removeGroups:(NSSet *)value;
- (void)addGroupsObject:(DOGroup *)value;
- (void)removeGroupsObject:(DOGroup *)value;
#end
-
- (NSFetchedResultsController *) createFetchedResultsController:(NSString*) cachName sectionString:(NSString*)sectionString sortBySection:(bool)sortBySection {
// Set up the fetched results controller (object who requests will own).
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:_objectName inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:_defaultSortField ascending:_sortAscending];
NSMutableArray *sortDescriptors = [[NSMutableArray alloc] init];
if(sortBySection && _sectionNameKeyPath != nil){
// Add section sorting at beginning
NSSortDescriptor *sortSectionDescriptor = [[NSSortDescriptor alloc] initWithKey:sectionString ascending:NO];
[sortDescriptors addObject:sortSectionDescriptor];
}
[sortDescriptors addObject:sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchLimit:0];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:sectionString cacheName:cachName];
//[sortDescriptors release];
return aFetchedResultsController;
}
- (NSFetchedResultsController*) findChildrenOfMenu:(DOMenuItem*) parentMenu {
NSPredicate* predicate;
if(parentMenu == nil){
predicate = [NSPredicate predicateWithFormat:#"(((ANY groups.validTime > %#) && (ANY groups.active == YES)) || (ANY groups.universal == YES)) && (parent == NULL) && (language.enabled == YES)", [NSDate date]];
//predicate = [NSPredicate predicateWithFormat:#"(parent == NULL)"];
} else {
predicate = [NSPredicate predicateWithFormat:#"(((ANY groups.validTime > %#) && (ANY groups.active == YES)) || (ANY groups.universal == YES)) && (parent == %#) && (language.enabled == YES)", [NSDate date], parentMenu];
}
[NSFetchedResultsController deleteCacheWithName:_cacheName];
NSFetchedResultsController* aFetchedResultsController = [self createFetchedResultsController:_cacheName sectionString:_sectionNameKeyPath sortBySection:YES];
[aFetchedResultsController.fetchRequest setPredicate:predicate];
[aFetchedResultsController.fetchRequest setFetchLimit:0];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:_defaultSortField ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[aFetchedResultsController.fetchRequest setSortDescriptors:sortDescriptors];
// Fetch the records and handle an error
NSError *fetchError;
if (![aFetchedResultsController performFetch:&fetchError]) {
// Handle the error.
// This is a serious error and should advise the user to restart the application
NSLog(#"Fetching data error: %#", [fetchError localizedDescription]);
}
return aFetchedResultsController;
}
[Edit 2:]
In the UIViewController:
- (void)viewDidLoad
{
iDomsAppDelegate *delegate = (iDomsAppDelegate *)[[UIApplication sharedApplication] delegate];
// Get the menu's
_objects = [[[DOMenuItemsController alloc] init:[delegate managedObjectContext]] findChildrenOfMenu:(DOMenuItem*) _parentMenu];
[_objects setDelegate:self];
}
[EDIT 3:]
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)aSectionIndex forChangeType:(NSFetchedResultsChangeType)type {
NSUInteger sectionIndex = aSectionIndex;
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeMove:
NSLog(#"NEED TO LOOK AT THIS!");
break;
case NSFetchedResultsChangeUpdate:
NSLog(#"NEED TO LOOK AT THIS!");
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)aIndexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)aNewIndexPath {
UITableView *tableView = [self tableView];
switch(type) {
case NSFetchedResultsChangeInsert: {
//NSLog(#"Inserted!");
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:aNewIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
break;
case NSFetchedResultsChangeDelete:{
//NSLog(#"Delete");
// Skip if previous in section had same commonId
NSLog(#"Table delete... r:%li - s:%li", (long)aIndexPath.row, (long)aIndexPath.section);
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:aIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
break;
case NSFetchedResultsChangeUpdate:
//NSLog(#"Update menu");
[(DOPrototypeCell*) [tableView cellForRowAtIndexPath:aIndexPath] populateCell:[_objects objectAtIndexPath:aIndexPath]];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:aIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeMove:{
//NSLog(#"Move");
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:aIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:aNewIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
#try {
[self.tableView endUpdates];
} #catch (NSException *exception) {
NSLog(#"Caught error on finding tabelview update: %#", exception.description);
[self.tableView reloadData];
}
[self checkViewheight];
}
Related
I would like to explain the most detailed the problem I'm facing.
Previously instead of the UIViewController i had directly a UITableViewController, the problem is that now i need to show more content in the view that just the tableview so i created a new view. Now i have a UIViewController that has: UIScrollView. Enside of it there are: 1. UIView + 1 UITableView
I'm using AutoLayout. And is working fine. The problem I'm facing is that since i switched to use this view I'm unable to display content in the table with coredata. I was able to do it without coredata but not with it.
In the interfacebuilder i set datasource and delegate to this UIViewController.
Header:
#import <UIKit/UIKit.h>
#interface ResultsExtraVC : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property(nonatomic, strong) AGTCoreDataStack *model;
#property (strong, nonatomic) IBOutlet UILabel *labelRaceTime;
#property (strong, nonatomic) IBOutlet UILabel *labelTrackCondition;
#property (strong, nonatomic) IBOutlet UILabel *labelRaceStarttime;
#property (strong, nonatomic) IBOutlet UILabel *labelRaceMode;
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (strong, nonatomic) IBOutlet UIView *raceInfoView;
#property(nonatomic, strong)NSNumber *eventID;
#property(nonatomic, strong)NSNumber *sectionID;
#property(nonatomic, strong)NSNumber *elementID;
#property(nonatomic, strong)NSNumber *categoryID;
#end
Implementation:
#import "ResultsExtraVC.h"
#import "ResultsExtraCellVC.h"
#import "Result.h"
#import "Element.h"
#import "Section.h"
#import <CoreData/CoreData.h>
#interface ResultsExtraVC () <NSFetchedResultsControllerDelegate>
//Core data
#property (nonatomic,strong) NSManagedObjectContext* managedObjectContext;
#property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
#end
static NSString * const cellIdentifier = #"resultsExrtaAltCell";
#implementation ResultsExtraVC
#synthesize managedObjectContext;
#synthesize fetchedResultsController = _fetchedResultsController;
#synthesize eventID, sectionID, elementID, categoryID, model;
- (void)viewDidLoad {
[super viewDidLoad];
managedObjectContext = self.model.context;
[self getResults];
NSLog(#"ElementName: %#", [self getElementName]);
NSLog(#"SectionName: %#", [self getSectionName]);
[self configureHeatInfo];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.fetchedResultsController = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - View Configuration
-(void) configureHeatInfo{
_scrollView.backgroundColor = [UIColor cyanColor];
_scrollView.translatesAutoresizingMaskIntoConstraints = NO;
_labelRaceTime.translatesAutoresizingMaskIntoConstraints = NO;
_labelRaceTime.text = #"Race time:\n10:00";
_labelRaceMode.translatesAutoresizingMaskIntoConstraints = NO;
_labelRaceMode.text = #"Mode: Groupstart";
_labelTrackCondition.translatesAutoresizingMaskIntoConstraints = NO;
_labelTrackCondition.text = #"Track Condition: Dry";
_labelRaceStarttime.translatesAutoresizingMaskIntoConstraints = NO;
_labelRaceStarttime.text = #"Starttime: 12.03.2016 11:09:10";
//self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
/*self.tableView.dataSource = self;
self.tableView.delegate = self;*/
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController sections] count];
}
/*- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
#ifndef NDEBUG
NSLog(#"%s (line:%d) - Rows: %lu",__PRETTY_FUNCTION__,__LINE__, (unsigned long)[sectionInfo numberOfObjects]);
#endif
// Return the number of rows in the section.
return [sectionInfo numberOfObjects];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return [self basicCellAtIndexPath:indexPath];
}
- (ResultsExtraCellVC *)basicCellAtIndexPath:(NSIndexPath *)indexPath {
ResultsExtraCellVC *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// Using a cell identifier will allow your app to reuse cells as they come and go from the screen.
if (cell == nil) {
#ifndef NDEBUG
NSLog(#"%s (line:%d)",__PRETTY_FUNCTION__,__LINE__);
#endif
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ResultsExtraCell" owner:self options:nil];
cell = (ResultsExtraCellVC *)[nib objectAtIndex:0];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(ResultsExtraCellVC *)cell atIndexPath:(NSIndexPath *)indexPath {
#ifndef NDEBUG
NSLog(#"%s (line:%d)",__PRETTY_FUNCTION__,__LINE__);
#endif
Result *result = [_fetchedResultsController objectAtIndexPath:indexPath];
[cell.cellRankNr setText:[NSString stringWithFormat:#"%#",[result endposition]]];
[cell.cellName setText:[NSString stringWithFormat:#"%# %# (%#)",[result pilotprename], [result pilotname], [result carnumber]]];
[cell.cellResult setText:[NSString stringWithFormat:#"%#",NSLocalizedFormatString(#"LapsXnTimeX",[result rlaps],[result rendtime])]];
[cell.cellRace setText:[NSString stringWithFormat:#"%#",NSLocalizedFormatString(#"BestTimeXnAvgTimeX",[result rbesttime], [result rmediumtime])]];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView respondsToSelector:#selector(setSeparatorInset:)])
{
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:#selector(setLayoutMargins:)])
{
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:#selector(setLayoutMargins:)])
{
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
-(NSString *)getElementName{
NSString *elementName = #"";
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"Elements" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init] ;
[request setEntity:entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(eventid = %# AND sectionid = %# AND elementid = %# AND categoryid= %#)",eventID, sectionID, elementID, categoryID];
[request setPredicate:predicate]; //added this line later
NSArray *array = [managedObjectContext executeFetchRequest:request error:nil];
if([array count]> 0){
Element *element = [array lastObject];
elementName = [element name];
}
return elementName;
}
-(NSString *)getSectionName{
NSString *sectiontName = #"";
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"Sections" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init] ;
[request setEntity:entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(eventid = %# AND sectionid = %#)",eventID, sectionID];
[request setPredicate:predicate]; //added this line later
NSArray *array = [managedObjectContext executeFetchRequest:request error:nil];
if([array count]> 0){
Section *section = [array lastObject];
sectiontName = [section name];
}
return sectiontName;
}
-(void)getResults{
// Initialize Fetch Request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:#"Results"];
// Add Sort Descriptors
//[fetchRequest setSortDescriptors:#[[NSSortDescriptor sortDescriptorWithKey:#"endposition" ascending:YES]]];
//Predicate
NSPredicate *commentsPredicate = [NSPredicate predicateWithFormat:#"(eventid = %# AND sectionid = %# AND elementid = %#)",eventID, sectionID, elementID];
[fetchRequest setPredicate:commentsPredicate]; //added this line later
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"endposition"
ascending:YES];
NSArray *sortDescriptors = #[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Initialize Fetched Results Controller
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
// Configure Fetched Results Controller
[self.fetchedResultsController setDelegate:self];
// Perform Fetch
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];
if (error) {
NSLog(#"Unable to perform fetch.");
NSLog(#"%#, %#", error, error.localizedDescription);
}
}
#pragma mark -
#pragma mark Fetched Results Controller Delegate Methods
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch (type) {
case NSFetchedResultsChangeInsert: {
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
case NSFetchedResultsChangeDelete: {
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
case NSFetchedResultsChangeUpdate: {
[self configureCell:(ResultsExtraCellVC *)[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
}
case NSFetchedResultsChangeMove: {
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
}
#end
My View looks like this:
I tried to follow some tutorials as this is the first time i'm doing this with CoreData, previously i always used only UITableViewController.
I followed this: http://code.tutsplus.com/tutorials/core-data-from-scratch-more-nsfetchedresultscontroller--cms-21777
And also checked: CoreData TableView inside UIViewController
But with no success.
I would be great if someone can help me with the problem.
Thanks in advance
Maybe in code line
// Initialize Fetched Results Controller
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
the sectionNameKeyPath is missing? It must be one of the NSSortDescriptor keys.
I'm implementing a UITableView with NSFetchedResultsController.
# D5ProductViewController.h
#interface D5ProductViewController : D5ViewControllerAbstract <UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate>
#property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, strong) D5Product *product;
#property (weak, nonatomic) IBOutlet UITableView *tableView;
- (IBAction)addVariantTapped:(id)sender;
#end
# D5ProductViewController.m
#interface D5ProductViewController ()
#property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
#end
#implementation D5ProductViewController
- (void)viewLoad {
NSError *error;
[self.fetcedResultsController performFetch:&error];
if (error) {
[self.alerts showError:[error localizedDescription]
:#"Error"];
}
}
#pragma mark - Properties
#synthesize managedObjectContext; // The context is passed from a parent view.
#synthesize fetchedResultsController = _fetchedResultsController;
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"Variant"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entityDescription];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"price"
ascending:NO];
[fetchRequest setSortDescriptors:#[sortDescriptor]];
NSString *predicateString = [NSString stringWithFormat:#"productId = '%#' AND isMaster = 0", self.product.identifier];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
[fetchRequest setPredicate:predicate];
[fetchRequest setReturnsObjectsAsFaults:NO];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
[self.tableView reloadData];
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:(D5VariantTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath]
withObject:(D5Variant *)[controller objectAtIndexPath:indexPath]];
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)configureCell:(D5VariantTableViewCell *)cell
withObject:(D5Variant *)variant {
cell.variant = variant;
}
#pragma mark - UITableViewDataSourceDelegate
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
id sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.fetchedResultsController.sections count];
}
#pragma mark - UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
D5VariantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:D5VariantCellReusableIdentifier
forIndexPath:indexPath];
D5Variant *variant = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.variant = variant;
cell.managedObjectContext = self.managedObjectContext;
return cell;
}
- (IBAction)addVariantTapped:(id)sender {
NSEntityDescription *variantEntityDescription = [NSEntityDescription entityForName:#"Variant"
inManagedObjectContext:self.managedObjectContext];
D5Variant *variant = [[D5Variant alloc] initWithEntity:variantEntityDescription
insertIntoManagedObjectContext:self.managedObjectContext];
variant.productId = self.product.identifier;
variant.price = [self.product.master price];
variant.weight = [self.product.master weight];
[variant markAsInserted];
NSError *error;
[self.fetchedResultsController performFetch:&error];
if (error) {
NSLog(#"%#", error);
}
[self.tableView reloadData];
}
#end
If I understand correctly, having implemented NSFetchedResultsController's controller:didChangeObject:atIndexPath:forChangeType:newIndexPath will cause UITableView to ask for a cell for the inserted row, which is configured with the object at the given indexPath from the fetchedResultsController.
The thing is that controller:didChangeObject...newIndexPath is never called when a new object is inserted in the managedObjectContext.
What am I missing? Do I need to call managedObjectContext's save method and then [self.tableView reloadData].
Thanks in advance!
Silly me, I forgot to set the UITableView's delegate:
The viewDidLoad method, besides setting the bindings, was just performing the fetch. Adding self.tableView.delegate = self; fixed the problem, BUT, isn't it enough setting the dataSource outlet in the IB? Which I already did. Why did I need to set the UITableView's delegate manually? In the case of the NSFetchedResultsController it needs to be set manually, but isn't it the purpose of the UITableView object having that outlet to just wire it up to the delegate object?
Anyways, setting self.tableView.delegate = self; fixed the problem.
I want to delete record from my tableviewcontroller cell.
The code seems fine to me, the exception came when i press a row to delete...
this is my code of ViewController.h file
#import <UIKit/UIKit.h>
#interface IMSCategoryViewController : UITableViewController
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property(nonatomic,retain)NSArray *arr;
#property (readonly, strong, nonatomic) NSMutableArray *categoryArray;
#end
And This one in the implementation file.
#import "IMSCategoryViewController.h"
#import "IMSAppDelegate.h"
#import "Category.h"
#interface IMSCategoryViewController ()
{
NSManagedObjectContext *context;
}
#end
#implementation IMSCategoryViewController
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
#synthesize categoryArray;
#synthesize arr;
- (void)viewDidLoad
{
[super viewDidLoad];
// [self.tableView reloadData];
IMSAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
context = [appDelegate managedObjectContext];
NSEntityDescription *category = [NSEntityDescription entityForName:#"Category" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:category];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"is_active == 1"];
[request setPredicate:predicate];
[request setFetchBatchSize:25];
[request setEntity:category];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
NSArray *srotDesc = [[NSArray alloc]initWithObjects:sort, nil];
[request setSortDescriptors:srotDesc];
NSError *error;
NSMutableArray *results = [[context executeFetchRequest:request error:&error] mutableCopy];
if (results == nil) {
//error handle here
}
[self setArr:results];
NSLog(#"there is category array");
[self.tableView reloadData];
[self.arr count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Category *category = [self.arr objectAtIndex:indexPath.row];
// Configure the cell...
cell.textLabel.text = [category name];
cell.detailTextLabel.text = [category descript];
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
and where i am performing the delete action.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObject *recordtoDelete = [self.arr objectAtIndex:indexPath.row];
[_managedObjectContext deleteObject:recordtoDelete];
[self.tableView beginUpdates];
// Category *deleteRecord = [self.arr objectAtIndex:indexPath.row];
// [self.managedObjectContext deleteObject:deleteRecord];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
//handle error here
}
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
OKAY So when i run this code...
it gives this type of error...
Terminating app due to uncaught exception `NSInternalInconsistencyException`,reason:
'Invalid update: invalid number of rows in section 0.
The number of rows contained in an existing section after the update (17) must be
equal to the number of rows contained in that section before the update (17),
plus or minus the number of rows inserted or deleted from that section
(0 inserted,1 deleted) and plus or minus the number of rows moved into or out of
that section (0 moved in, 0 moved out).
'
It's better to use NSFetchResultController than array, you can easily update tableView with NSFetchResultControllerDelgate. You only need 2 in a UItableViewController like this:
#property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
First Rewrite getter for NSFetchedResultController
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Category" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"is_active == 1"];
[fetchRequest setPredicate:predicate];
[fetchRequest setFetchBatchSize:25];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
NSArray *sortDescriptors = #[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:#"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
Rewrite datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Category *category = (Category *)[self.fetchedResultsController objectAtIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [category name];
cell.detailTextLabel.text = [category descript];
}
Delete
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:#[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:#[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
I try to get an Master-Detail-View with CollectionView instead of TableView up and running. Basicly I followed the examples of http://goo.gl/e07Bs and http://goo.gl/tGLVY to get it working. As with Xcide 4.6 the boilerplate changed a little bit, but I tried my best.
When I run a smoke-test the app crashes with the Message:
1 -fetchedResultsController value self.mOContext 0x00000000
2013-02-12 20:33:52.895 foobar[1461:c07] *** Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'+entityForName: nil is not a legal NSManagedObjectContext
parameter searching for entity name 'PhotoModel''
The error seems to be in this line:
NSEntityDescription *entity = [NSEntityDescription entityForName:#"PhotoModel" inManagedObjectContext:self.managedObjectContext];
I created a breakpoint before this line and checked the content of managedObjectContext, as seen above in the log it is empty (first line, self.mOContext).
How do I set the managedObjectContext, or where is my error that this is not working...?
Edit: As I needed a new scene before the master-detail-scenes, i had to change the AppDelegate.m to meet the new requirements. I'd comment the intial boilerplate of didFinishLaunchingWithOptions to get the initial scene working:
#import "AppDelegate.h"
#import "MasterViewController.h"
#import "LoginViewController.h"
#implementation AppDelegate
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
/*
// Old Master-Detail-View-Controller, commented to get the initial new scene to work
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
*/
return YES;
}
Heres my MasterViewController.h
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#interface MasterViewController : UICollectionViewController <NSFetchedResultsControllerDelegate>
#property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#end
and MasterViewController.m
#import "MasterViewController.h"
#import "CollectionViewCell.h"
#import "DetailViewController.h"
static NSString *cellid = #"cellid";
#interface MasterViewController ()
- (void)configureCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
#end
#implementation MasterViewController
{
NSMutableArray *_objectChanges;
NSMutableArray *_sectionChanges;
}
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// not sure if needed
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
[newManagedObject setValue:[NSDate date] forKey:#"timeStamp"];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
// 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();
}
}
#pragma mark - Collection View
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
return [sectionInfo numberOfObjects];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellid forIndexPath:indexPath];
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
#warning Unimplementd Cell Configuration
return cell;
}
#pragma mark - Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] lastObject];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[[segue destinationViewController] setDetailItem:object];
}
}
#pragma mark - Fetched results controller
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"PhotoModel" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"timeStamp" ascending:NO];
NSArray *sortDescriptors = #[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:#"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// 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();
}
return _fetchedResultsController;
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
NSMutableDictionary *change = [NSMutableDictionary new];
switch(type) {
case NSFetchedResultsChangeInsert:
change[#(type)] = #(sectionIndex);
break;
case NSFetchedResultsChangeDelete:
change[#(type)] = #(sectionIndex);
break;
}
[_sectionChanges addObject:change];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UICollectionView *collectionView = self.collectionView;
NSMutableDictionary *change = [NSMutableDictionary new];
switch(type) {
case NSFetchedResultsChangeInsert:
change[#(type)] = newIndexPath;
break;
case NSFetchedResultsChangeDelete:
change[#(type)] = newIndexPath;
break;
case NSFetchedResultsChangeUpdate:
change[#(type)] = newIndexPath;
break;
case NSFetchedResultsChangeMove:
change[#(type)] = #[indexPath, newIndexPath];
break;
}
[_objectChanges addObject:change];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
if ([_sectionChanges count] > 0)
{
[self.collectionView performBatchUpdates:^{
for (NSDictionary *change in _sectionChanges)
{
[change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) {
NSFetchedResultsChangeType type = [key unsignedIntegerValue];
switch (type)
{
case NSFetchedResultsChangeInsert:
[self.collectionView insertSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];
break;
case NSFetchedResultsChangeDelete:
[self.collectionView deleteSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];
break;
case NSFetchedResultsChangeUpdate:
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];
break;
}
}];
}
} completion:nil];
}
if ([_objectChanges count] > 0 && [_sectionChanges count] == 0)
{
[self.collectionView performBatchUpdates:^{
for (NSDictionary *change in _objectChanges)
{
[change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) {
NSFetchedResultsChangeType type = [key unsignedIntegerValue];
switch (type)
{
case NSFetchedResultsChangeInsert:
[self.collectionView insertItemsAtIndexPaths:#[obj]];
break;
case NSFetchedResultsChangeDelete:
[self.collectionView deleteItemsAtIndexPaths:#[obj]];
break;
case NSFetchedResultsChangeUpdate:
[self.collectionView reloadItemsAtIndexPaths:#[obj]];
break;
case NSFetchedResultsChangeMove:
[self.collectionView moveItemAtIndexPath:obj[0] toIndexPath:obj[1]];
break;
}
}];
}
} completion:nil];
}
[_sectionChanges removeAllObjects];
[_objectChanges removeAllObjects];
}
- (void)configureCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
//cell.textLabel.text = [[object valueForKey:#"timeStamp"] description];
#warning textlabel has to be created in storyboard
}
#end
The managedObjectContext is usually created in the boilerplate code by XCode. When you create the project, make sure to enable Core Data. During the project creation wizard, there should be a checkbox to "Use Core Data". This will insert the correct boilerplate code to create the managed object context and the persistantstorecoordinator, both of which you will need whenever working with Core Data.
EDIT:
In the old AppDelegate code that you commented out, they set the managedObjectContext variable in the master view controller to self.managedObjectContext here:
controller.managedObjectContext = self.managedObjectContext;
Did you make sure to do the same thing for your custom controller?
Please help me to fix this issue (Im getting crazy now!), My project is saving the data properly to the sqlite db in coredata, as checked in a SQLite grapical manager,but I dont seem to be able to show it in a table view, I need my table inside my viewController (small table not whole screen),
If you feel generous please check my little sample project
Here Thankyou!
here the code of the *viewcontroller.h
(Please note that I included a IBoutlet for the table view as tableView is not an instance in UIViewController!) was this done properly?,
also, I get 7 warnings where i call the tableView> Local declaration of 'tableView' hides instance variable
#import <UIKit/UIKit.h>
#interface CoreDataEnsaViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate> //va controller delegate??
{
UITextField *name;
UITextField *address;
UITextField *phone;
UILabel *status;
//la table
//NSMutableArray *array;
//la tabla??
UITableView *tableView;
NSFetchedResultsController *_fetchedResultsController;
NSManagedObjectContext *_context;
}
#property (nonatomic, retain) IBOutlet UITextField *name;
#property (nonatomic, retain) IBOutlet UITextField *address;
#property (nonatomic, retain) IBOutlet UITextField *phone;
#property (nonatomic, retain) IBOutlet UILabel *status;
#property (nonatomic, retain) IBOutlet UITableView *tableView;
#property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
#property (nonatomic, retain) NSManagedObjectContext *context;
- (IBAction) saveData;
- (IBAction) findContact;
- (IBAction) showbtn:(id) sender;
#end
and here the *viewController.m
#import "CoreDataEnsaViewController.h"
#import "CoreDataEnsaAppDelegate.h"
#import "ShowViewController.h"
#import "Contacts.h"
#implementation CoreDataEnsaViewController
ShowViewController *showView;
#synthesize name, address, phone, status, tableView;
#synthesize context = _context;
#synthesize fetchedResultsController = _fetchedResultsController;
-(IBAction) showbtn:(id) sender {
showView = [[ShowViewController alloc] initWithNibName:#"ShowViewController" bundle:nil];
//anima
[UIView beginAnimations:#"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
forView:self.view cache:YES];
[self.view addSubview:showView.view];
[UIView commitAnimations];
}
- (void) saveData
{
CoreDataEnsaAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:#"Contacts" inManagedObjectContext:context];
[newContact setValue:name.text forKey:#"name"];
[newContact setValue:address.text forKey:#"address"];
[newContact setValue:phone.text forKey:#"phone"];
name.text = #"";
address.text = #"";
phone.text = #"";
NSError *error;
[context save:&error];
status.text = #"Contact saved";
}
- (void) findContact
{
CoreDataEnsaAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:#"Contacts" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"(name = %#)", name.text];
[request setPredicate:pred];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
if ([objects count] == 0) {
status.text = #"No matches";
} else {
matches = [objects objectAtIndex:0];
address.text = [matches valueForKey:#"address"];
phone.text = [matches valueForKey:#"phone"];
status.text = [NSString stringWithFormat:#"%d matches found", [objects count]];
}
[request release];
}
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Contacts" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
//NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:#"details.closeDate" ascending:NO];
//[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:#"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
self.name = nil;
self.address = nil;
self.phone = nil;
self.status = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
//tabla
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
//return [array count];
//return [[fetchedResultsController sections] count];
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Contacts *info = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = info.name;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#, %#", info.address, info.phone];
}
//---insert individual row into the table view---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
//---try to get a reusable cell---
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//---create new cell if no reusable cell is available---
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//tabla end
- (void)dealloc {
[name release];
[address release];
[phone release];
[status release];
self.fetchedResultsController = nil;
self.context = nil;
[super dealloc];
}
#pragma mark NSFetchedResultsControllerDelegate methods
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
[tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
[self.tableView endUpdates];}
#end
The connections in the xib are>
table view
outlet:
data source-----------files owner;
delegate--------------files owner;
referencing outlets
tableView-------------files owner;
thank you so much!!!!
As for the "NSInternalConsistencyException" check the managed object model against your code and make sure you're calling it exactly right. Failing that, have you done a clean, restarted XCode, restarted your computer, deleted app from simulator, the usual stuff? If you've made changes to the file or swapped it out, the old stuff will sometimes "lurk" and cause these issues. Failing that, sometimes when I've had a confounding core data problem, rebuilding the model file from scratch and auto-creating the subclasses over will fix it...don't know why.
I know this wasn't your question, but it's widely discouraged to simply call the app delegate for the managed object context--though it does work and you'll see it in some code examples, it's not best practice. It introduces an unnecessary dependency in your project which will make your code harder to maintain. The app delegate should only be used to handle system-level events as it was designed, not "abused" for other purposes. The better way is to create a managed object context property for each controller that needs it, and pass the reference when that controller is created. Your App Delegate will pass it to the root view controller in didFinishLaunchingWithOptions (simply by setting the receiving controller's property), like this:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions...
{
UINavigationController *navController = self.window.rootViewController
MyViewController *controller = navController.viewControllers[0];
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
When that root view controller needs to pass the MOC to another controller, it happens like this:
-(void)prepareForSegue...
{
MyOtherController *otherController = [segue destinationViewController];
controller.managedObjectContext = self.managedObjectContext;
}
And so on and so forth, for each controller that needs it. That way you aren't introducing unnecessary dependencies that make your code less modular and reusable.
Hope this helped.
Try to add this code to perform a fetch of data in your viewDidLoad method:NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(#"Unsolved error: %#, %#", error, [error userInfo]);
abort();
}