⚠️ 由于此存储库即将被存档,建议您迁移到 NSPersistentContainer(自 iOS 10 可用)而不是它。对于除了栈管理之外的其他便利辅助程序,我目前仅使用 这个。
AERecord
Core Data (iOS, macOS, tvOS) 的超级棒的 Swift 小助手
索引
简介
AECoreDataUI 之前是 AERecord 的一部分,因此您可能还需要检查。
我们为什么还需要另一个 Core Data 包装器?告诉您!
启发了众多不同(剧透警告)神奇的解决方案,我希望某样东西能够完美地结合复杂性和功能性。所有用于设置Core Data堆栈的样板代码,传递正确的NSManagedObjectContext
跨越整个项目和各种线程,更不用说任何类型数据的创建或查询的枯燥的NSFetchRequest
样板代码 - 这将变得更加简单。
特性
- 轻松创建或自定义可从任何地方便捷访问的Core Data堆栈(或更多堆栈)
- 拥有主和背景上下文,始终处于同步状态,但不必担心
- 以多种方式使用
CRUD
数据,并有通用的一行代码
- 支持
iCloud
- 包含
单元测试
- 包含内联文档
用法
例如,您可以查看此演示项目。
创建Core Data堆栈
几乎在AERecord
中所有内容都通过“可选”参数(如果未指定任何内容,将具有默认值)来创建。
因此,您可以像这样加载(如果尚不存在则创建)CoreData堆栈:
do {
try AERecord.loadCoreDataStack()
} catch {
print(error)
}
或像这样:
let myModel: NSManagedObjectModel = AERecord.modelFromBundle(for: MyClass.self)
let myStoreType = NSInMemoryStoreType
let myConfiguration = ...
let myStoreURL = AERecord.storeURL(for: "MyName")
let myOptions = [NSMigratePersistentStoresAutomaticallyOption : true]
do {
try AERecord.loadCoreDataStack(managedObjectModel: myModel, storeType: myStoreType, configuration: myConfiguration, storeURL: myStoreURL, options: myOptions)
} catch {
print(error)
}
或这些的组合。
如果您出于任何原因想要完全删除您的堆栈并重新开始(例如,独立的演示数据堆栈),您可以这样做,就像这样简单:
do {
try AERecord.destroyCoreDataStack() // destroy default stack
} catch {
print(error)
}
do {
let demoStoreURL = AERecord.storeURL(for: "Demo")
try AERecord.destroyCoreDataStack(storeURL: demoStoreURL) // destroy custom stack
} catch {
print(error)
}
类似地,您可以像这样删除所有实体的全部数据(不改动堆栈):
AERecord.truncateAllData()
上下文操作
如果您没有指定任何内容,则会使用当前线程的上下文(以下所有示例都使用Context.default
)。
// get context
AERecord.Context.main // get NSManagedObjectContext for main thread
AERecord.Context.background // get NSManagedObjectContext for background thread
AERecord.Context.default // get NSManagedObjectContext for current thread
// execute NSFetchRequest
let request = ...
let managedObjects = AERecord.execute(fetchRequest: request) // returns array of objects
// save context
AERecord.save() // save default context
AERecord.saveAndWait() // save default context and wait for save to finish
// turn managed objects into faults (you don't need this often, but sometimes you do)
let objectIDs = ...
AERecord.refreshObjects(with: [objectIDs], mergeChanges: true) // turn objects for given IDs into faults
AERecord.refreshRegisteredObjects(mergeChanges: true) // turn all registered objects into faults
简单查询
简单的查询助手作为NSManagedObject
扩展创建。
所有查询都在通用的NSManagedObject
上调用,如果未指定任何内容(以下所有示例均使用Context.default
)。所有查找器都有可选的NSSortDescriptor
参数,但在这些示例中未使用。要查看更多示例,请查看单元测试。
通用
如果您需要自定义NSFetchRequest
,可以使用createPredicate(with:)
和createFetchRequest(predicate:sortdDescriptors:)
进行调整,按要求执行并使用AERecord
。
// create request for any entity type
let attributes = ...
let predicate = NSManagedObject.createPredicate(with: attributes)
let sortDescriptors = ...
let request = NSManagedObject.createFetchRequest(predicate: predicate, sortDescriptors: sortDescriptors)
// set some custom request properties
request.someProperty = someValue
// execute request and get array of entity objects
let managedObjects = AERecord.execute(fetchRequest: request)
当然,所有创建、查找、计数或删除实体的常用请求都已经准备好了,请继续阅读。
创建
NSManagedObject.create() // create new object
let attributes = ...
NSManagedObject.create(with: attributes) // create new object and sets it's attributes
NSManagedObject.firstOrCreate(with: "city", value: "Belgrade") // get existing object (or create new if it doesn't already exist) with given attribute
let attributes = ...
NSManagedObject.firstOrCreate(with: attributes) // get existing object (or create new if it doesn't already exist) with given attributes
查找第一个
NSManagedObject.first() // get first object
let predicate = ...
NSManagedObject.first(with: predicate) // get first object with predicate
NSManagedObject.first(with: "bike", value: "KTM") // get first object with given attribute name and value
let attributes = ...
NSManagedObject.first(with: attributes) // get first object with given attributes
NSManagedObject.first(orderedBy: "speed", ascending: false) // get first object ordered by given attribute name
查找所有
NSManagedObject.all() // get all objects
let predicate = ...
NSManagedObject.all(with: predicate) // get all objects with predicate
NSManagedObject.all(with: "year", value: 1984) // get all objects with given attribute name and value
let attributes = ...
NSManagedObject.all(with: attributes) // get all objects with given attributes
删除
let managedObject = ...
managedObject.delete() // delete object (call on instance)
NSManagedObject.deleteAll() // delete all objects
NSManagedObject.deleteAll(with: "fat", value: true) // delete all objects with given attribute name and value
let attributes = ...
NSManagedObject.deleteAll(with: attributes) // delete all objects with given attributes
let predicate = ...
NSManagedObject.deleteAll(with: predicate) // delete all objects with given predicate
计数
NSManagedObject.count() // count all objects
let predicate = ...
NSManagedObject.count(with: predicate) // count all objects with predicate
NSManagedObject.count(with: "selected", value: true) // count all objects with given attribute name and value
let attributes = ...
NSManagedObject.count(with: attributes) // count all objects with given attributes
唯一
do {
try NSManagedObject.distinctValues(for: "city") // get array of all distinct values for given attribute name
} catch {
print(error)
}
do {
let attributes = ["country", "city"]
try NSManagedObject.distinctRecords(for: attributes) // get dictionary with name and values of all distinct records for multiple given attributes
} catch {
print(error)
}
自动递增
如果需要自动递增属性,只需创建一个 Int 类型的属性并获取下一个 ID,如下所示
NSManagedObject.autoIncrementedInteger(for: "myCustomAutoID") // returns next ID for given attribute of Integer type
将管理对象转换为错误
NSFetchedResultsController 被设计为一次只监视一个实体,但是当界面稍微复杂一点(例如显示相关实体数据时),有时必须手动刷新相关数据,这可以通过将“监视”的实体对象转换为错误来实现。这是做这件事的快捷方式(mergeChanges
参数默认为 true
)。您可以在 Core Data 文档中了解更多关于将对象转换为错误的信息。
let managedObject = ...
managedObject.refresh() // turns instance of managed object into fault
批量更新
批量更新是 iOS 8 中的“新”特性,它直接在持久化存储中执行操作,因此需要小心处理,并在开始之前先阅读文档。顺便说一下,NSPredicate
也是一个可选参数。
NSManagedObject.batchUpdate(properties: ["timeStamp" : NSDate()]) // returns NSBatchUpdateResult?
NSManagedObject.objectsCountForBatchUpdate(properties: ["timeStamp" : NSDate()]) // returns count of updated objects
NSManagedObject.batchUpdateAndRefreshObjects(properties: ["timeStamp" : NSDate()]) // turns updated objects into faults after updating them in persistent store
安装
-
.Package(url: "https://github.com/tadija/AERecord.git", majorVersion: 4)
-
github "tadija/AERecord"
-
pod 'AERecord'
许可证
AERecord采用MIT许可证发布。详情请见LICENSE。