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 in testing enviroments
- No singletons
- SQLite and InMemory support out of the box
- Easy database drop method
- Swift
- Objective-C support
- Free
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"];
It is recommended that DATAStack gets persisted when these two methods are called in your AppDelegate
.
Swift
func applicationDidEnterBackground(application: UIApplication) {
self.dataStack.persistWithCompletion(nil)
}
func applicationWillTerminate(application: UIApplication) {
self.dataStack.persistWithCompletion(nil)
}
Objective-C
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self.dataStack persistWithCompletion:nil];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[self.dataStack persistWithCompletion:nil];
}
Getting access to the NSManagedObjectContext attached to the main thread is as simple as using the mainContext
property.
self.dataStack.mainContext
You can easily create a new background NSManagedObjectContext for data processing. This block is completely asynchronous and will be run on a background thread.
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 autocompletion the backgroundContext
parameter name doesn't get included. Make sure to add it.
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 drop];
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.)
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.
DATAStack is available through CocoaPods. To install it, simply add the following line to your Podfile:
use_frameworks!
pod 'DATAStack'
If something looks stupid, please create a friendly and constructive issue, getting your feedback would be awesome.
Have a great day.
Elvis Nuñez, @3lvis
DATAStack is available under the MIT license. See the LICENSE file for more info.