Search code examples
iphoneuitableviewcore-datauitabbarnsmanagedobjectcontext

iPhone UITableView Weirdness


I'm not even sure what to call this problem. Much less how to search for a solution.

I'm using Xcode 4. I'm using CoreData. I have tab bar app. 4 different tabs. This is an issue from the simulator.

The root table view for two of the tab is populated by an array of controllers.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];

SecondViewController *nextController = [self.medControllersArray objectAtIndex:row];
[self.navigationController pushViewController:nextController
                                     animated:YES];
}

When drilling down to one of the controllers, there's another table table view as you can see.

When tapping the Add button in the next controller's table view and adding a name or whatever to populate the table view, if you go to another next controller's table view it is populated with the same info and the other table view.

Since I was getting the infamous +entityForName error

I add this in viewDidLoad

 if (managedObjectContext == nil) 
{ 
    managedObjectContext = [(MIT2AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    NSLog(@"After managedObjectContext_: %@",  managedObjectContext);
}

Add this in the app's delegate method application:didFinishLaunchingWithOptions

ViewController *viewController = [[ViewController alloc] init];
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
    NSLog(@"\nCould not create *context for self");
}

viewController.managedObjectContext = context;

I did this for each next view controller I had and the root views for the two tabs whose root view isn't populated by and array of controllers.

I also get this warning in the console when a next view is pushed:

After managedObjectContext_: <NSManagedObjectContext: 0x5934e10>

I hope it all makes sense. And that someone can point me in the right direction.

Thanks in advance.


Solution

  • Thank GOD!!! It's worked out. I made (I'm sure of it) a rookie Core Data mistake in my data model.

    I was getting duplicates in the simulator (as well as the phone) because of this mistake:

    In the data model I have an entity for medications and all the attributes, blah, blah, blah. I created a class and decided to use just that class to share between 3 table views, current meds, past meds, and allergies. In doing so naturally there was no differentiation. I went back, create a new model, merged yada, yada, and added 3 new entities for the current, past, and allergies. Then set there parent entity to the main med entity and abstracted the parent. When I went through again and reworked the code to the new entities. Everything worked! No more duplications. Well, really, no more triplication.

    For the warning I was getting (not error, the app still build, didn't crash and you could still work through it) was do to the managed object context. To silence the infamous +entityForName error I tried something that I either saw in a book or from Apple. I imported the AppDelegate to each of my tables and in fetchedResultsController did this:

    - (NSFetchedResultsController *)fetchedResultsController {
    MIT2AppDelegate *mit = [[MIT2AppDelegate alloc] init];
    
    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }
        //if (fetchedResultsController == nil) {
        // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    
    
        //  the entity name
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Allergies" inManagedObjectContext:mit.managedObjectContext];
    [fetchRequest setEntity:entity];
    

    While reworking the code I tried what tomasBULL recommended: How can I solve NSInternalInconsistencyException', reason: '+entityForName: fail report

    That's where the error was coming from. So I went back to what I had before and now, no more error.

    I do this in my AppDelegate managedObjectModel

    - (NSManagedObjectModel *)managedObjectModel{
        if (managedObjectModel != nil){
            return managedObjectModel;
        }
    
        NSString *path = [[NSBundle mainBundle]  pathForResource:@"MIT2ver 3" ofType:@"mom" inDirectory:@"MIT2ver2.momd"];
    
            NSURL *momURL = [NSURL fileURLWithPath:path];
            managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
    
        return managedObjectModel;
    }
    

    Because before I had made another rookie mistake of when I added a model I took out the space. Thanks to udibr answer: Implementation of “Automatic Lightweight Migration” for Core Data (iPhone)

    Thanks to everyone who answered. GOD worked it out!