Table of Contents
#AACoreData
##Description
AACoreData is a lightweight data persistence wrapper designed to provide an easier solution for CRUD
operations using CoreData
, written in Swift. It provides a singleton instance to access CoreData
objects anywhere in the code and uses 'value types' to define CoreData
entities.
##Demonstration
To run the example project, clone the repo, and run pod install
from the Example directory first.
##Requirements
- iOS 8.0+ / Mac OS X 10.9+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 8.0+
Installation
AACoreData can be installed using CocoaPods, Carthage, or manually.
##CocoaPods
AACoreData is available through CocoaPods. To install CocoaPods, run:
$ gem install cocoapods
Then create a Podfile with the following contents:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
target '<Your Target Name>' do
pod 'AACoreData', '~> 1.0'
end
Finally, run the following command to install it:
$ pod install
##Carthage
To install Carthage, run (using Homebrew):
$ brew update
$ brew install carthage
Then add the following line to your Cartfile:
github "EngrAhsanAli/AACoreData"
Then import the library in all files where you use it:
import AACoreData
##Manual Installation
Simply copy Classes/AACoreData.swift
to your Xcode project and that's it!
#Getting Started
##Define entities
extension AACoreData {
static let myEntity = AACoreEntity("ExampleEntity")
}
##Shared Instance
You can access the instance easily by adding this line in specific class or globally anywhere outside the class:
let instance = AACoreData.shared
##Creating your own data model
You have to create your own data model file in your project. By default "AACoreData.xcdatamodeld"
is assumed to find in your project bundle like ./YouAppName/AACoreData.xcdatamodeld
If you want to create different name of data model, then you have to specify it by using singleton property in AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
AACoreData.shared.dataModel = "ExampleDataModel"
return true
}
##Create new entity object
let record = instance.getNewObject(entityName: .myEntity) as! ExampleEntity
##Save your changes
instance.saveContext()
##Fetch your records
You can fetch your objects easily. Note that parameters 'predicate' and 'sortDescriptors' are optional
instance.fetchRecords(entityName: #ENTITY#, predicate: #PREDICATE#, sortDescriptors: #SORTDECRIPTORS#, completion: #COMPLETION BLOCK#)
You can easily check the object's existence or fetch the required persist data
instance.fetchRecords(entityName: .myEntity, sortDescriptors: [sorter]) { (results) in
if let result = results {
// MARK:- Record(s) found
// You can fetch using loop
}
else {
// MARK:- No Record found
// You can insert new record to avoid redundancy
}
}
##Delete a object
You can easily delete an object using shared instance.
instance.deleteRecord(record)
Tip: Don't forget to save changes after update persisted data using
instance.saveContext()
##Delete all objects
You can easily remove all the records from an entity using shared instance
instance.deleteAllRecords(entity: .Example)
#Contributions & License
AACoreData
is available under the MIT license. See the LICENSE file for more info.
Pull requests are welcome! The best contributions will consist of substitutions or configurations for classes/methods known to block the main thread during a typical app lifecycle.
I would love to know if you are using AACoreData
in your app, send an email to Engr. Ahsan Ali