IOS7 App Development Essentials(1) Persistent Store
1. Introduction
Store Types and Behaviors
XML, atomic, SQLite, in-memory store.
NSBinaryStoreType — build-in atomic store.
The XML store is not available on iOS.
SQLite Store
Core Data provides a way to control sync behavior in SQLite using 2 independent pragmas.
PRAGMA synchronous FULL 2/ NORMAL 1 / OFF 0
PRAGMA fullfsync 1/0
NSPersistentStoreCoordinator
NSPersistentStore
2. Using Persistent Stores
Creating and Accessing a Store
XML store fragment
NSManagedObjectContext *moc = <context>;
NSPersistentStoreCoordinator *psc = [moc persistentStoreCoordinator];
NSError *error = nil;
NSDictionary *option =
[NSDictionary dictionaryWithObject: [NSNumber numberWithBool:1]
forKey:NSReadOnlyPersistentStoreOption];
NSPersistentStore *roStore =
[psc addPersistentStoreWithType:NSXMLStoreType
configuration:nil URL:url
options:options error:&error];
Get a store from URL
NSPersistentStoreCoordinator *psc = <>;
USURL *myURL = <>;
NSPersistentStore *myStore = [psc persistentStoreForURL:myURL];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setAffectedStores: [NSArray arrayWithObject: myStore ]];
Changing a Store’s Type and Location
NSPersistentStoreCoordinator *psc = [aManagedObjectContext persistentStoreCoordinator];
NSURL *oldURL = <>;
NSURL *newURL = <>;
NSError *error = nil;
NSPersistentStore *xmlStore = [psc persistentStoreForURL:oldURL];
NSPersistentStore *sqLiteStore = [psc migratePersistentStore: xmlStore
otURL:newURL
options:nil
withType:NSSQLiteStoreType
error:&error];
2. Example CoreDataBooks
CoreData.framework
3. Solutions Iphone Sqlite VS. Core Data
SQLite directly
Relational Database System
Protential for cross platform
Many early iPhone database example
Object-c Wrappers FMDB
Core Data
Store in binary or SQLite storage format
Serialize Objects
Higher level
Not a RDBMS
4. Directly Using SQLite
Link the lib
>TARGETS ——> Build Phases —> Link Binary With Libraries ———> libsqlite3.dylib
Importing sqlite3.h and declaring the Database Reference
#import <UIKit/UIKit.h> #import <sqlite3.h>@interface easylocationViewController : UIViewController@property (strong, nonatomic) NSString *databasePath; @property (nonatomic) sqlite3 *contactDB;
@end
Designing the User Interface
buttons: find and save
labels: name, address and phone, status
text: name, address, phone
ctrl + click to link all the input text boxes and one label ’status’ to Interface class, create some outlets.
ctrl + click to link buttons to Interface class, create some actions.
@property (strong, nonatomic) IBOutletUITextField *name; @property (strong, nonatomic) IBOutletUITextField *address; @property (strong, nonatomic) IBOutletUITextField *phone; @property (strong, nonatomic) IBOutletUILabel *status; - (IBAction)saveData:(id)sender;
- (IBAction)findContact:(id)sender;
Creating the Database and Table
- (void)viewDidLoad { [superviewDidLoad]; NSString *docsDir; NSArray *dirPaths; // Get the documents directory dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); docsDir = dirPaths[0]; // Build the path to the database file _databasePath = [[NSStringalloc] initWithString: [docsDir stringByAppendingPathComponent:@"contacts.db"]]; NSFileManager *filemgr = [NSFileManagerdefaultManager]; if ([filemgr fileExistsAtPath: _databasePath ] == NO) { constchar *dbpath = [_databasePathUTF8String]; if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) { char *errMsg; constchar *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)"; if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) { _status.text = @"Failed to create table"; } sqlite3_close(_contactDB); } else { _status.text = @"Failed to open/create database"; } } }
Implementing the Code to Save Data to the SQLite Database
- (IBAction)saveData:(UIButton *)sender { sqlite3_stmt *statement; constchar *dbpath = [_databasePathUTF8String]; if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) { NSString *insertSQL = [NSStringstringWithFormat: @"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")", self.name.text, self.address.text, self.phone.text]; constchar *insert_stmt = [insertSQL UTF8String]; sqlite3_prepare_v2(_contactDB, insert_stmt, -1, &statement, NULL); if (sqlite3_step(statement) == SQLITE_DONE) { self.status.text = @"Contact added"; self.name.text = @""; self.address.text = @""; self.phone.text = @""; } else { self.status.text = @"Failed to add contact"; } sqlite3_finalize(statement); sqlite3_close(_contactDB); }
}
Implementing Code to Extract Data from the SQLite Database
- (IBAction)findContact:(UIButton *)sender { constchar *dbpath = [_databasePathUTF8String]; sqlite3_stmt *statement; if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK) { NSString *querySQL = [NSStringstringWithFormat: @"SELECT address, phone FROM contacts WHERE name=\"%@\"", _name.text]; constchar *query_stmt = [querySQL UTF8String]; if (sqlite3_prepare_v2(_contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK) { if (sqlite3_step(statement) == SQLITE_ROW) { NSString *addressField = [[NSStringalloc] initWithUTF8String: (constchar *) sqlite3_column_text(statement, 0)]; _address.text = addressField; NSString *phoneField = [[NSStringalloc] initWithUTF8String: (constchar *)sqlite3_column_text(statement, 1)]; _phone.text = phoneField; _status.text = @"Match found"; } else { _status.text = @"Match not found"; _address.text = @""; _phone.text = @""; } sqlite3_finalize(statement); } sqlite3_close(_contactDB); }
}
One Tips for Text Input box
Add this in the View Controller
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"touchesBegan:withEvent:"); [self.viewendEditing:YES]; [supertouchesBegan:touches withEvent:event];
}
References:
http://sillycat.iteye.com/blog/1837717
https://developer.apple.com/library/ios/search/?q=SQLite
sample code
https://developer.apple.com/library/ios/samplecode/CoreDataBooks/Introduction/Intro.html
Directly use SQLite
http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_6_iPhone_Application
http://stackoverflow.com/questions/4314866/what-is-the-difference-between-libsqlite3-dylib-and-libsqlite3-0-dylib
http://stackoverflow.com/questions/989558/best-practices-for-in-app-database-migration-for-sqlite
http://www.sqlite.org/lang_altertable.html
Core Data
http://www.techotopia.com/index.php/Working_with_iOS_6_iPhone_Databases_using_Core_Data
http://www.techotopia.com/index.php/An_iOS_6_iPhone_Core_Data_Tutorial
http://cases.azoft.com/database-migration-ios-applications/
text field editor
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uitextfield-uitextfielddelegate/
http://www.techotopia.com/index.php/IOS_7_App_Development_Essentials