A simple core data wrapper with synchronous and asynchronous helper functions. It supports SQLite, Binary and In-Memory configuration.
- Features
- Requirements
- Installation
- How to use
- Initialization
- Main context synchronous operations
- Add synchronous operation
- Add with properties - synchronous operation
- Fetch synchronous operation
- Fetch all entities synchronous operation
- Delete synchronous operation
- Delete all entities synchronous operation
- Update synchronous operation
- Update all entities synchronous operation
- Count synchronous operation
- Fetch properties synchronously
- Math operation synchronously
- Main context asynchronous operations
- Add asynchronous operation
- Add with properties - asynchronous operation
- Fetch asynchronous operation
- Fetch all entities asynchronous operation
- Delete asynchronous operation
- Delete all entities asynchronous operation
- Update asynchronous operation
- Update all entities asynchronous operation
- Count asynchronous operation
- Fetch properties asynchronously
- Math operation asynchronously
- Background context asynchronous operations
- Save main context
- Save background context
- Author
- License
- Singleton free
- No external dependencies
- Multi-threaded per se
- Multiple instances possbile with different model files
- Supports SQLITE, Binary and In-Memory store types
- Main context synchronous helper functions
- Main context asynchronous helper functions
- Background context asynchronous helper functions
- Free
- iOS 12.0+ / macOS 10.14+ / tvOS 12.0+ / watchOS 5.0+
- Xcode 10.2+
- Swift 5+
CoreDataWrapper is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'CoreDataWrapper', :git => 'https://github.com/Dilip-Parmar/CoreDataWrapper'
CoreDataWrapper is also available through Carthage. To install it, simply add the following line to your Cartfile:
github "Dilip-Parmar/CoreDataWrapper" "1.0.0" //always use latest release version
CoreDataWrapper is also available through Swift Package Manager. To install it, simply enter given URL into "Enter Package Repository URL" search field of Xcode.
https://github.com/Dilip-Parmar/CoreDataWrapper
let bundle = Bundle(identifier: "com.myBundleId")
let coreDataWrapper = CoreDataWrapper.init(modelFileName: "Model", bundle: bundle, storeType: StoreType.sqlite)
coreDataWrapper.loadStore(completionBlock: { (isSuccess, error) in
})
let person = coreDataWrapper.addOf(type: Person.self)
let person = coreDataWrapper.addOf(type: Person.self, properties: ["fname" : "Dilip", ...], shouldSave: true)
let existingPerson = coreDataWrapper.fetchBy(objectId: person.objectID)
let predicate = NSPredicate.init(format: "fname == ", argumentArray: ["Dilip"])
let sortBy = ["fname" : true]
let allFetchedEntities = coreDataWrapper.fetchAllOf(type: Person.self, predicate: predicate, sortBy: sortBy)
coreDataWrapper.deleteBy(objectId: person.objectID, shouldSave: true)
let predicate = NSPredicate.init(format: "fname == ", argumentArray: ["Dilip"])
coreDataWrapper.deleteAllOf(type: Person.self, predicate: predicate, shouldSave: true)
coreDataWrapper.updateBy(objectId: person.objectID, properties: ["fname" : "Dilip", ...], shouldSave: true)
let predicate = NSPredicate.init(format: "fname == ", argumentArray: ["Dilip"])
coreDataWrapper.updateAllOf(type: Person.self, properties: ["fname" : "Dilip", ...], predicate: predicate, shouldSave: true)
let predicate = NSPredicate.init(format: "fname == ", argumentArray: ["Dilip"])
let count = coreDataWrapper.countOf(type: Person.self, predicate: predicate)
let predicate = NSPredicate.init(format: "fname == ", argumentArray: ["Dilip"])
let sortBy = ["fname" : true]
let properties = coreDataWrapper.fetchPropertiesOf(type: Person.self, propertiesToFetch: ["fname, "lname"...], predicate: predicate, sortBy: sortBy, needDistinctResults: true)
let predicate = NSPredicate.init(format: "age >= ", argumentArray: ["30"])
let properties = coreDataWrapper.performOperation(operation: .sum, type: Person.self, propertyName: "age", predicate: predicate)
coreDataWrapper.addAsyncOf(type: Person.self, completion: {
(person) in
})
coreDataWrapper.addAsyncOf(type: Person.self, properties: ["fname" : "Dilip", ...], shouldSave: true, completion: {
(person) in
}, completionOnMainThread: false)
let person = coreDataWrapper.fetchAsyncBy(objectId: person.objectID, completion: {
(person) in
}, completionOnMainThread: false)
let predicate = NSPredicate.init(format: "age >= ", argumentArray: ["30"])
let sortBy = ["fname" : true]
let fetchedEntities = coreDataWrapper.fetchAllAsyncOf(type: Person.self, predicate: predicate, sortBy: sortBy, completion: {
(persons) in
}, completionOnMainThread: false))
coreDataWrapper.deleteAsyncBy(objectId: person.objectID, shouldSave: true, completion: {
}, completionOnMainThread: false)
let predicate = NSPredicate.init(format: "age >= ", argumentArray: ["30"])
coreDataWrapper.deleteAllAsyncOf(type: Person.self, predicate: predicate, shouldSave: true, completion: {
}, completionOnMainThread: false)
coreDataWrapper.updateAsyncBy(objectId: person.objectID, properties: ["fname" : "Dilip", ...], shouldSave: true, completion: {
}, completionOnMainThread: false)
let predicate = NSPredicate.init(format: "age >= ", argumentArray: ["30"])
coreDataWrapper.updateAllAsyncOf(type: Person.self, properties: ["fname" : "Dilip", ...], predicate: predicate, shouldSave: true, completion: {
}, completionOnMainThread: false)
let predicate = NSPredicate.init(format: "age >= ", argumentArray: ["30"])
coreDataWrapper.countAsyncOf(type: Person.self, predicate: predicate, completion: {
(count) in
}, completionOnMainThread: false)
let predicate = NSPredicate.init(format: "fname == ", argumentArray: ["Dilip"])
let sortBy = ["fname" : true]
let properties = coreDataWrapper.fetchPropertiesOf(type: Person.self, propertiesToFetch: ["fname, "lname"...], predicate: predicate, sortBy: sortBy, needDistinctResults: true, completion: {
(properties) in
}, completionOnMainThread: false)
let predicate = NSPredicate.init(format: "age >= ", argumentArray: ["30"])
coreDataWrapper.performOperationAsync(operation: .sum, type: Person.self, propertyName: "age", predicate: predicate, completion: {
(result) in
}, completionOnMainThread: false)
Background context asynchronous operations are same as main context asynchronous operations provided background context is passed to function. eg.
let newBgContext = coreDataWrapper.newBgContext
coreDataWrapper.addAsyncOf(type: Person.self, context: newBgContext, completion: {
(person) in
})
coreDataWrapper.saveMainContext(isSync: false, completion: {
})
coreDataWrapper.saveBGContext(context: bgContext, isSync: false, completion: {
})
CoreDataWrapper is released under the MIT license. See LICENSE for details.