2015-03-31 22 views
0

在苹果公司的核心数据例如“TaggedLocations”他们有在头文件声明此在Apple的'TaggedLocations'Core Data示例中,一个ivar NSManagedObjectContext获取一个值,但是我找不到在哪里?

@property (nonatomic) NSManagedObjectContext *managedObjectContext; 

然后,在的ViewController主文件,他们从来没有这方面无所不能或初始化或任何分配它。他们只是在提取请求中使用它来检索结果。这是有道理的,因为上下文仅仅是持久存储对象的“暂存器”

但是,我没有看到它们是如何声明哪一个或任何持久存储器在以前的Core Data示例中,我总是看到人们创建AppDelegate的实例并访问它的上下文和存储,这是有道理的,因为在这个例子中,整个核心数据栈在那里。

这是Apple示例的代码片段,我在看什么?

/* 
Fetch existing events. 
Create a fetch request for the Event entity; add a sort descriptor; then execute the fetch. 
*/ 
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"APLEvent"]; 
[request setFetchBatchSize:20]; 

// Order the events by creation date, most recent first. 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO]; 
NSArray *sortDescriptors = @[sortDescriptor]; 
[request setSortDescriptors:sortDescriptors]; 

// Execute the fetch. 

//Not sure how they're excuting a fetch request on self.managedObjectContext? Seems to be a nil unintialized context 
//TO-DO Test value of context 
NSError *error; 
NSArray *fetchResults = [self.managedObjectContext executeFetchRequest:request error:&error]; 
if (fetchResults == nil) { 
    // 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(); 
} 

链接到苹果公司的例子https://developer.apple.com/library/ios/samplecode/TaggedLocations/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008914

提前感谢您的任何意见!

+0

签入AppDelegate .... – sample 2015-03-31 21:22:28

回答

0

每当与核心数据创建的项目,在应用程序委托u能得到这些,

@property(只读,强,非原子)的NSManagedObjectContext * managedObjectContext; @property(只读,强,非原子)NSManagedObjectModel * managedObjectModel; @property(只读,强,非原子)NSPersistentStoreCoordinator * persistentStoreCoordinator;

你也可以获得外部文件,你的数据将存储在哪里。

+0

对,我看到App Delegate中的人,但他们如何通过此视图控制器访问它?是否因为获取请求具有我们正在查找的实体名称,并且由于此项目中只有一个模型,它知道自动查找的位置?仍有点困惑。另外,我没有看到外部文件? – gregyoung14 2015-04-01 14:56:58

+0

找到了!在didFinishLaunchingWithOptions方法中的App Delegate中,他们将ViewControllers managedObjectContext设置为与AppDelegate相同的一个,这就是他们的做法。 'APLEventsTableViewController * controller =(APLEventsTableViewController *)navigationController.topViewController; controller.managedObjectContext = self.managedObjectContext;' – gregyoung14 2015-04-01 15:02:44

相关问题