- Core Data Programming Guide
- 数据库的table对应object,table的column对应property
- NSManagedObjectContext通过coordinator管理存储,NSPersistentStoreCoordinator将实际的NSPersistentStore(如sqlite文件)和NSManagedObjectModel(编译后的data model)关联。
- CoreData使用多个数据库:
- 在Xcode的DataModel中添加config,让entiey分属不同的config
- ManagedObjectContext只关联一个PersistentStoreCoordinator,coordinator中可添加多个PersistentStore,让这些store分别对应不同的config:[coordinator addPersistentStoreWithType:configuration:URL:options:error:]
- relationship只能用在同个PersistentStore中,跨store要使用FetchedRequest
加密CoreData
使用sqlcipher加密sqlite,可使用其Xcode5分支:
编译:
./configure --with-crypto-lib=commoncrypto --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2 -DSQLCIPHER_CRYPTO_CC" LDFLAGS="-framework Security" && make sqlite3.c
要禁掉sqlcipher项目的warnings,在Build Phases的
Compile Sources
中给sqlite3.c加上Compiler Flags:-w
把
sqlcipher.xcodeproj
拉进Xcode作为子项目,在父项目Build Phases的Target Depedencies
和Link Binary With Libraries
中添加sqlcipher
在父项目Build Settings的
Other C Flags
中添加:-DSQLITE_HAS_CODEC
使用encrypted-core-data加密CoreData,encrypted-core-data依赖sqlcipher
使用MagicalRecord方便CoreData,见使用入门
使用前需要在
.pch
文件中添加:#define MR_SHORTHAND #import "CoreData+MagicalRecord.h"