测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2016年11月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Marco Cattai 维护。
MCCoreDataStack 是一个简单的 SWIFT 包装器,用于创建、保存和检索 Apple 的 Core Data 框架中的 Managed Objects
这是一个非常简单、线程安全的 swift 库,有助于您深入 Core Data 框架。我是为个人项目创建的它,自从 2016 年 2 月以来就放在我的个人库中。
这个库已被成功使用,没有特别的问题。在 GitHub 上,它可能对其他开发者有用,希望他们将会为其做出贡献。请自由创建 pull requests。谢谢。
这个类包含设置 CoreData Stack 所有基本功能。它使用 Core Data 大师 Marcus Zarra 描述的方法,该方法基于上述父子方法,但是添加了一个专门用于写入磁盘的额外上下文。长时间写入操作可能会在短时间内阻塞主线程,导致 UI 冻结。这种智能方法将写入解耦到自己的私有队列中,并保持 UI 平滑。
这个类包含了使用 MCCoreDataStackManager 设置 CoreDataStack 的辅助方法。此类提供了创建、删除和检索 NSManagedObject 的基本功能。
要在这个库上使用 iOS 7
此库支持写入-读取-读取歧义操作的链式操作。更多信息请参阅以下示例。
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)
}
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)
}
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都会抛出异常。您可能想将此更改检查到您的版本控制系统中,这样您的团队能够在开发早期早期跟踪这些违规行为。