CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.
TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Mar 2015 |
SPMSupports SPM | ✗ |
Maintained by Rafael Veronezi.
CoreDataContext is a Cocoa Touch Framework written in Swift to allow fast integration of Core Data into your iOS projects, and easy data operations on defined entities.
This release version brings advances to the Library:
CoreDataContext is fully implemented in Swift, and is distributed as a Cocoa Touch Framework Library, and is also available via CocoaPods.
Cocoa Touch Frameworks requires minimum deployment target of iOS 8. To use CoreDataContext on projects that need to support iOS 7, you need to include the Source Code directly in your project.
CoreDataContext is packaged as a Xcode Project with two targets: 1. CoreDataContext_iOS: a Cocoa Touch Framework to be linked with iOS projects. 2. CoreDataCOntext_OSX: a Cocoa Framework to be linked with OS X projects.
You can download or clone the code to your project, or clone this repo as a Submodule, then add the Framework as a Linked Binary in your App’s target, based on the needed platform.
Add the following files to your source:
CoreDataContext provides very simple API for interacting with your Core Data Model entities. First you need to derive from BaseDataContext, which contains all the interactions with the NSManagedObjectContext. Set the name of the model using it’s super initializer. Declare the properties to access your entities using the generic EntityDataSource class, and initilize then with the entity and key field name, as the template from the Sample Project:
class NotesContext: BaseDataContext {
//
// MARK: - Entity Data Source Properties
var categories: EntityDataSource<Category, NSNumber>!
var notes: EntityDataSource<Note, NSString>!
//
// MARK: - Initializers
init() {
super.init(resourceName: "Notes")
if let moc = self.managedObjectContext {
self.categories = EntityDataSource(managedObjectContext: moc,
entityName: "Category",
entityKeyName: "categoryId",
entityKeyGeneration: PrimaryKeyGeneration.AutoNumber);
self.notes = EntityDataSource(managedObjectContext: moc,
entityName: "Note",
entityKeyName: "noteId",
entityKeyGeneration: PrimaryKeyGeneration.UUID);
}
}
}
Optionally you can set your data Context as a Singleton, adding the property:
//
// MARK: - Singleton
class var sharedInstance: NotesContext {
struct Singleton {
static var instance: NotesContext?
static var token: dispatch_once_t = 0
}
dispatch_once(&Singleton.token) {
Singleton.instance = NotesContext()
}
return Singleton.instance!
}
Be sure to replace the types appropriately. A few things to note:
Checkout the source for EntityDataSource.swift to see all the available methods. Follows a short list:
The NotesApp Project is an very simple note taking iOS App, that employs CoreDataContext to provide simple model management. You can see and download the Sample project code from it’s GitHub Repository.
This library is not thread safe. It’s best suited to be running as a Singleton. It’s main target is for small and simple projects that employs no complex Core Data Models.
Thanks for using CoreDataContext. If you like this API please contribute and help us making a bigger and more complete Core Data API using the power of Swift!! You’re welcome to Fork this repo and do your Pull Requests.