All Projects → 3lvis → Datastack

3lvis / Datastack

Licence: other
100% Swift Simple Boilerplate Free Core Data Stack. NSPersistentContainer

Programming Languages

swift
15916 projects

Labels

Projects that are alternatives of or similar to Datastack

Shob
SwiftUI + CoreData & iCloud AutoSync Project
Stars: ✭ 90 (-57.55%)
Mutual labels:  coredata
Coredataplaygrounds
Exploring Core Data through Swift playgrounds
Stars: ✭ 139 (-34.43%)
Mutual labels:  coredata
Velik
Ride tracking app
Stars: ✭ 168 (-20.75%)
Mutual labels:  coredata
Bgfmdb
BGFMDB让数据的增删改查分别只需要一行代码即可,就是这么简单任性,本库几乎支持存储ios所有基本的自带数据类型.
Stars: ✭ 1,344 (+533.96%)
Mutual labels:  coredata
Widgetexamples
A demo project showcasing different types of Widgets created with SwiftUI and WidgetKit.
Stars: ✭ 125 (-41.04%)
Mutual labels:  coredata
Justpersist
JustPersist is the easiest and safest way to do persistence on iOS with Core Data support out of the box. It also allows you to migrate to any other persistence framework with minimal effort.
Stars: ✭ 157 (-25.94%)
Mutual labels:  coredata
Jotify
Sticky notes reimagined - written in Swift
Stars: ✭ 79 (-62.74%)
Mutual labels:  coredata
Swiftdb
A modern database abstraction layer, batteries included.
Stars: ✭ 183 (-13.68%)
Mutual labels:  coredata
Swiftui Core Data Test
Sample program to demonstrate how CoreData can be used with SwiftUI.
Stars: ✭ 135 (-36.32%)
Mutual labels:  coredata
Clean Architecture Swiftui
SwiftUI sample app using Clean Architecture. Examples of working with CoreData persistence, networking, dependency injection, unit testing, and more.
Stars: ✭ 2,925 (+1279.72%)
Mutual labels:  coredata
Ios Hierarchy Viewer
iOS Hierarchy viewer - View and Coredata debugging made easy
Stars: ✭ 1,345 (+534.43%)
Mutual labels:  coredata
Dictionaryplusplus
Dictionary++ is a simple interface to iOS's system dictionary.
Stars: ✭ 112 (-47.17%)
Mutual labels:  coredata
Coredata Crud Swift 5.0 Example
Swift 5.0 Example project that exposes the usage of Core Data to create Entities and to persist to a SQLite Datastore
Stars: ✭ 157 (-25.94%)
Mutual labels:  coredata
Hardchoice
有时候作抉择真的很痛苦,Swift写的生活类APP
Stars: ✭ 90 (-57.55%)
Mutual labels:  coredata
Coredatabestpractices
Best Practices in Core Data explained within a demo application
Stars: ✭ 171 (-19.34%)
Mutual labels:  coredata
Tothepenny
A budget tracker app for iOS
Stars: ✭ 82 (-61.32%)
Mutual labels:  coredata
Cloudcore
Framework that enables syncing between iCloud (CloudKit) and Core Data
Stars: ✭ 146 (-31.13%)
Mutual labels:  coredata
Hardcoredata
CoreData stack and controller that will never block UI thread
Stars: ✭ 207 (-2.36%)
Mutual labels:  coredata
Core Data Editor
Core Data Editor lets you easily view, edit and analyze applications‘ data. Core Data Editor is compatible with Mac and iOS applications and supports XML, SQLite and binary stores, visualizes all relationships and is able to edit the data and generate Objective-C code for the data model.
Stars: ✭ 2,106 (+893.4%)
Mutual labels:  coredata
Upcomingmovies
Movies app written in Swift 5 using the TMDb API and demonstrating Clean Architecture, Dependency Injection, MVVM and Coordinators.
Stars: ✭ 160 (-24.53%)
Mutual labels:  coredata

DATAStack

DATAStack helps you to alleviate the Core Data boilerplate. Now you can go to your AppDelegate remove all the Core Data related code and replace it with an instance of DATAStack (ObjC, Swift).

  • Easier thread safety
  • Runs synchronously when using unit tests
  • No singletons
  • SQLite and InMemory support out of the box
  • Easy database drop method
  • Shines with Swift
  • Compatible with Objective-C
  • Free

Table of Contents

Running the demos

  • Clone the repository
  • Open the Demo.xcodeproj
  • Enjoy!

Initialization

You can easily initialize a new instance of DATAStack with just your Core Data Model name (xcdatamodel).

Swift

let dataStack = DATAStack(modelName:"MyAppModel")

Objective-C

DATAStack *dataStack = [[DATAStack alloc] initWithModelName:@"MyAppModel"];

There are plenty of other ways to intialize a DATAStack:

  • Using a custom store type.
let dataStack = DATAStack(modelName:"MyAppModel", storeType: .InMemory)
  • Using another bundle and a store type, let's say your test bundle and .InMemory store type, perfect for running unit tests.
let dataStack = DATAStack(modelName: "Model", bundle: NSBundle(forClass: Tests.self), storeType: .InMemory)
  • Using a different name for your .sqlite file than your model name, like CustomStoreName.sqlite.
let dataStack = DATAStack(modelName: "Model", bundle: NSBundle.mainBundle(), storeType: .SQLite, storeName: "CustomStoreName")
  • Providing a diferent container url, by default we'll use the documents folder, most apps do this, but if you want to share your sqlite file between your main app and your app extension you'll want this.
let dataStack = DATAStack(modelName: "Model", bundle: NSBundle.mainBundle(), storeType: .SQLite, storeName: "CustomStoreName", containerURL: sharedURL)

Main Thread NSManagedObjectContext

Getting access to the NSManagedObjectContext attached to the main thread is as simple as using the mainContext property.

self.dataStack.mainContext

or

self.dataStack.viewContext

Background Thread NSManagedObjectContext

You can easily create a new background NSManagedObjectContext for data processing. This block is completely asynchronous and will be run on a background thread.

To be compatible with NSPersistentContainer you can also use performBackgroundTask instead of performInNewBackgroundContext.

Swift

func createUser() {
    self.dataStack.performInNewBackgroundContext { backgroundContext in
        let entity = NSEntityDescription.entityForName("User", inManagedObjectContext: backgroundContext)!
        let object = NSManagedObject(entity: entity, insertIntoManagedObjectContext: backgroundContext)
        object.setValue("Background", forKey: "name")
        object.setValue(NSDate(), forKey: "createdDate")
        try! backgroundContext.save()
    }
}

Objective-C

- (void)createUser {
    [self.dataStack performInNewBackgroundContext:^(NSManagedObjectContext * _Nonnull backgroundContext) {
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:backgroundContext];
        NSManagedObject *object = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:backgroundContext];
        [object setValue:@"Background" forKey:@"name"];
        [object setValue:[NSDate date] forKey:@"createdDate"];
        [backgroundContext save:nil];
    }];
}

When using Xcode's Objective-C autocompletion the backgroundContext parameter name doesn't get included. Make sure to add it.

Clean up

Deleting the .sqlite file and resetting the state of your DATAStack is as simple as just calling drop.

Swift

self.dataStack.drop()

Objective-C

[self.dataStack forceDrop];

Testing

DATAStack is optimized for unit testing and it runs synchronously in testing enviroments. Hopefully you'll have to use less XCTestExpectations now.

You can create a stack that uses in memory store like this if your Core Data model is located in your app bundle:

Swift

let dataStack = DATAStack(modelName: "MyAppModel", bundle: NSBundle.mainBundle(), storeType: .InMemory)

Objective-C

DATAStack *dataStack = [[DATAStack alloc] initWithModelName:@"MyAppModel"
                                                     bundle:[NSBundle mainBundle]
                                                  storeType:DATAStackStoreTypeInMemory];

If your Core Data model is located in your test bundle:

Swift

let dataStack = DATAStack(modelName: "MyAppModel", bundle: NSBundle(forClass: Tests.self), storeType: .InMemory)

Objective-C

DATAStack *dataStack = [[DATAStack alloc] initWithModelName:@"MyAppModel"
                                                     bundle:[NSBundle bundleForClass:[self class]]
                                                  storeType:DATAStackStoreTypeInMemory];

(Hint: Maybe you haven't found the best way to use NSFetchedResultsController, well here it is.)

Migrations

If DATAStack has troubles creating your persistent coordinator because a migration wasn't properly handled it will destroy your data and create a new sqlite file. The normal Core Data behaviour for this is making your app crash on start. This is not fun.

Installation

DATAStack is available through CocoaPods. To install it, simply add the following line to your Podfile:

use_frameworks!

pod 'DATAStack', '~> 6'

DATAStack is also available through Carthage. To install it, simply add the following line to your Cartfile:

github "SyncDB/DATAStack" ~> 6.0

Be Awesome

If something looks stupid, please create a friendly and constructive issue, getting your feedback would be awesome.

Have a great day.

Author

Elvis Nuñez, @3lvis

License

DATAStack is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].