CRDCoreDataStack
基于 Swift 的简单直观的 Core Data stack。
概述
我厌倦了给每个创建的 Swift 应用程序添加相同的 Core Data 范本代码,所以我就把代码整合成这个小的框架以实现更容易的复用。
要求
- iOS 11.0+ / macOS 10.12+ / watchOS 3.0+ / tvOS 11.0+
- Xcode 10.2+
- Swift 5.0+
安装
你可以简单地将以下文件从 GitHub 树复制到你的项目中
CRDCoreDataStack.swift
- Swift 类,用于封装所有必要的 Core Data 范本代码。
使用CocoaPods
或者,您也可以将其作为CocoaPods安装
CocoaPods是一个Cocoa项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
构建CRDCoreDataStack需要CocoaPods 1.7.0+版本
要使用CocoaPods将CRDCoreDataStack集成到您的Xcode项目中,请在您的Podfile
中指定
target 'MyApp' do
use_frameworks!
# Pods for MyApp
pod 'CRDCoreDataStack'
end
然后,执行以下命令
$ pod install
使用Carthage
您可以使用Carthage将此框架添加到项目中
- 在您的xcodeproj文件所在的目录中添加一个
Cartfile
- 编辑此文件以指定此框架的1.0或更高版本发布
github "cdisdero/CRDCoreDataStack" >= 1.0
- 运行Carthage以添加框架源并构建此框架
carthage update
- 在Xcode项目应用目标的通用标签页中,将Carthage构建的框架拖放到底部链接框架和库列表。您可以在项目中找到构建的框架,位置在子文件夹Carthage/Build/[target]/CRDCoreDataStack.framework,其中[target]可能是'iOS'等。
- 为了确保框架已正确删除发布应用程序中的多余CPU类型,请为您应用目标的Run Script构建阶段添加以下命令
/usr/local/bin/carthage copy-frameworks
这假定Carthage已安装在您的系统中/usr/local/bin
。也在相同的Run Script构建阶段中指定以下输入文件
$(SRCROOT)/Carthage/Build/iOS/CRDCoreDataStack.framework
使用方法
该库很容易使用。只需导入CRDCoreDataStack并创建一个新对象
let coreDataStack = CRDCoreDataStack(modelName, delegate)
modelName参数是Core Data xcdatamodeld文件的名称。第二个可选的delegate参数是一个实现了CRDCoreDataStackProtocol的类实例的引用, 该协议包含一个名为'seedDataStore'的函数,该函数允许您在首次创建时对模型数据存储进行初始化。
func seedDataStore() {
do {
// Create and insert some new data objects in the store.
let context = self.coreDataStack.privateChildManagedObjectContext()
// First object
var myDataObject = try MyDataObject(context: context)
myDataObject.name = "Frank"
// Second object
myDataObject = try MyDataObject(context: context)
myDataObject.name = "John"
// Third object
myDataObject = try MyDataObject(context: context)
myDataObject.name = "Chris"
// Commit the new objects to the private context.
try context.save()
// Save all private and main context changes to the data store.
self.coreDataStack.saveChanges(onError: { (error) in
if let error = error {
print(error)
}
})
} catch let error {
print(error)
}
}
您可能想要将上面创建的coreDataStack实例存储在应用程序中的中央位置,以便从大多数代码中访问,例如在AppDelegate中。
使用CRDCoreDataStack实例,您可以访问模型的主NSManagedObjectContext,同时创建私有子上下文。
let mainContext = coreDataStack.mainManagedObjectContext
let privateContext = coreDataStack.privateChildManagedObjectContext()
初始化Core Data栈的过程可能需要一些时间,因此在调用CRDCoreDataStack初始化器后,根据初始化是否成功,会抛出两个通知。
CRDCoreDataStack.notificationInitialized
CRDCoreDataStack.notificationInitializedFailed
在您对数据存储进行任何操作之前,请等待收到 CRDCoreDataStack.notificationInitialization
。以下是一个示例代码,展示如何使用这些通知。
// Notification that the stack was successfully initialized and the database was seeded
// (if it was newly created)
NotificationCenter.default.addObserver(forName: Notification.Name(CRDCoreDataStack.notificationInitialized), object: nil, queue: OperationQueue.main) { (notification) in
do {
// Create and insert a new data object on a private context.
let context = self.coreDataStack.privateChildManagedObjectContext()
let myDataObject = try MyDataObject(context: context)
myDataObject.name = "Frank"
// Commit the new object to the private context.
try context.save()
// Save all private and main context changes to the data store.
self.coreDataStack.saveChanges(onError: { (error) in
if let error = error {
print(error)
}
})
} catch let error {
print(error)
}
}
// Notification that the stack failed to initialize.
NotificationCenter.default.addObserver(forName: Notification.Name(CRDCoreDataStack.notificationInitializedFailed), object: nil, queue: OperationQueue.main) { (notification) in
// See if the notification has an error in the userInfo.
if let userInfo = notification.userInfo, userInfo.count > 0 {
if let error = userInfo["error"] {
print(error)
}
}
}
您可以使用 saveChanges(onError:) 函数将私有和主上下文中的任何更改保存到磁盘。
coreDataStack.saveChanges { (error) in
print(error)
}
结论
希望这个小型库/框架对您的下一个 Swift 项目有所帮助。我会根据时间和兴趣不断更新,并当然欢迎您的所有反馈。
许可
CRDCoreDataStack 在 Apache 2.0 许可下发布。有关详细信息,请参阅 LICENSE。