//YJYAppDelegate.m

#import "YJYAppDelegate.h"

#import "YJYMasterViewController.h"

@implementation YJYAppDelegate

//@synthesize managedObjectContext = _managedObjectContext;的含义就是属性managedObjectContext的存取方法是做用于_managedObjectContext这个变量的。

//managedObjectContext是属性,而_managedObjectContext才是变量,我们最终操作的变量都是managedObjectContext。@synthesize managedObjectContext = _managedObjectContext;

@synthesize managedObjectModel = _managedObjectModel;

@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

//当由于其它方法打开应用程序(如URL指定或者连接),通知委托启动完毕

- (BOOL)application:(UIApplication *)appl

@implementation YJYAppDelegate

//@synthesize managedObjectContext = _managedObjectContext;的含义就是属性managedObjectContext的存取方法是做用于_managedObjectContext这个变量的。

//managedObjectContext是属性,而_managedObjectContext才是变量,我们最终操作的变量都是managedObjectContext。

@synthesize managedObjectContext = _managedObjectContext;

@synthesize managedObjectModel = _managedObjectModel;

@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

//当由于其它方法打开应用程序(如URL指定或者连接),通知委托启动完毕

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

NSLog(@"didFinishLaunchingWithOptions begin running()...");

// Override point for customization after application launch.

UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;

UINavigationController *navigationController = [splitViewController.viewControllers lastObject];

splitViewController.delegate = (id)navigationController.topViewController;

UINavigationController *masterNavigationController = splitViewController.viewControllers[0];

YJYMasterViewController *controller = (YJYMasterViewController *)masterNavigationController.topViewController;

controller.managedObjectContication didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

NSLog(@"didFinishLaunchingWithOptions begin running()...");

// Override point for customization after application launch.

UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;

UINavigationController *navigationController = [splitViewController.viewControllers lastObject];

splitViewController.delegate = (id)navigationController.topViewController;

UINavigationController *masterNavigationController = splitViewController.viewControllers[0];

YJYMasterViewController *controller = (YJYMasterViewController *)masterNavigationController.topViewController;

controller.managedObjectContext = self.managedObjectContext;

NSLog(@"didFinishLaunchingWithOptions() over...");

returnYES;

}

- (void)applicationWillResignActive:(UIApplication *)application

{// 通知委托应用程序将进入非活动状态,在此期间,应用程序不接收消息或事件,比如来电话了

NSLog(@"applicationWillResignActive() begin...over该方法没内容");

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application

{//当程序被推送到后台的时候调用。所以要设置后台继续运行,则在这个函数里面设置即可

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application

{//当程序从后台将要重新回到前台时候调用,这个刚好跟上面的那个方法相反。

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application

{//通知委托应用程序进入活动状态,请恢复数据

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

//这个方法定义的是当应用程序退到后台时将执行的方法,按下home键执行(通知中心来调度)

//实现此方法的目的是将托管对象上下文存储到数据存储区,防止程序退出时有未保存的数据

- (void)applicationWillTerminate:(UIApplication *)application

{//当程序将要退出是被调用,通常是用来保存数据和一些退出前的清理工作。这个需要要设置

//applicationWillTerminate: 在程序结束前,Core Data会保存任何对其的更改

// Saves changes in the application's managed object context before the application terminates.

[selfsaveContext];

}

//相当与持久化方法

- (void)saveContext

{

NSError *error = nil;

NSManagedObjectContext *managedObjectContext = self.managedObjectContext;

if (managedObjectContext != nil) {

if ([managedObjectContext hasChanges] && ![managedObjectContext 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 - Core Data stack

/*

先建立一个managedObjectModel,

然后根据 managedObjectModel 建立 persistentStoreCoordinator

然后根据 persistentStoreCoordinator 建立 managedObjectContext

然后是把managedObjectContext传给真正要使用数据的 viewcontroller

*/

// Returns the managed object context for the application.

// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.

- (NSManagedObjectContext *)managedObjectContext

{//返回managedObjectContext对象

//自定义的managedObjectContext的getter, 它其实是真正在使用的时候的被操作对象

NSLog(@"managedObjectContext() begin...");

if (_managedObjectContext != nil) {//_managedObjectContext存在,直接返回

return_managedObjectContext;

}

//_managedObjectContext不存在,创建后返回

NSPersistentStoreCoordinator *coordinator = [selfpersistentStoreCoordinator];

if (coordinator != nil) {

_managedObjectContext = [[NSManagedObjectContextalloc] init];//创建一个_managedObjectContext对象

[_managedObjectContextsetPersistentStoreCoordinator:coordinator];

//这里可以看到,“内容管理器”和“数据一致性存储器”的关系,

//managedObjectContext需要得到这个“数据一致性存储器”

}

NSLog(@"managedObjectContext() over...");

return_managedObjectContext;

}

// Returns the managed object model for the application.

// If the model doesn't already exist, it is created from the application's model.

- (NSManagedObjectModel *)managedObjectModel

{//自定义的CoreData数据模板的getter,数据模板其实就是一个描述实体与实体的关系

//,类似于关系型数据库的关系描述文件

NSLog(@"managedObjectModel() begin....");

if (_managedObjectModel != nil) {

return_managedObjectModel;

}

NSURL *modelURL = [[NSBundlemainBundle] URLForResource:@"YJY"withExtension:@"momd"];//这里的URLForResource:@"YJY" 的url名字(YJY)要和你建立datamodel时候取的名字是一样的,至于怎么建datamodel很多教程讲的很清楚,扩展名是momd

_managedObjectModel = [[NSManagedObjectModelalloc] initWithContentsOfURL:modelURL];

NSLog(@"managedObjectModel() over...");

return_managedObjectModel;

}

// Returns the persistent store coordinator for the application.

// If the coordinator doesn't already exist, it is created and the application's store added to it.

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator

{//自定义“数据一致性存储器” persistentStoreCoordinator的getter

NSLog(@"persistentStoreCoordinator() begin...");

if (_persistentStoreCoordinator != nil) {

return_persistentStoreCoordinator;

}

//定义一个本地地址到NSURL,用来存储那个SQLite文件

//这个地方的lich.sqlite名字没有限制,就是一个数据库文件的名字

NSURL *storeURL = [[selfapplicationDocumentsDirectory] URLByAppendingPathComponent:@"YJY.sqlite"];

NSError *error = nil;

_persistentStoreCoordinator = [[NSPersistentStoreCoordinatoralloc] initWithManagedObjectModel:[selfmanagedObjectModel]];

//从这里可以看出,其实persistentStoreCoordinator需要的不过是一个

//存储数据的位置,它是负责管理CoreData如何储存数据的

if (![_persistentStoreCoordinatoraddPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:storeURL options:nilerror:&error]) {// Handle the 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.

Typical reasons for an error here include:

* The persistent store is not accessible;

* The schema for the persistent store is incompatible with current managed object model.

Check the error message to determine what the actual problem was.

If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

If you encounter schema incompatibility errors during development, you can reduce their frequency by:

* Simply deleting the existing store:

[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

* Performing automatic lightweight migration by passing the following dictionary as the options parameter:

@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

*/

NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

abort();

}

NSLog(@"persistentStoreCoordinator() over...");

return_persistentStoreCoordinator;

}

#pragma mark - Application's Documents directory

// Returns the URL to the application's Documents directory.

- (NSURL *)applicationDocumentsDirectory

{

//Documents目录路径,应用程序沙箱下的Documents目录路径

//返回该程序的档案目录,用来简单使用

NSLog(@"applicationDocumentsDirectory() begin...over");

return [[[NSFileManagerdefaultManager] URLsForDirectory:NSDocumentDirectoryinDomains:NSUserDomainMask] lastObject];

}

@end

Note_Master-Detail Application(iOS template)_02_YJYAppDelegate.m的更多相关文章

  1. Note_Master-Detail Application(iOS template)_05_ YJYMasterViewController.m

    //  YJYMasterViewController.m #import "YJYMasterViewController.h" #import "YJYDetailV ...

  2. Note_Master-Detail Application(iOS template)_07_ YJYDetailViewController.m

    //  YJYDetailViewController.m #import "YJYDetailViewController.h" @interfaceYJYDetailViewC ...

  3. Note_Master-Detail Application(iOS template)_03_main.m

    // main.m #import <UIKit/UIKit.h>//UIKitk框架提供一系列的Class(类)来建立和管理iPhone OS应用程序的用户界面( UI )接口.应用程序 ...

  4. Note_Master-Detail Application(iOS template)_06_ YJYDetailViewController.h

    //  YJYDetailViewController.h #import <UIKit/UIKit.h> @interface YJYDetailViewController : UIV ...

  5. Note_Master-Detail Application(iOS template)_04_ YJYMasterViewController.h

    //YJYMasterViewController.h #import <UIKit/UIKit.h> @classYJYDetailViewController; #import < ...

  6. Note_Master-Detail Application(iOS template)_01_YJYAppDelegate.h

    //YJYAppDelegate.h #import <UIKit/UIKit.h> @interface YJYAppDelegate : UIResponder <UIAppli ...

  7. Differences Between Xcode Project Templates for iOS Apps

    Differences Between Xcode Project Templates for iOS Apps When you create a new iOS app project in Xc ...

  8. iOS开发实践:一个类微博客户端从启动到与用户交互的过程

    本文基于数据字典和数据流图两种工具讲述一个完整微博客户端的实现.数据字典和数据流图都可以用来表达线程的执行流程,同时定义了需要的类,是进一步设计类的基础. 数据字典实际上是一张表,表的第一个字段是程序 ...

  9. 如何在macox下面配置集成ios和android游戏教程

    教程截图: 1.准备工作,配置开发环境: 开发环境:mac ox 10.7.3  +   xcode4.2  + ndk r7 + eclipse helios 部署环境:中兴v880  root过了 ...

随机推荐

  1. 2D客户端+微端技术总结

    本人于2013年9月23号加入一个页游项目组, 并作为项目组的客户端小组的主程, 带领一个4个人(峰值)的前端小组, 进行微端的开发.微端项目于2014年8月底大体完成, 历时11个月.9月份之后微端 ...

  2. 关于智能指针boost::shared_ptr

    boost库中的智能指针shared_ptr, 功能强大, 且开销小,故受到广大coder的欢迎. 但在实际的使用过程中,笔者也发现了一些不足. 1.定制的删除器 shared_ptr除了可以使用默认 ...

  3. canvas实现绘画

    html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  4. robotframework笔记21

    创建输出 当执行测试,创建多个输出文件和所有的 都以某种方式相关测试结果. 本节讨论什么 输出创建.如何配置创建,以及如何 调整他们的内容. 不同的输出文件 本节解释了不同可以创建和输出文件 如何配置 ...

  5. Unity Invoke 方法

    Invoke() 方法是 Unity3D 的一种委托机制 如: Invoke("a", 5);   它的意思是:5 秒之后调用 a() 方法: 使用 Invoke() 方法需要注意 ...

  6. md5算法原理一窥(其一)

    首先,需要了解的事,md5并不是传说中的加密算法,只是一种散列算法.其加密的算法并不是我们说所的那样固定不变,只是一种映射的关系. 所以解密MD5没有现成的算法,只能用穷举法,把可能出现的明文,用MD ...

  7. Echart多图联动

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  8. SQL语句创建数据库,SQL语句删除数据库,SQL语句创建表,SQL语句删除表,SQL语句添加约束,SQL语句删除约束

    创建数据库: CREATE DATABASE Test --要创建的数据库名称 ON PRIMARY ( --数据库文件的具体描述 NAME='Test_data', --主数据文件的逻辑名称 FIL ...

  9. c# form的设定

    1. 窗体的显示位置startPosition CenterScreen 窗体在当前居中 CenterParent 窗体在其父窗体居中 WindowDefaultBounds 窗体定期在windows ...

  10. 一步一步配置NLB

    废话不说,配置NLB需要准备以下环境: 1. 至少两个服务器,我的是windows server 2008 R2; 我的两个服务器名分别为NLB3和NLB2,其中NLB3是主,为什么呢?后面会谈到,在 ...