Swiftpack.co - Package - Dilip-Parmar/CoreDataWrapper

CoreDataWrapper

A simple core data wrapper with synchronous and asynchronous helper functions. It supports SQLite, Binary and In-Memory configuration.

Table of Contents

Features

  • 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

Requirements

  • iOS 12.0+ / macOS 10.14+ / tvOS 12.0+ / watchOS 5.0+
  • Xcode 10.2+
  • Swift 5+

Installation

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

How to use

Initialization

let bundle = Bundle(identifier: "com.myBundleId")
let coreDataWrapper = CoreDataWrapper.init(modelFileName: "Model", bundle: bundle, storeType: StoreType.sqlite)

Main context synchronous operations

Add synchronous operation

let person = coreDataWrapper.addOf(type: Person.self)

Add with properties synchronous operation

let person = coreDataWrapper.addOf(type: Person.self, properties: ["fname" : "Dilip", ...], shouldSave: true)

Fetch synchronous operation

let existingPerson = coreDataWrapper.fetchBy(objectId: person.objectID)

Fetch all entities synchronous operation

let predicate = NSPredicate.init(format: "fname == ", argumentArray: ["Dilip"])
let sortBy = ["fname" : true]
let allFetchedEntities = coreDataWrapper.fetchAllOf(type: Person.self, predicate: predicate, sortBy: sortBy)

Delete synchronous operation

coreDataWrapper.deleteBy(objectId: person.objectID, shouldSave: true)

Delete all entities synchronous operation

let predicate = NSPredicate.init(format: "fname == ", argumentArray: ["Dilip"])
coreDataWrapper.deleteAllOf(type: Person.self, predicate: predicate, shouldSave: true)

Update synchronous operation

coreDataWrapper.updateBy(objectId: person.objectID, properties: ["fname" : "Dilip", ...], shouldSave: true)

Update all entities synchronous operation

let predicate = NSPredicate.init(format: "fname == ", argumentArray: ["Dilip"])
coreDataWrapper.updateAllOf(type: Person.self, properties: ["fname" : "Dilip", ...], predicate: predicate, shouldSave: true)

Count synchronous operation

let predicate = NSPredicate.init(format: "fname == ", argumentArray: ["Dilip"])
let count = coreDataWrapper.countOf(type: Person.self, predicate: predicate)

Fetch properties synchronously

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)

Math operation synchronously

let predicate = NSPredicate.init(format: "age >= ", argumentArray: ["30"])
let properties = coreDataWrapper.performOperation(operation: .sum, type: Person.self, propertyName: "age", predicate: predicate)

Main context asynchronous operations

Add asynchronous operation

coreDataWrapper.addAsyncOf(type: Person.self, completion: {
    (person) in 
})

Add with properties asynchronous operation

coreDataWrapper.addAsyncOf(type: Person.self, properties: ["fname" : "Dilip", ...], shouldSave: true, completion: {
    (person) in 

}, completionOnMainThread: false)

Fetch asynchronous operation

let person = coreDataWrapper.fetchAsyncBy(objectId: person.objectID, completion: {
    (person) in 

}, completionOnMainThread: false)

Fetch all entities asynchronous operation

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))

Delete asynchronous operation

coreDataWrapper.deleteAsyncBy(objectId: person.objectID, shouldSave: true, completion: {
    
}, completionOnMainThread: false)

Delete all entities asynchronous operation

let predicate = NSPredicate.init(format: "age >= ", argumentArray: ["30"])
coreDataWrapper.deleteAllAsyncOf(type: Person.self, predicate: predicate, shouldSave: true, completion: {

}, completionOnMainThread: false)

Update asynchronous operation

coreDataWrapper.updateAsyncBy(objectId: person.objectID, properties: ["fname" : "Dilip", ...], shouldSave: true, completion: {

}, completionOnMainThread: false)

Update all entities asynchronous operation

let predicate = NSPredicate.init(format: "age >= ", argumentArray: ["30"])
coreDataWrapper.updateAllAsyncOf(type: Person.self, properties: ["fname" : "Dilip", ...], predicate: predicate, shouldSave: true, completion: {

}, completionOnMainThread: false)

Count asynchronous operation

let predicate = NSPredicate.init(format: "age >= ", argumentArray: ["30"])
coreDataWrapper.countAsyncOf(type: Person.self, predicate: predicate, completion: {
    (count) in
}, completionOnMainThread: false)

Fetch properties asynchronously

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)

Math operation asynchronously

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

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 
})

Save main context

coreDataWrapper.saveMainContext(isSync: false, completion: {

})

Save background context

coreDataWrapper.saveBGContext(context: bgContext, isSync: false, completion: {

})

Author

Dilip Parmar

License

CoreDataWrapper is released under the MIT license. See LICENSE for details.

Github

link
Stars: 1
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

1.0.0 - Jul 24, 2019