FacadeSwift 0.1.2

FacadeSwift 0.1.2

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2016年11月
SwiftSwift 版本2.3
SPM支持 SPM

Olivier Thierry 维护。



  • 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")

使用 CoreData 内置选项

当然,您可以通过使用共享 Configoptions 属性来使用 CoreData 内置选项。

facade_stack.config.options[NSMigratePersistentStoresAutomaticallyOption] = true
facade_stack.config.options[NSInferMappingModelAutomaticallyOption] = true

连接到数据库

配置阶段之后,您必须建立与数据库的连接。此操作将持久存储添加到持久存储协调器。

try! facade_stack.connect()

查询 API

查询API拥有灵活性和强大的功能。您不需要关心NSFetchRequestSQLite的底层,您可以直接使用高级API,并专注于应用程序的功能部分。

查询选项

  • 匹配选项

    • 不区分大小写
    • 不区分重音符号
  • 自定义查询

    • limit(_:): 正在编写文档
    • offset(_:): 正在编写文档
    • fetchBatchSize(_:): 正在编写文档
    • prefetch(_:): 正在编写文档
    • faults(_:): 正在编写文档
    • groupBy(_:): 正在编写文档
    • refresh(_:): 正在编写文档

选择查询管理对象上下文

默认情况下,请求总是在facade_stack.mainManagedObjectContext中执行。您可以通过指定查询必须执行的上下文来覆盖此行为。

let user = facade_query(User)
  .inManagedObjectContext(myCustomContext)
  .first()

谓词

with(_:equalTo:options:) / with(_:notEqualTo:options:)

let recipes = facade_query(Recipe)
  .with("difficulty", equalTo: 3)
  .all()
let users = facade_query(User)
  .with(
    "firstname",
    notEqualTo: "olivier",
    options: [
      .CaseInsensitive,
      .DiacriticInsensitive])
  .all()

with(_:containedIn:) / with(_:notContainedIn:])

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()

with(_:containingAll:) / with(_:containingNone:) / with(_:containingAny:)

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()

with(_:containing:options:) / with(_:like:options:)

let users = facade_query(User)
  .with(
    "firstname",
    containing: "liv")
  .all()
let users = facade_query(User)
  .with(
    "firstname",
    like: "oliv%",
    options: [.CaseInsensitive])
  .all()

with(_:existing:)

匹配存在(!= nil)或不存在(== nil)的属性谓词

let usersWithoutEmail = facade_query(User)
  .with("email", existing: false)
  .all()

with(_:startingWith:options:) / with(_:endingWith:options:)

这些函数是针对LIKE pattern%LIKE %pattern的语法函数

let users = facade_query(User)
  .with("email", endingWith: "@gmail.com")
  .all()

with(_:greaterThan:) / with(_:greaterThanOrEqual:) / with(_:lowerThan:) / with(_:lowerThanOrEqual:)

let users = facade_query(User)
  .with("age", greaterThanOrEqual: 18)
  .all()

.or()

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()

您可以在排序谓词中添加DESCdesc(不区分大小写),以更改排序顺序。

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

all() 与 execute() 与 count()

有三种执行查询的方式。

  • count()按其名称返回匹配实体的简单计数。
  • all()将始终返回一个[A]数组,其中A是您的实体类型。
  • execute()有三种签名

    • execute() -> [A]:与all()行为相同
    • execute() -> [NSManagedObjectID]:将获取请求的结果类型设置为.ManagedObjectIDResultType,并返回一个NSManagedObjectID数组。
    • execute() -> [NSDictionary]:将获取请求的结果类型设置为.DictionaryResultType,并以NSDictionary实例的形式返回原始对象表示。

    在大多数情况下,我只是使用execute()并让编译器推断我期望的类型,它在这方面做得相当不错 <3。

first() & last()

在某些情况下,您可能想要获取与查询匹配的第一个或最后一个对象。默认情况下,first()last()将表现完全相同。默认情况下,这意味着query.first() === query.last()。因为这两个方法会将限制设置为1,并按facade_stack.config.modelPrimaryKey排序结果集,如果您最初没有设置它,这将不会有任何效果。

在您的栈配置期间。添加以下内容

facade_stack.config.modelPrimaryKey = "id"

从现在开始,query.first() != query.last()(当然,除非query.count() <= 1),因为Facade将检测默认主键并相应地排序结果。

Facade.Query到NSFetchedResultsController

正在编写文档

弹性查询

正在编写文档

事务

正在编写文档

运算符

  • <->正在编写文档
  • <=>正在编写文档
  • </>正在编写文档
  • <-正在编写文档

作者

Olivier Thierry,[email protected]

许可证

FacadeSwift在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。