项目作者: Dilip-Parmar

项目描述 :
A simple library with core data stack and helper functions (Core Data Wrapper)
高级语言: Swift
项目地址: git://github.com/Dilip-Parmar/CoreDataWrapper.git
创建时间: 2019-07-24T18:39:25Z
项目社区:https://github.com/Dilip-Parmar/CoreDataWrapper

开源协议:MIT License

下载


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:

  1. 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:

  1. 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.

  1. https://github.com/Dilip-Parmar/CoreDataWrapper

How to use

Initialization

  1. let bundle = Bundle(identifier: "com.myBundleId")
  2. let coreDataWrapper = CoreDataWrapper.init(modelFileName: "Model", bundle: bundle, storeType: StoreType.sqlite)
  3. coreDataWrapper.loadStore(completionBlock: { (isSuccess, error) in
  4. })

Main context synchronous operations

Add synchronous operation

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

Add with properties synchronous operation

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

Fetch synchronous operation

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

Fetch all entities synchronous operation

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

Delete synchronous operation

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

Delete all entities synchronous operation

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

Update synchronous operation

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

Update all entities synchronous operation

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

Count synchronous operation

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

Fetch properties synchronously

  1. let predicate = NSPredicate.init(format: "fname == ", argumentArray: ["Dilip"])
  2. let sortBy = ["fname" : true]
  3. let properties = coreDataWrapper.fetchPropertiesOf(type: Person.self, propertiesToFetch: ["fname, "lname"...], predicate: predicate, sortBy: sortBy, needDistinctResults: true)

Math operation synchronously

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

Main context asynchronous operations

Add asynchronous operation

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

Add with properties asynchronous operation

  1. coreDataWrapper.addAsyncOf(type: Person.self, properties: ["fname" : "Dilip", ...], shouldSave: true, completion: {
  2. (person) in
  3. }, completionOnMainThread: false)

Fetch asynchronous operation

  1. let person = coreDataWrapper.fetchAsyncBy(objectId: person.objectID, completion: {
  2. (person) in
  3. }, completionOnMainThread: false)

Fetch all entities asynchronous operation

  1. let predicate = NSPredicate.init(format: "age >= ", argumentArray: ["30"])
  2. let sortBy = ["fname" : true]
  3. let fetchedEntities = coreDataWrapper.fetchAllAsyncOf(type: Person.self, predicate: predicate, sortBy: sortBy, completion: {
  4. (persons) in
  5. }, completionOnMainThread: false))

Delete asynchronous operation

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

Delete all entities asynchronous operation

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

Update asynchronous operation

  1. coreDataWrapper.updateAsyncBy(objectId: person.objectID, properties: ["fname" : "Dilip", ...], shouldSave: true, completion: {
  2. }, completionOnMainThread: false)

Update all entities asynchronous operation

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

Count asynchronous operation

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

Fetch properties asynchronously

  1. let predicate = NSPredicate.init(format: "fname == ", argumentArray: ["Dilip"])
  2. let sortBy = ["fname" : true]
  3. let properties = coreDataWrapper.fetchPropertiesOf(type: Person.self, propertiesToFetch: ["fname, "lname"...], predicate: predicate, sortBy: sortBy, needDistinctResults: true, completion: {
  4. (properties) in
  5. }, completionOnMainThread: false)

Math operation asynchronously

  1. let predicate = NSPredicate.init(format: "age >= ", argumentArray: ["30"])
  2. coreDataWrapper.performOperationAsync(operation: .sum, type: Person.self, propertyName: "age", predicate: predicate, completion: {
  3. (result) in
  4. }, 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.

  1. let newBgContext = coreDataWrapper.newBgContext
  2. coreDataWrapper.addAsyncOf(type: Person.self, context: newBgContext, completion: {
  3. (person) in
  4. })

Save main context

  1. coreDataWrapper.saveMainContext(isSync: false, completion: {
  2. })

Save background context

  1. coreDataWrapper.saveBGContext(context: bgContext, isSync: false, completion: {
  2. })

Author

Dilip Parmar

License

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