-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoSQLAppAppDelegate.m
More file actions
228 lines (179 loc) · 7.28 KB
/
Copy pathToDoSQLAppAppDelegate.m
File metadata and controls
228 lines (179 loc) · 7.28 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// ToDoSQLAppAppDelegate.m
// ToDoSQLApp
//
// Created by Natalie Podrazik on 9/11/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "ToDoSQLAppAppDelegate.h"
#import "RootViewController.h"
#import "Todo.h"
#import "TodoVC.h"
#define DATABASE_NAME @"todo3.sqlite"
/* This is private and declared in the .M for a reason --
"We declared it here because it's specific to this object so
it does not need to be declared in the .h file" -- hmmm
*/
@interface ToDoSQLAppAppDelegate (Private)
-(void)createEditableCopyOfDatabaseIfNeeded;
-(void)initializeDatabase;
@end
@implementation ToDoSQLAppAppDelegate
/*
Creates a writeable copy of the bundled default database in the
application Documents directory.
*/
-(void)createEditableCopyOfDatabaseIfNeeded {
// test for existence...
BOOL success;
NSFileManager * fileManager = [NSFileManager defaultManager];
NSError * error;
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * writeableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME];
success = [fileManager fileExistsAtPath:writeableDBPath];
if (success) {
return;
}
/*
The writeable database does not exist, so copy the default
to the appropriate location
*/
NSString * defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DATABASE_NAME];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writeableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writeable database file with message '%@'.\n", [error localizedDescription]);
}
}
/*
Open the database connection and retrieve the minimal information
for all of its objects.
*/
-(void)initializeDatabase {
NSMutableArray * todoArray = [[NSMutableArray alloc] init];
self.todos = todoArray;
[todoArray release];
/* database is stored in app bundle */
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME];
/*
open the database. db was prepared outside this app!
*/
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
// get the primary key for all books.
const char * sql = "SELECT pk FROM todo";
sqlite3_stmt * statement;
/*
preparing a statement compiles the SQL query into a byte-code
program in the SQLite library. The third parameter is either
the length of the SQL string, or -1 to read up to the first
null terminator.
*/
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
// "step" through the results - once for each row
while (sqlite3_step(statement) == SQLITE_ROW) {
// second parameter indicates the colum index onto the result set.
int primaryKey = sqlite3_column_int(statement, 0);
/*
Avoid the alloc-init-autorelease pattern here because
we are in a tight loop and autorelease is slightly more
expensive than release. This design choice has nothing
to do with actual memory management -- at the end of
this block of code, all the book objects allocated here
will be in memory regardless of whether we use auto-
release or release, because they are retained by the
books array.
*/
Todo * td = [[Todo alloc] initWithPrimaryKey:primaryKey database:database];
[todos addObject:td];
[td release];
}
}
/*
"Finalize" the statement - release the resources
associated with the statement.
*/
sqlite3_finalize(statement);
}
else {
/*
Even though the open failed, call close to properly
clean up the references.
*/
sqlite3_close(database);
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
/*
** MORE ERROR HANDLING GOES HERE
*/
}
}
/*
@synthesize window=_window;
@synthesize navigationController=_navigationController;
*/
@synthesize window, navigationController, todos;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/* Here is where we set up the sql. */
[self createEditableCopyOfDatabaseIfNeeded];
[self initializeDatabase];
// configure and show the window.
//[window addSubview:[navigationController view]];
TodoVC * todoVC = [[TodoVC alloc] initialize:self];
[window addSubview:todoVC.view];
[todoVC release];
[window makeKeyAndVisible];
// Override point for customization after application launch.
// Add the navigation controller's view to the window and display.
/*
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
*/
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}
- (void)dealloc
{
/*
[_window release];
[_navigationController release];
*/
[window release];
[navigationController release];
[super dealloc];
}
@end