summaryrefslogtreecommitdiff
path: root/Juick/CoreDataStack.m
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-10-15 23:47:11 +0300
committerGravatar Vitaly Takmazov2017-10-15 23:47:11 +0300
commit8ae952ec8df2a713afeaef5960a88888e050c6fc (patch)
treeb7b09deed573336e5d274eed790c30ba57e4a910 /Juick/CoreDataStack.m
parent6499148cb3f62e20534b62c181125ebeacfa3438 (diff)
WIP
Diffstat (limited to 'Juick/CoreDataStack.m')
-rw-r--r--Juick/CoreDataStack.m133
1 files changed, 133 insertions, 0 deletions
diff --git a/Juick/CoreDataStack.m b/Juick/CoreDataStack.m
new file mode 100644
index 0000000..6122731
--- /dev/null
+++ b/Juick/CoreDataStack.m
@@ -0,0 +1,133 @@
+//
+// CoreDataStack.m
+// Juick
+//
+// Created by Vitaly Takmazov on 15/10/2017.
+// Copyright © 2017 com.juick. All rights reserved.
+//
+
+#import "CoreDataStack.h"
+
+@interface CoreDataStack ()
+@property (nonatomic, getter=isStoreLoaded) BOOL storeLoaded;
+@property (nonatomic, strong) NSPersistentContainer *persistentContainer;
+@end
+
+@implementation CoreDataStack
+
++ (NSURL *)defaultDirectoryURL {
+ return [NSPersistentContainer defaultDirectoryURL];
+}
+
+- (instancetype)initWithName:(NSString *)name {
+
+ NSManagedObjectModel *mom = [NSManagedObjectModel mergedModelFromBundles:nil];
+ if (mom == nil) return nil;
+
+ self = [super init];
+ if (self) {
+ _storeLoaded = NO;
+ _shouldAddStoreAsynchronously = YES;
+ _shouldMigrateStoreAutomatically = YES;
+ _shouldInferMappingModelAutomatically = YES;
+ _readOnly = NO;
+ _persistentContainer = [NSPersistentContainer persistentContainerWithName:name managedObjectModel:mom];
+ _persistentContainer.viewContext.automaticallyMergesChangesFromParent = YES;
+ }
+ return self;
+}
+
+- (void)loadStoreWithCompletionHandler:(void(^)(NSError *))handler {
+
+ [self loadStoreAtURL:self.storeURL withCompletionHandler:handler];
+}
+
+- (void)loadStoreAtURL:(NSURL *)storeURL withCompletionHandler:(void(^)(NSError *))handler {
+
+ if (!self.persistentContainer) {
+ NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSNotFound userInfo:nil];
+ if (handler) {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ handler(error);
+ });
+ }
+ return;
+ }
+
+ self.persistentContainer.persistentStoreDescriptions = @[[self storeDescriptionWithURL:storeURL]];
+ [self.persistentContainer loadPersistentStoresWithCompletionHandler:
+ ^(NSPersistentStoreDescription *storeDescription, NSError *error) {
+ if (error == nil) {
+ self.storeLoaded = YES;
+ }
+ if (handler) {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ handler(error);
+ });
+ }
+ }];
+}
+
+- (BOOL)persistentStoreExistsAtURL:(NSURL *)storeURL {
+
+ if (storeURL.isFileURL &&
+ [NSFileManager.defaultManager fileExistsAtPath:storeURL.path]) {
+ return YES;
+ }
+ return NO;
+}
+
+- (NSURL *)storeURL {
+ NSArray *descriptions = self.persistentContainer.persistentStoreDescriptions;
+ NSPersistentStoreDescription *description = [descriptions firstObject];
+ return description.URL;
+}
+
+- (BOOL)destroyPersistentStoreAtURL:(NSURL *)storeURL {
+
+ NSError *error = nil;
+ BOOL result = [self.persistentContainer.persistentStoreCoordinator destroyPersistentStoreAtURL:storeURL withType:NSSQLiteStoreType options:nil error:&error];
+ return result;
+}
+
+- (BOOL)replacePersistentStoreAtURL:(NSURL *)destinationURL withPersistentStoreFromURL:(NSURL *)sourceURL {
+
+ NSError *error = nil;
+ BOOL result = [self.persistentContainer.persistentStoreCoordinator replacePersistentStoreAtURL:destinationURL destinationOptions:nil withPersistentStoreFromURL:sourceURL sourceOptions:nil storeType:NSSQLiteStoreType error:&error];
+ return result;
+}
+
+- (NSManagedObjectContext *)viewContext {
+
+ return self.persistentContainer.viewContext;
+}
+
+- (NSManagedObjectContext *)newPrivateContext {
+
+ return [self.persistentContainer newBackgroundContext];
+}
+
+- (void)performBackgroundTask:(void(^)(NSManagedObjectContext *))block {
+
+ [self.persistentContainer performBackgroundTask:block];
+}
+
+- (NSManagedObjectID *)managedObjectIDForURIRepresentation:(NSURL *)url {
+ return [self.persistentContainer.persistentStoreCoordinator managedObjectIDForURIRepresentation:url];
+}
+
+#pragma mark -
+#pragma mark === Private methods ===
+#pragma mark -
+
+- (NSPersistentStoreDescription *)storeDescriptionWithURL:(NSURL *)URL {
+
+ NSPersistentStoreDescription *description = [NSPersistentStoreDescription persistentStoreDescriptionWithURL:URL];
+ description.shouldAddStoreAsynchronously = self.shouldAddStoreAsynchronously;
+ description.shouldMigrateStoreAutomatically = self.shouldMigrateStoreAutomatically;
+ description.shouldInferMappingModelAutomatically = self.shouldInferMappingModelAutomatically;
+ description.readOnly = self.isReadOnly;
+ return description;
+}
+
+@end