ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - mrdekk/DataKernel: Simple CoreData wrapper to ease operations
Simple CoreData wrapper to ease operations. Contribute to mrdekk/DataKernel development by creating an account on GitHub.
Visit Site

GitHub - mrdekk/DataKernel: Simple CoreData wrapper to ease operations

GitHub - mrdekk/DataKernel: Simple CoreData wrapper to ease operations

DataKernel

Language: Swift Language: Swift Build Status Carthage compatible CocoaPods compatible

What is DataKernel?

DataKernel is a minimalistic wrapper around CoreData stack to ease persistence operations. It is heavily inspired by SugarRecord but have no external dependencies (except Cocoa of course) and with some refinements. It is covered with unit tests.

If you have any propositions please file issue. If you need usage examples - see unit test, it is very straightforward

Features

  • Swift (tested in XCode 9.1)
  • Protocols based design
  • Fully tested
  • Actively supported
  • Rich set of operations (but I think something may be missed)
  • No iCloud yet (but planned)

Note

  • context.wipe uses NSBatchDeleteRequest on iOS 9 and OSX 10.11, it gives big performance improvements, but can cause problems if you use patterns like wipe all and create new, because it acts directly on persistent store (see Apple WWDC 2015 video or slides)

Setup

CocoaPods

  1. Install CocoaPods. You can do it with gem install cocoapods
  2. Edit your Podfile file and add the following line pod 'DataKernel' (you have to use use_frameworks!, because this is a Swift pod)
  3. Update your pods with the command pod install
  4. Open the project from the generated workspace (.xcworkspace file).

Note: You can also test the last commits by specifying it directly in the Podfile line

Carthage

  1. Install Carthage on your computer using brew install carthage
  2. Edit your Cartfile file adding the following line github "mrdekk/DataKernel"
  3. Update and build frameworks with carthage update
  4. Add generated frameworks to your app main target following the steps here
  5. Link your target with CoreData library (from Build Phases)

How to use

Creating your Storage

A storage is a general wrapper around CoreData PersistentStoreCoordination and PersistentStores. The first step you need to get started is to create CoreDataLocalStorage.

let store = StoreRef.Named("test1")
let bundle = NSBundle(forClass: self.classForCoder)
let model = ModelRef.Merged([bundle])
let storage = try! CoreDataLocalStorage(store: store!, model: model!, migration: true)

Contexts

Storage offer access to uiContext (NSManagedObjectContext on main thread to GET data from storage). All modification operations on CoreData entities should be performed with perform operation on storage.

Fetching data

let cars: [Cars] = try! storage.uiContext.fetch(Request<Car>().filtered("mark", equalTo: "Honda"))
let cars: [Cars] = try! storage.uiContext.fetch(Request<Car>())
let cars: [Cars] = try! storage.uiContext.fetch(Request<Car>().sorted("model", ascending: true))

let predicate = NSPredicate(format: "model == %@", "CRZ")
let crz: Car? = try! storage.uiContext.fetch(Request<Car>(predicate: predicate)).first

Remove/Insert/Update operations

All modification operations should be performed under perform operation due to it handles all core data context/threading things internally and you can safely use modification operations under them.

save operation perform recursive save with nesting context on core data stack. So, if you change entities that already loaded in uiContext, they will be updated.

Note the first parameter ephemeral on perform function. If it is true, new context for this operation will be created, and then after save succeeded it will be removed. Creation of contexts in CoreData is rather cheap operations, so don't worry. If you don't need to save data in ephemeral context, just don't call save. If you set ephemeral = false then a pre-created special save context will be used for all ephemeral = false operations.

do {
  storage.perform(true) { (context, save) throws -> Void in
    // do you unit of work here
    save()
  }
}
catch {
  // There was an error in the operation
}
Creating a model

You can use the create() for initializing and inserting in the context in the same operation:

do {
  storage.perform(true) { (context, save) throws -> Void in
    let newCar: Car = try! context.create()
    newCar.model = "Honda"
    newCar.mark = "CRZ"
    save()
  }
}
catch {
  // There was an error in the operation
}

Upsert a model

Upsert - update or insert first search the entity in storage, and then if it is not found creates it. Just call the acquire function.

Important: to get this feature work, you should add model property pk and set it to name of the field, that is primary key of this entity. Moreover, this property should be indexed. It is needed to achieve needed level of performance.

do {
  storage.perform(true) { (context, save) throws -> Void in
    let car: Car = try! context.acquire("CRZ")
  }
}
catch {
  // There was an error in the operation
}
Delete a model

In a similar way you can use the remove() method from the context passing the objects you want to remove from the database:

do {
  storage.perform(true) { (context, save) throws -> Void in
    let car: Car? = try! context.fetch(Request<Car>.filtered("model", equalTo: "CRZ")).first
    if let car = car {
      try! context.remove([car])
      save()
    }
  }
}
catch {
  // There was an error in the operation
}

Contributing

Support

If you want to communicate any issue, suggestion or even make a contribution, you have to keep in mind the flow bellow:

  • If you need help, ask your doubt in Stack Overflow using the tag 'datakernel'
  • If you want to ask something in general, use Stack Overflow too.
  • Open an issue either when you have an error to report or a feature request.
  • If you want to contribute, submit a pull request, and remember the rules to follow related with the code style, testing, ...

Code of conduct

This project adheres to the [Open Code of Conduct][code-of-conduct]. By participating, you are expected to honor this code. [code-of-conduct]: http://todogroup.org/opencodeofconduct/#DataKernel/[email protected]

License

The MIT License (MIT)

Copyright (c) <2017>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory