views:

26

answers:

2

I am trying to pass a context from my app delegate to my rootview, then to my secondary view. The secondary view is where all the data is really loaded, the rootview is just a quick synopsis of a few key points. When I instantiate the context in appDelegate i log to check if it is not nil:

NSManagedObjectContext *context = [self managedObjectContext];
 if (!context) {
  //Handle error
 }

 if (context == nil) {
  NSLog(@"Context is nil in appdelegate");
 } 
 else {
  NSLog(@"Context is set in appdelegate"); 
 }

then set the rootViewController's context as

rootViewController.context = context;

followed by the same check, both show not nil. Then I push the view. Then I check to see if context is set during viewDidLoad and it is nil. The rootViewController:

#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate> { 
 NSFetchedResultsController *fetchedResultsController;
 NSManagedObjectContext *context;
 NSMutableArray *rootViewContentArray;
}
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *context;
@end


#import "RootViewController.h"
#import "DetailViewController.h"
#import "SwapViewController.h"
#import "Swap.h"

@implementation RootViewController
@synthesize fetchedResultsController, context;

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

 //initialize array for rootView

 rootViewContentArray = [[NSMutableArray alloc] init];

 NSArray .....set array values....

        //check context
 if (context == nil){
  NSLog(@"RootViewcontroller context is nil during viewdidload");
 } 
 if (!(context == nil)){
  NSLog(@"RootViewcontroller context is set during viewdidload");
 }

 //fill array
.........
}

Am I missing something small, or do I have a fundamental misunderstanding here? Also, it seems to me that if I set a variable on a view I create it should still be instantiated when the view loads. I am going through the example apps and using the books by Apress on iPhone developing and I try to create simple apps with what I have learned so far. The Core Data apps seem either way too simple or way above my head and I'm trying to make the jump. Any help would be greatly appreciated! Thanks.

A: 
if(managedObjectContext == nil)
 {
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  managedObjectContext = [appDelegate managedObjectContext];
 }
Asad Khan
Thanks for the answer, I have seen this line in the other posts I have looked at, but I don't really understand it. Where should it go? in the AppDelegate viewdidload? or the root controller? The way I am reading it is that a new instance of the appdelegate is created, why is this needed? I tried placing it in the appdidfinishloading and got an error stating "-[Swaps3AppDelegate context]: unrecognized selector sent to instance 0x612d630".
JC43
Also I don't understand why the context in the original code is valid as I pass it to the rootview, right up until I push the rootview. I know that these are probably very fundamental ideas, but I am still trying to wrap my head around it using as many sources as possible. Thank you for any clarification.
JC43
A: 

I found what I was doing wrong, I did not set my rootViewController as my navigationController in my appDelegate. My question now is why would my program run normally until I try to access Core Data objects in the rootview? why would the rootView I created get pushed by calling

[window addSubview:[navigationController view]];

Previously the navigation controller was not set at all in my app:

RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStyleGrouped];
rootViewController.managedObjectContext = [self managedObjectContext];



if (rootViewController.managedObjectContext == nil) {
    NSLog(@"RootViewcontroller context is nil before view is pushed");
}
if (!(rootViewController.managedObjectContext == nil)){
    NSLog(@"RootViewcontroller context is SET before view is pushed");
}

// Add the navigation controller's view to the window and display.
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];

The code now reads

RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStyleGrouped];
rootViewController.managedObjectContext = [self managedObjectContext];

self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];


if (rootViewController.managedObjectContext == nil) {
    NSLog(@"RootViewcontroller context is nil before view is pushed");
}
if (!(rootViewController.managedObjectContext == nil)){
    NSLog(@"RootViewcontroller context is SET before view is pushed");
}
[rootViewController release];
// Add the navigation controller's view to the window and display.
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];

and everything works perfectly!?

JC43