diff options
author | Vitaly Takmazov | 2018-10-16 23:37:26 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-10-16 23:37:26 +0300 |
commit | 0c94a6095e42fc5f14f521699601ada6895047aa (patch) | |
tree | c69f422ef2947499a8156117ba0ad48764b862cf /Juick/CoreDataStack.m | |
parent | b81b10a8d8e7f077eb5370e6c07b123de56cea8d (diff) |
Project cleanup
Diffstat (limited to 'Juick/CoreDataStack.m')
-rw-r--r-- | Juick/CoreDataStack.m | 133 |
1 files changed, 0 insertions, 133 deletions
diff --git a/Juick/CoreDataStack.m b/Juick/CoreDataStack.m deleted file mode 100644 index 6122731..0000000 --- a/Juick/CoreDataStack.m +++ /dev/null @@ -1,133 +0,0 @@ -// -// 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 |