测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新发布 | 2016年11月 |
SwiftSwift 版本 | 2.3 |
SPM支持 SPM | ✗ |
由 Olivier Thierry 维护。
文档编写中
Facade
作为您的应用程序和 CoreData
之间的外观,使配置和使用变得更加简单,提供了灵活的父子架构和强大的查询 API。
Facade.Stack
构建了一个优化的 父子
堆栈架构,允许在持久化阶段进行高效的读取和异步写入操作,同时还支持断开事务。
FacadeSwift 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod "FacadeSwift"
facade_stack.config.storeName = "MyApp"
facade_stack.connect()
这就是开始使用 Facade
所需要做的全部。有关更深入的配置和使用,请参阅以下文档。
您可以在首次设置数据库时使用种子。
facade_stack.config.seedURL = NSBundle.mainBundle()
.URLForResource(
"myseed",
withExtension: "sqlite")
// Install the seed
try! facade_stack.seed()
try! facade_stack.connect()
// Check that database exists before trying to drop it
if facade_stack.installed {
try! facade_stack.drop()
}
如果您的数据模型模式不在主包中(在框架或资源包中),您必须提供 URL,以便 CoreData 可以获取它。
facade_stack.config.modelURL = myBundle
.URLForResource(
"MyAppDataModel",
withExtension: "momd")
当然,您可以通过使用共享 Config
的 options
属性来使用 CoreData 内置选项。
facade_stack.config.options[NSMigratePersistentStoresAutomaticallyOption] = true
facade_stack.config.options[NSInferMappingModelAutomaticallyOption] = true
在 配置阶段之后,您必须建立与数据库的连接。此操作将持久存储添加到持久存储协调器。
try! facade_stack.connect()
查询API拥有灵活性和强大的功能。您不需要关心NSFetchRequest
或SQLite
的底层,您可以直接使用高级API,并专注于应用程序的功能部分。
匹配选项
不区分大小写
不区分重音符号
自定义查询
limit(_:)
: 正在编写文档offset(_:)
: 正在编写文档fetchBatchSize(_:)
: 正在编写文档prefetch(_:)
: 正在编写文档faults(_:)
: 正在编写文档groupBy(_:)
: 正在编写文档refresh(_:)
: 正在编写文档默认情况下,请求总是在facade_stack.mainManagedObjectContext
中执行。您可以通过指定查询必须执行的上下文来覆盖此行为。
let user = facade_query(User)
.inManagedObjectContext(myCustomContext)
.first()
let recipes = facade_query(Recipe)
.with("difficulty", equalTo: 3)
.all()
let users = facade_query(User)
.with(
"firstname",
notEqualTo: "olivier",
options: [
.CaseInsensitive,
.DiacriticInsensitive])
.all()
let user = facade_query(User)
.with(
"email",
containedIn: [
"[email protected]",
"[email protected]"])
.first()
let roles = facade_query(Role)
.limit(2)
.all()
let users = facade_query(User)
.with(
"role",
notContainedIn: roles)
.all()
let recipes = facade_query(Recipe)
.with("ingredients", containingAll: listOfAvailableIngredients)
.all()
let recipes = facade_query(Recipe)
.with("ingredients", containingAny: listOfAvailableIngredients)
.all()
let recipes = facade_query(Recipe)
.with("ingredients", containingNone: listOfAvailableIngredients)
.all()
let users = facade_query(User)
.with(
"firstname",
containing: "liv")
.all()
let users = facade_query(User)
.with(
"firstname",
like: "oliv%",
options: [.CaseInsensitive])
.all()
匹配存在(!= nil
)或不存在(== nil
)的属性谓词
let usersWithoutEmail = facade_query(User)
.with("email", existing: false)
.all()
这些函数是针对LIKE pattern%
和LIKE %pattern
的语法函数
let users = facade_query(User)
.with("email", endingWith: "@gmail.com")
.all()
let users = facade_query(User)
.with("age", greaterThanOrEqual: 18)
.all()
let recipes = facade_queryOr([
// Vegan AND difficulty maximum (5)
facade_query(Recipe)
.with("difficulty", equalTo: 5)
.with("vegan", equalTo: true),
// Non-vegan AND difficulty between 3-5
facade_query(Recipe)
.with("difficulty", greaterThanOrEqualTo: 3)
.with("vegan", equalTo: false)
])
// In both case, we want recipes to take less than 30 mins to cook
.with("time", lessThanOrEqualTo: 30)
.sort("difficulty")
.all()
默认情况下,使用sort(_:)
将按升序排序结果。
facade_query(User)
.sort("email")
.all()
// Is exactly the same as
facade_query(User)
.sort("email ASC")
.all()
您可以在排序谓词中添加DESC
或desc
(不区分大小写),以更改排序顺序。
facade_query(User)
.sort("email DESC")
.all()
您可以直接从查询对象中删除/批量删除匹配的结果。
facade_query(Recipe)
.with("favorite", equalTo: false)
.delete()
facade_query(Recipe)
.with("favorite", equalTo: false)
.batchDelete() // Async delete using NSBatchDeleteRequest
有三种执行查询的方式。
count()
按其名称返回匹配实体的简单计数。all()
将始终返回一个[A]数组,其中A是您的实体类型。execute()
有三种签名
execute() -> [A]
:与all()
行为相同execute() -> [NSManagedObjectID]
:将获取请求的结果类型设置为.ManagedObjectIDResultType
,并返回一个NSManagedObjectID
数组。execute() -> [NSDictionary]
:将获取请求的结果类型设置为.DictionaryResultType
,并以NSDictionary
实例的形式返回原始对象表示。在大多数情况下,我只是使用execute()
并让编译器推断我期望的类型,它在这方面做得相当不错 <3。
在某些情况下,您可能想要获取与查询匹配的第一个或最后一个对象。默认情况下,first()
和last()
将表现完全相同。默认情况下,这意味着query.first() === query.last()
。因为这两个方法会将限制设置为1,并按facade_stack.config.modelPrimaryKey
排序结果集,如果您最初没有设置它,这将不会有任何效果。
在您的栈配置期间。添加以下内容
facade_stack.config.modelPrimaryKey = "id"
从现在开始,query.first() != query.last()
(当然,除非query.count() <= 1
),因为Facade
将检测默认主键并相应地排序结果。
正在编写文档
正在编写文档
正在编写文档
<->
:正在编写文档<=>
:正在编写文档</>
:正在编写文档<-
:正在编写文档Olivier Thierry,[email protected]
FacadeSwift在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。