This repository has been archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_DCTDataCenterManagedObjectContextViewController.m
92 lines (65 loc) · 3.17 KB
/
_DCTDataCenterManagedObjectContextViewController.m
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// DCTDataCenterManagedObjectContextViewController.m
// DataCenter
//
// Created by Daniel Tull on 23.02.2011.
// Copyright 2011 Daniel Tull. All rights reserved.
//
#import "_DCTDataCenterManagedObjectContextViewController.h"
#import "_DCTDataCenterManagedObjectViewController.h"
#import "NSManagedObject+_DCTDataCenterNiceDescription.h"
@implementation _DCTDataCenterManagedObjectContextViewController {
__strong NSArray *_entities;
__strong NSMutableDictionary *_fetchedEntities;
}
- (id)initWithManagedObjectContext:(NSManagedObjectContext *)managedObjectContext {
self = [self initWithStyle:UITableViewStyleGrouped];
if (!self) return nil;
self.title = @"Data Center";
_managedObjectContext = managedObjectContext;
_entities = [[[_managedObjectContext persistentStoreCoordinator] managedObjectModel] entities];
_fetchedEntities = [[NSMutableDictionary alloc] initWithCapacity:[_entities count]];
[_entities enumerateObjectsUsingBlock:^(NSEntityDescription *entity, NSUInteger i, BOOL *stop) {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSArray *fetchResult = [managedObjectContext executeFetchRequest:request error:NULL];
[_fetchedEntities setObject:fetchResult forKey:[entity name]];
}];
return self;
}
#pragma mark - UIViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [_entities count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[_entities objectAtIndex:section] name];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[_fetchedEntities objectForKey:[[_entities objectAtIndex:section] name]] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSEntityDescription *entity = [_entities objectAtIndex:indexPath.section];
NSArray *objects = [_fetchedEntities objectForKey:[entity name]];
NSManagedObject *managedObject = [objects objectAtIndex:indexPath.row];
cell.textLabel.text = [managedObject dctDataCenter_niceDescription];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSEntityDescription *entity = [_entities objectAtIndex:indexPath.section];
NSArray *objects = [_fetchedEntities objectForKey:[entity name]];
NSManagedObject *managedObject = [objects objectAtIndex:indexPath.row];
_DCTDataCenterManagedObjectViewController *vc = [[_DCTDataCenterManagedObjectViewController alloc] initWithManagedObject:managedObject];
[self.navigationController pushViewController:vc animated:YES];
}
@end