Giter VIP home page Giter VIP logo

coredatastorage's Introduction

CoreData Storage

Tiny CoreData helper class and test utils.

How to use?

Create storage object:

let storage = CoreDataStorage(container: NSPersistentContainer(name: "Model"))

It provides two managed object contexts, Main/UI and background:

storage.mainContext // Automatically merges changes from parent context
storage.privateContext // Created once as new background context

Perform block on background context and save:

let cancellable = storage
    .performInBackgroundAndSave { context in
        // work with background context
    }
    .receive(on: DispatchQueue.main)
    .sink(
        receiveCompletion: { completion in
        // process completion on main queue
    },
        receiveValue: { value in
        //
    })

Fetch items in given context type:

let cancellable = storage
    .fetch(Item.fetchItems(), inContext: .background)
    .receive(on: DispatchQueue.main)
    .sink(
        receiveCompletion: { completion in
        // process completion on main queue
    },
        receiveValue: { value in
            let items: [Item] = value
    })

How to test?

Use CoreDataModel to build ManagedObjectModel which you can provide to NSPersistentContainer:

private static let managedObjectModel = CoreDataModel {
    Entity(name: "Item", className: Item.self) {
        Property(name: "id", type: .UUIDAttributeType)
        Property(name: "name", type: .stringAttributeType, isOptional: true)
    }
}.build()

Model should be created once. managedObjectModel builds model for the managed object:

private final class Item: NSManagedObject {
    @NSManaged var id: UUID
    @NSManaged var name: String?
}

Create in memory persistent store:

let container = NSPersistentContainer(name: "Temp", managedObjectModel: managedObjectModel)
let description = NSPersistentStoreDescription()
description.type = NSInMemoryStoreType
description.shouldAddStoreAsynchronously = false
container.persistentStoreDescriptions = [description]

How to add it to Xcode project?

  1. In Xcode select File ⭢ Swift Packages ⭢ Add Package Dependency...
  2. Copy-paste repository URL: https://github.com/avdyushin/CoreDataStorage
  3. Hit Next two times, under Add to Target select your build target.
  4. Hit Finish

coredatastorage's People

Contributors

avdyushin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

isapozhnik

coredatastorage's Issues

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.