forked from jsonmodel/jsonmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreDataViewController.m
More file actions
192 lines (158 loc) · 6.12 KB
/
CoreDataViewController.m
File metadata and controls
192 lines (158 loc) · 6.12 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// CoreDataViewController.m
// JSONModelDemo_iOS
//
// Created by Marin Todorov on 22/1/14.
// Copyright (c) 2014 Underplot ltd. All rights reserved.
//
#import "CoreDataViewController.h"
#import "JSONModelLib.h"
#import "ReposModel.h"
#import "GitHubRepoModel.h"
#import <CoreData/CoreData.h>
#import "GitHubRepoEntity.h"
@interface CoreDataViewController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@property (strong, nonatomic) NSMutableArray* entities;
@property (strong, nonatomic) ReposModel* repos;
@property (strong, nonatomic) IBOutlet UITableView* tableView;
@end
@implementation CoreDataViewController
#pragma mark - Core Data
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] & ![managedObjectContext save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil)
{
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil)
{
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"github" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil)
{
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"github.sqlite"];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
#pragma mark - view controller life cycle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.entities = [@[] mutableCopy];
//setup core data
[self managedObjectContext];
//delete all entities
NSFetchRequest* entitiesRequest = [[NSFetchRequest alloc] init];
[entitiesRequest setEntity:[NSEntityDescription entityForName: [[GitHubRepoEntity class] description]
inManagedObjectContext: self.managedObjectContext]];
[entitiesRequest setIncludesPropertyValues:NO];
for (GitHubRepoEntity* repo in [self.managedObjectContext executeFetchRequest:entitiesRequest error:nil]) {
[self.managedObjectContext deleteObject:repo];
}
[self saveContext];
//import the json
NSString* filePath = [[NSBundle bundleForClass:[JSONModel class]].resourcePath stringByAppendingPathComponent:@"github-iphone.json"];
NSString* jsonContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSError* err;
self.repos = [[ReposModel alloc] initWithString: jsonContents error:&err];
//setup UI
self.title = @"CoreData Demo";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Import next repo" style:UIBarButtonItemStylePlain target:self action:@selector(importJSON)];
}
-(void)importJSON
{
//get next repo to import
GitHubRepoModel* model = self.repos.repositories.firstObject;
[self.repos.repositories removeObjectAtIndex:0];
//create an entity for next repo
NSError* error = nil;
GitHubRepoEntity* entity = [GitHubRepoEntity entityWithModel:model
inContext:self.managedObjectContext
error:&error];
if (entity) {
[self.entities addObject: entity];
[self saveContext];
[self.tableView reloadData];
} else {
NSLog(@"Error importing JSON: %@", error);
}
}
#pragma mark - table stuff
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.entities.count;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"%lu entities", (unsigned long)self.entities.count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
GitHubRepoEntity* entity = self.entities[indexPath.row];
cell.textLabel.text = entity.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ (%@) forks:%@", entity.owner, entity.language, entity.forks];
return cell;
}
@end