在苹果公司的核心数据例如“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();
}
提前感谢您的任何意见!
签入AppDelegate .... – sample 2015-03-31 21:22:28