TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Sep 2016 |
SPMSupports SPM | ✗ |
Maintained by Louis BODART.
CoreDataHelper is a tiny Swift Framework that helps you to easily manage CoreData objects in the main context.
To install this, simply add the .xcodeproj
to your project, and do not forget to link the .framework
.
If you’re using cocoapods, just add pod 'CoreDataHelper'
into your Podfile
file.
Whenever you want to use it in your code, simply type :
import CoreDataHelper
To start with CoreDataHelper, it is pretty simple. Just go to your AppDelegate.swift
file and add the following code :
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
CDHelper.initializeWithMainContext(self.managedObjectContext)
return true
}
Note that your project must use CoreData.
Let’s create an entity named User
which contains 4 properties :
Once you created the class User
which inherit of NSManagedObject
, just add the CDHelperEntity
protocol like that :
class User: NSManagedObject, CDHelperEntity {
static var entityName: String! { return "User" } // Required
@NSManaged var first_name: String?
@NSManaged var age: NSNumber?
@NSManaged var email: String?
@NSManaged var last_name: String?
}
You just need to add the entityName
variable to be conform with the CDHelperEntity
protocol and that’s it ! You’re ready to use all the features of CoreDataHelper !
There are two ways to create a new entity. First, you can create an empty entity and fill it after :
let user: User = User.new()
user.first_name = "John"
user.last_name = "Doe"
user.email = "[email protected]"
user.age = 42
You can also create an entity using a data dictionary :
let userData: [String: AnyObject?] = [
"first_name": "John",
"last_name": "Doe",
"email": "[email protected]",
"age": 42,
]
let user: User = User.new(userData)
To save your entity, simply use the .save()
method like that :
let user: User = User.new()
// ...
user.save()
If you don’t need your entity anymore, you can use the .destroy()
method :
let user: User = User.new()
// ...
user.save()
// ... Ok, let's admit you want to delete your user
user.destroy()
There are different ways to retrieve your entities.
let users: [User] = User.findAll()
let users: [User] = User.findAll(usingSortDescriptors: [NSSortDescriptor(key: "first_name", ascending: true)])
let user: User? = User.findOne("first_name=\"John\"")
let users: [User] = User.find("first_name=\"John\"")
let users: [User] = User.find("first_name=\"John\"", usingSortDescriptors: [NSSortDescriptor(key: "first_name", ascending: true)])
let users: [User] = User.find("first_name=\"John\"", limit: 5)
let users: [User] = User.find("first_name=\"John\"", usingSortDescriptors: [NSSortDescriptor(key: "first_name", ascending: true)], limit: 5)
User.asynchronouslyFindAll { (results: [User]) -> Void in
// ...
}
User.asynchronouslyFindAll(usingSortDescriptors: [NSSortDescriptor(key: "first_name", ascending: true)]) { (results: [User]) -> Void in
// ...
}
User.asynchronouslyFindOne("first_name=\"John\"") { (user: User?) -> Void in
// ...
}
User.asynchronouslyFind("first_name=\"John\"") { (user: [User]) -> Void in
// ...
}
User.asynchronouslyFind("first_name=\"John\"", usingSortDescriptors: [NSSortDescriptor(key: "first_name", ascending: true)]) { (user: [User]) -> Void in
// ...
}
User.asynchronouslyFind("first_name=\"John\"", limit: 5) { (user: [User]) -> Void in
// ...
}
User.asynchronouslyFind("first_name=\"John\"", usingSortDescriptors: [NSSortDescriptor(key: "first_name", ascending: true)], limit: 5) { (user: [User]) -> Void in
// ...
}