我正在使用核心数据数据库。到目前为止,我的工作是从 Web 服务获取我的所有数据并将其放入核心数据数据库中。为此,我正在做以下事情。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!self.genkDatabase) { // we'll create a default database if none is set
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Default appGenk Database"];
self.genkDatabase = [[UIManagedDocument alloc] initWithFileURL:url]; // setter will create this for us on disk
}
}
- (void)fetchGenkDataIntoDocument:(UIManagedDocument *)document
{
NSLog(@"Fetch data");
dispatch_queue_t fetchQ = dispatch_queue_create("Genk fetcher", NULL);
dispatch_async(fetchQ, ^{
NSArray *news = [GenkData getNews];
NSArray *players = [GenkData getPlayers];
NSArray *staff = [GenkData getStaff];
NSArray *trainingen = [GenkData getTrainingen];
[document.managedObjectContext performBlock:^{ // perform in the NSMOC's safe thread (main thread)
for (NSDictionary *genkInfo in news) {
[News newsWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext];
// table will automatically update due to NSFetchedResultsController's observing of the NSMOC
}
for (NSDictionary *genkInfo in staff) {
[Staff staffWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext];
// table will automatically update due to NSFetchedResultsController's observing of the NSMOC
}
for (NSDictionary *genkInfo in players) {
[Team teamWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext];
// table will automatically update due to NSFetchedResultsController's observing of the NSMOC
}
for (NSDictionary *genkInfo in trainingen ) {
[Training trainingWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext];
}
[document saveToURL:document.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:NULL];
// note that we don't do anything in the completion handler this time
}];
});
}
- (void)setGenkDatabase:(UIManagedDocument *)genkDatabase
{
if (_genkDatabase != genkDatabase) {
_genkDatabase = genkDatabase;
[self useDocument];
}
}
- (void)useDocument
{
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.genkDatabase.fileURL path]]) {
// does not exist on disk, so create it
[self.genkDatabase saveToURL:self.genkDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
[self setupFetchedResultsController];
[self fetchGenkDataIntoDocument:self.genkDatabase];
}];
} else if (self.genkDatabase.documentState == UIDocumentStateClosed) {
// exists on disk, but we need to open it
[self.genkDatabase openWithCompletionHandler:^(BOOL success) {
[self setupFetchedResultsController];
}];
} else if (self.genkDatabase.documentState == UIDocumentStateNormal) {
// already open and ready to use
[self setupFetchedResultsController];
}
}
- (void)setupFetchedResultsController // attaches an NSFetchRequest to this UITableViewController
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"News"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.genkDatabase.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
}
我在打开的第一个 viewController 上执行此操作。我的问题是知道如何在不每次都在视图控制器中实现这些方法的情况下访问核心数据数据库?因为当我想执行一个获取请求时,我需要一个托管对象上下文。现在我只能通过以下方式获得它
self.genkDatabase.managedObjectContext
请帮助 非常感谢提前