CoreData笔记

  • Core Data Programming Guide
    • 数据库的table对应object,table的column对应property
    • NSManagedObjectContext通过coordinator管理存储,NSPersistentStoreCoordinator将实际的NSPersistentStore(如sqlite文件)和NSManagedObjectModel(编译后的data model)关联。
  • CoreData使用多个数据库:
    1. 在Xcode的DataModel中添加config,让entiey分属不同的config
    2. ManagedObjectContext只关联一个PersistentStoreCoordinator,coordinator中可添加多个PersistentStore,让这些store分别对应不同的config:[coordinator addPersistentStoreWithType:configuration:URL:options:error:]
    3. relationship只能用在同个PersistentStore中,跨store要使用FetchedRequest
    参考:stackoverflow

加密CoreData

  • 使用sqlcipher加密sqlite,可使用其Xcode5分支

    1. 编译:

       ./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

    2. sqlcipher.xcodeproj拉进Xcode作为子项目,在父项目Build Phases的Target DepedenciesLink Binary With Libraries中添加sqlcipher

    3. 在父项目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"