Giter Club home page Giter Club logo

coredatawrapper's Introduction

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)
coreDataWrapper.loadStore(completionBlock: { (isSuccess, error) in

})

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.

coredatawrapper's People

Contributors

dilip-parmar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.