managedObjectContext is nil - ios

I'm trying to add Core Data to an existing project. I've:
1)added the Core Data framework
2)added the accessors and properties to the AppDelegate
3)created the data model file
Now when I try to call
NSManagedObjectContext *context = [self managedObjectContext];
from a view controller the context is nil and the managedObjectContext never fires.
Here is the AppDelegate:
#import "XXXAppDelegate.h"
#import <CoreData/CoreData.h>
#implementation XXXAppDelegate
#synthesize window=_window;
#synthesize navigationController=_navigationController;
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the navigation controller's view to the window and display.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
// Explicitly write Core Data accessors
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
stringByAppendingPathComponent: #"<Project Name>.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeUrl options:nil error:&error]) {
/*Error for store creation should be handled in here*/
}
return persistentStoreCoordinator;
}
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
#end
EDIT: here is my view controller code
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *cardSet = [NSEntityDescription insertNewObjectForEntityForName:#"CardSet" inManagedObjectContext:context];
[cardSet setValue:#"Set 1" forKey:#"cardSetName"];

Try adding these condition to check if your managedObjectContext is nil or not wherever you want to use it. If its nil copy it from Appdelegate file.
if (managedObjectContext == nil)
{
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(#"After managedObjectContext: %#", managedObjectContext);
}

Assuming your properties are declared... you are synthesizing them to ivars with an underscore in front. That's a good thing. However, the only place you want to access them with the underscore is in the implementation of the getter/setter for the property. Unfortunately, that's not happening in any of these. Change it to...
- (NSManagedObjectContext *) managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return _managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
_managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
stringByAppendingPathComponent: #"<Project Name>.sqlite"]];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
if(![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeUrl options:nil error:&error]) {
/*Error for store creation should be handled in here*/
}
return _persistentStoreCoordinator;
}

If you look at the Master-Detail Application template in Xcode you see they pass the ManagedObjectContext in the AppDelegate like this:
#import "AppDelegate.h"
#import "MasterViewController.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.
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
NSLog(#"navigationController viewControllers: %#",[navigationController viewControllers]);
NSLog(#"navigationController.topViewController: %#",navigationController.topViewController);
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
If you need a TabBarViewController in front of your app the code looks like this:
#import "AppDelegate.h"
#import "MasterViewController.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.
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = (UINavigationController *)[[tabController viewControllers] objectAtIndex:0];
MasterViewController *controller = (MasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
controller.managedObjectContext = self.managedObjectContext;
navigationController = (UINavigationController *)[[tabController viewControllers] objectAtIndex:1];
controller = (MasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
controller.managedObjectContext = self.managedObjectContext;
navigationController = (UINavigationController *)[[tabController viewControllers] objectAtIndex:2];
controller = (MasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
controller.managedObjectContext = self.managedObjectContext;
return YES;
}

I don't know how you got past without compiler errors, but your #synthesizes use underscore'd variables that your getters never access. Do it like this:
- (NSManagedObjectContext *) managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return _managedObjectContext;
}
Take note of _managedObjectContext (with underscore). Do the same fixes with managedObjectModel and persistentStoreCoordinator.

Related

Set managedObjectContext of a UITableViewController with a UINavigationController

I have looked at other questions and answers but I still cannot set the managedObjectContext of a UITableViewController embedded in a UINavigationController. Here is my current code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *navigationController = [sb instantiateViewControllerWithIdentifier:#"Learn"];
MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
However when I run the code, the app crashes and produces a log output of this:
+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name
When I try just logging NSLog(#"%#", self.managedObjectContext); in the MasterViewController the result is `(null)
I created my managedObjectContext like so and it received no errors:
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
N.B. I am trying to set a UITableViewController that is not the initial view controller
Any ideas? Thanks in advance!
If you are using the boilerplate Apple template (from Xcode), your app delegate should have a method like this, creating the managed objects dynamically when you call self.managedObjectContext:
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
Put a breakpoint there and check that you pass the correct context to the view controller. Make sure the VC property is (nonatomic, strong).
If your navigation controller is set as the initial controller in the storyboard, then the problem is that you're instantiating a new instance of that navigation controller with instantiateViewControllerWithIdentifier. You should get the reference to MasterViewController like this instead:
MasterViewController *controller = (MasterViewController *)[(UINavigationController *)self.window.rootViewController topViewController];

SIGABRT when saving to managed object context in core data

This code is supposed to save my data to core data in TestViewController.m file:
//Property
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
//Methods
- (void)saveUser:(NSString *) username withEmail:(NSString *) email withName:(NSString *) name
{
//1
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
//2
self.managedObjectContext = appDelegate.managedObjectContext;
User *coreDataUser = [NSEntityDescription insertNewObjectForEntityForName:#"User" inManagedObjectContext:self.managedObjectContext];
coreDataUser.username = username;
coreDataUser.email = email;
coreDataUser.name = name;
NSError *error;
if (![self.managedObjectContext save:&error])
{
NSLog(#"Whoops, couldn't save: %#", [error localizedDescription]);
}
}
//In my save user method I have stuff that is done in another thread but then this is called:
// Add in any UIKit code here on main queue
dispatch_async(dispatch_get_main_queue(), ^{
if (isUnique == YES)
{
[self saveUser:[emailStr lowercaseString] withEmail:emailStr withName:nameStr];
My code always crashes on:
ser *coreDataUser = [NSEntityDescription insertNewObjectForEntityForName:#"User" inManagedObjectContext:self.managedObjectContext];
It says signal SIGABRT in main.
I am following this tutorial: http://www.codigator.com/tutorials/ios-core-data-tutorial-with-example/
And running in ios 7.
What am I missing?
I'm tracing the appDelegate.m code now but it looks like this:
// 1
- (NSManagedObjectContext *) managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_ managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return _managedObjectContext;
}
//2
- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
_managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return _managedObjectModel;
}
//3
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil)
{
return _persistentStoreCoordinator;
}
NSString *test = [self applicationDocumentsDirectory];
NSString *test2 = test;
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"HappyPeople.sqlite"]];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
i f (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error])
{
/*Error for store creation should be handled in here*/
}
return _persistentStoreCoordinator;
}
- (NSString *)applicationDocumentsDirectory
{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
Seems like the sqllite db file gets created.
The exception I am getting is this:
NSException * name:#"NSInternalInconsistencyException" reason:#"+entityForName: could not locate an entity named 'Users' in this model." 0x0895ee20
I know this question is a little old...
This is the exception I get when I change the schema of the object model and try to use a file with the old schema. The sqllite db on disk may not match your apps schema.
I have been using CoreData with a UIManagedDocument so it is a little different. There you have options to automatically migrate on the open.
Also, early on in the comments it was asked whether the ManagedObjectContext was nil. I believe that this should be checked before you call the insert. Are we sure all of the core data setup is on the main queue? It can take a little time to open. (Again with UIManagedDocument is explicitly on another thread.)

managedObjectContext func in AppDelegate

I strive to understand Core Data implementation.
When I examined Core Data implementations I encountered codes as above
In AppDelegate.m
#synthesize managedObjectContext = __managedObjectContext;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSManagedObjectContext *context = [self managedObjectContext];
..
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil) {
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}
There is a property as
#synthesize managedObjectContext = __managedObjectContext;
and there is a func as
- (NSManagedObjectContext *)managedObjectContext
How it's create a relation between them, how/where it's call managedObjectContext func.
__managedObjectContext is an instance variable.
managedObjectContext is a getter method for __managedObjectContext. It allows you to get __managedObjectContext by, e.g., self.managedObjectContext.
Access of instance variable is usually done through its setter and getter methods.

"window" undeclared (first use in this function) error in iOS tutorial

I am trying the Core Data Tutorial and I have copied the code as given in this Apple's tutorial on Core Data here:
http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Articles/02_RootViewController.html
It asks us to compile and run after implementing the application delegate. When I do so, Xcode 4 compiler gives this error "window undeclared (first use in this function)". I can see that there is window declared as a property and there is a synthesize line in the code like so:
#synthesize window=_window;
so even though there is a window property declared in the program, why am I getting this error?
Here are the .h and .m files:
.h:
#import
#interface LocationsAppDelegate : NSObject <UIApplicationDelegate> {
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) UINavigationController *navigationController;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
#end
Now for the .m file:
#import "LocationsAppDelegate.h"
#import "RootViewController.h"
#implementation LocationsAppDelegate
#synthesize navigationController;
#synthesize window=_window;
#synthesize managedObjectContext=__managedObjectContext;
#synthesize managedObjectModel=__managedObjectModel;
#synthesize persistentStoreCoordinator=__persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
NSManagedObjectContext *context = [self managedObjectContext];
if(!context) {
//handle the error you dummy!!
}
rootViewController.managedObjectContext = context;
UINavigationController *aNavigationController = [[UINavigationController alloc]initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
[rootViewController release];
[aNavigationController release];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application
{
[self saveContext];
}
- (void)dealloc
{
[_window release];
[__managedObjectContext release];
[__managedObjectModel release];
[__persistentStoreCoordinator release];
[super dealloc];
}
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
}
#pragma mark - Core Data stack
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil)
{
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
{
return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"Locations" withExtension:#"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"Locations.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
#end
Try to use
self.window
instead of
window
Just to expand on that answer a bit:
You can see from the #synthesize statement that the property window is backed by the instance variable _window. So you use window to access it via the getter (self.window or [self window]), but if you're trying to access the instance variable directly, it would be _window.

TTNavigator URL values do not match current VC

Good day guys, I would like to inquire if anyone of you may have encountered a problem like mine. I have been working on a project using the TTNavigator of the Three20 framework. Every view is displayed and transitioned as it should be. I have an App menu that has buttons to the application's respective modules. The problem is that, when i click the button to a particular module, and when that view is displayed, the URL property value of the TTNavigator is that of the app's menu view (tt://mainMenu) and not the module's initial View (e.g. "tt://messageBoard" or "tt://profilePage"). i have checked and reviewed the necessary code blocks to which this problem may be linked to but i can't seem to fine the fault at hand.
Here's the definition for my AppDelegate
#import "Three20TestAppDelegate.h"
#import "StartViewController.h"
#import "JumpsiteProfilePage.h"
#import "MenuViewController.h"
#import "BNDefaultStylesheet.h"
#import "MessageBoardViewController.h"
#import "GroupListViewController.h"
#import "DrillDownGroupListView.h"
#import "ProfileListViewController.h"
#import "ProfileDetailsViewController.h"
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#implementation UINavigationBar (UINavigationBarCategory)
-(void)drawRect:(CGRect)rect{
UIImage *image = [UIImage imageNamed:#"NavBar BG.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = UIColorFromRGB(0xFFD900);
}
#end
#implementation UIToolbar (UIToolbarCategory)
-(void)drawRect:(CGRect)rect{
UIImage *image = [UIImage imageNamed:#"NavBar BG.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = UIColorFromRGB(0xFFD900);
[super drawRect:rect];
}
#end
#implementation Three20TestAppDelegate
#synthesize window=_window;
#synthesize managedObjectContext=__managedObjectContext;
#synthesize managedObjectModel=__managedObjectModel;
#synthesize persistentStoreCoordinator=__persistentStoreCoordinator;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
TTNavigator *navigator = [TTNavigator navigator];
navigator.window = _window;
navigator.persistenceMode = TTNavigatorPersistenceModeAll;
navigator.supportsShakeToReload = YES;
[TTStyleSheet setGlobalStyleSheet:[[[BNDefaultStylesheet alloc] init] autorelease]];
TTURLMap *map = navigator.URLMap;
//startView(Log-in View)
[map from:#"tt://startView"
toSharedViewController:[StartViewController class]];
//Application's Menu
[map from:#"tt://mainMenu"
toSharedViewController:[MenuViewController class]];
//User's Profile module
[map from:#"tt://profilePage"
toSharedViewController:[JumpsiteProfilePage class]];
//Message Board module
[map from:#"tt://messageBoard"
toSharedViewController:[MessageBoardViewController class]];
[map from:#"tt://groupList"
toSharedViewController:[GroupListViewController class]];
[map from:#"tt://drillDownListView"
toSharedViewController:[DrillDownGroupListView class]];
//Profile List module
[map from:#"tt://profileList"
toViewController:[ProfileListViewController class]];
[map from:#"tt://profileDetailsList"
toSharedViewController:[ProfileDetailsViewController class]];
[navigator openURLAction:[TTURLAction actionWithURLPath:#"tt://startView"]];
// Override point for customization after application launch
[_window makeKeyAndVisible];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
[self saveContext];
}
- (void)dealloc
{
[_window release];
[__managedObjectContext release];
[__managedObjectModel release];
[__persistentStoreCoordinator release];
[super dealloc];
}
- (void)awakeFromNib
{
}
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
}
#pragma mark - Core Data stack
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil)
{
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
{
return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"Three20Test" withExtension:#"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"Three20Test.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
#end
Mapping ViewController
[map from:kAppRootURLPath toViewController:[HomeThumbnailViewController class]];
Call to ViewController
TTURLAction *urlAction=[[[TTURLAction alloc] initWithURLPath:strTTURL] autorelease];
urlAction.animated=YES;
[[TTNavigator navigator]openURLAction:urlAction];

Resources