MCCoreDataStack 0.9.1

MCCoreDataStack 0.9.1

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

Marco Cattai 维护。



  • [Marco Cattai]

MCCoreDataStack

MCCoreDataStack 是一个简单的 SWIFT 包装器,用于创建、保存和检索 Apple 的 Core Data 框架中的 Managed Objects

文档

API 参考

介绍

这是一个非常简单、线程安全的 swift 库,有助于您深入 Core Data 框架。我是为个人项目创建的它,自从 2016 年 2 月以来就放在我的个人库中。

这个库已被成功使用,没有特别的问题。在 GitHub 上,它可能对其他开发者有用,希望他们将会为其做出贡献。请自由创建 pull requests。谢谢。

MCCoreDataStackManager

这个类包含设置 CoreData Stack 所有基本功能。它使用 Core Data 大师 Marcus Zarra 描述的方法,该方法基于上述父子方法,但是添加了一个专门用于写入磁盘的额外上下文。长时间写入操作可能会在短时间内阻塞主线程,导致 UI 冻结。这种智能方法将写入解耦到自己的私有队列中,并保持 UI 平滑。

alt tag

MCCoreDataRepository

这个类包含了使用 MCCoreDataStackManager 设置 CoreDataStack 的辅助方法。此类提供了创建、删除和检索 NSManagedObject 的基本功能。

集成到现有项目中

手动(iOS 7+)

要在这个库上使用 iOS 7

  • 将 Library 文件夹拖放到项目树中
  • 构建项目
  • #import “[YourProductModuleName]-Swift.h”

此库支持链式操作

此库支持写入-读取-读取歧义操作的链式操作。更多信息请参阅以下示例。

如何使用

设置 CoreDataStack

self.coreDataRepo = MCCoreDataRepository()
let success = self.coreDataRepo.setup(storeName: "TestDB.sqlite", domainName: "co.uk.tests")

在后端创建一个对象,在主队列中获取它,并在后端删除它

//Here we define our Dictionaries

let subDictionary: [String: AnyObject] = ["subCategoryID": "sub12345", "subCategoryName": "subTest12345"]
let dictionary: [String: AnyObject] = ["categoryID": "12345", "categoryName": "Test12345", "subCategory": subDictionary]

self.coreDataRepo.write(operationBlock: { (context) in

    self.coreDataRepo.create(dictionary: dictionary, entityName: "MCCategoryTest", context: context)

   }) { (error) in

    //Object is persisted on disk

})

//Here we don't use chaining

self.coreDataRepo.read_MT { (context) in

    let results = self.coreDataRepo.fetch(entityName: "MCCategoryTest", context: context, resultType: .ManagedObjectResultType) as? [NSManagedObject]

    // Objects will be deleted in a background thread. Deletion will fetch the objects from the background context
    self.coreDataRepo.delete(containedInArray: results, completionBlock: nil)
}

让我们尝试在后端创建5000个假对象,然后获取它们,使用链式调用来填充UI

var dataSource: [AnyObject]? = nil

self.coreDataRepo.write(operationBlock: { (context) in

            for index in 0..<5000 {

                let categoryID = String(index)
                let categoryName = "categoryName"
                let dictionary: [String: AnyObject] = ["categoryID": categoryID, "categoryName": categoryName]
                self.coreDataRepo.create(dictionary: dictionary, entityName: "MCCategoryTest", context: context)
            }


    }) { (error) in
            //Here they are persisted

}.read_MT { (context) in

    dataSource = self.coreDataRepo.fetch(entityName: "MCCategoryTest", context: context, resultType: .ManagedObjectResultType)
}

现在我们想要更新上面示例的dataSource

更新后,我们希望在主上下文中请求数据(由主线程使用)

self.coreDataRepo.write(operationBlock: { (context) in

    let objs = context.moveInContext(managedObjects: dataSource as! [NSManagedObject])

    for obj in objs {
            if let category = obj as? MCCategoryTest {
                    category.categoryName = "UPDATED"
                }
        }

}) { (error) in
            //Here they are persisted
}.read_MT { (context) in
    dataSource = self.coreDataRepo.fetch(entityName: "MCCategoryTest", context: context, resultType: .ManagedObjectResultType)

    //Here we have our updated objects

}

在后端读取和更新对象

    self.coreDataRepo.write(operationBlock: { (context) in
        let results = self.coreDataRepo.fetch(byPredicate: NSPredicate(format: "%K = %@", "categoryName", "categoryName"), entityName: "MCCategoryTest", context: context, resultType: .ManagedObjectResultType)

        //Here we update our objects in BKG

    }, completion: nil)

… 请参阅单元测试。在单元测试中,我已测试了创建/获取和删除数千个对象的操作

跟踪违反规则

1) 请在应用程序启动期间通过传递以下参数来启用Enable Core Data多线程断言。

-com.apple.CoreData.ConcurrencyDebug 1

2) 验证Xcode是否在控制台中打印以下文本,以指示已启用多线程断言。

CoreData: annotation: Core Data multi-threading assertions enabled.

3) 一旦启用Core Data调试,每当应用程序尝试从错误上下文中访问托管对象实例时,Xcode都会抛出异常。您可能想将此更改检查到您的版本控制系统中,这样您的团队能够在开发早期早期跟踪这些违规行为。