Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Various fixes.
Readme updated.
Bug fix.
Persistent Description introduced.
  • Loading branch information
lolgear committed Mar 12, 2017
1 parent 8acfc4c commit 324fb4b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Incremental Store/EncryptedStore.h
Expand Up @@ -91,9 +91,11 @@ typedef NS_ENUM(NSInteger, EncryptedStoreError)
@interface EncryptedStore (Initialization)
+ (NSPersistentStoreCoordinator *)makeStoreWithOptions:(NSDictionary *)options managedObjectModel:(NSManagedObjectModel *)objModel error:(NSError * __autoreleasing*)error;
+ (NSPersistentStoreCoordinator *)coordinator:(NSPersistentStoreCoordinator *)coordinator byAddingStoreAtURL:(NSURL *)url configuration:(NSString *)configuration options:(NSDictionary *)options error:(NSError * __autoreleasing*)error;
+ (NSPersistentStoreDescription *)makeDescriptionWithOptions:(NSDictionary *)options configuration:(NSString *)configuration error:(NSError * __autoreleasing*)error API_AVAILABLE(macosx(10.12),ios(10.0),tvos(10.0),watchos(3.0));
@end

@interface EncryptedStore (Configuration)
//alias to options.
@property (copy, nonatomic, readonly) NSDictionary *configurationOptions;
@property (strong, nonatomic, readonly) EncryptedStoreFileManager *fileManager;
@end
Expand Down
33 changes: 22 additions & 11 deletions Incremental Store/EncryptedStore.m
Expand Up @@ -157,8 +157,10 @@ - (void)setupDatabaseWithOptions:(NSDictionary *)options error:(NSError *__autor
backup = NO;
}


if (databaseURL)
{
self.configuration.databaseURL = databaseURL;
NSMutableDictionary *fileAttributes = [options mutableCopy];
[fileAttributes removeObjectsForKeys:@[EncryptedStorePassphraseKey, EncryptedStoreDatabaseLocation]];
[self setAttributes:fileAttributes ofItemAtURL:databaseURL error:error];
Expand Down Expand Up @@ -211,7 +213,10 @@ + (NSString *)optionFileManager {
@end

@implementation EncryptedStore (Configuration)
@dynamic configurationOptions;
- (NSDictionary *)configurationOptions {
return self.options;
}

- (EncryptedStoreFileManager *)fileManager {
EncryptedStoreFileManager *fileManager = [self.configurationOptions objectForKey:self.class.optionFileManager];

Expand All @@ -223,17 +228,13 @@ - (EncryptedStoreFileManager *)fileManager {
}
@end

@interface EncryptedStore ()
@property (copy, nonatomic, readwrite) NSDictionary *configurationOptions;
@end

@implementation EncryptedStore (Initialization)
+ (NSPersistentStoreCoordinator *)makeStoreWithOptions:(NSDictionary *)options managedObjectModel:(NSManagedObjectModel *)objModel error:(NSError *__autoreleasing *)error
{
NSPersistentStoreCoordinator * persistentCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:objModel];

// NSString* appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
EncryptedStoreFileManager *fileManager = [options objectForKey:self.class.optionFileManager] ?: [EncryptedStoreFileManager defaultManager];
EncryptedStoreFileManager *fileManager = [options objectForKey:self.optionFileManager] ?: [EncryptedStoreFileManager defaultManager];
[fileManager setupDatabaseWithOptions:options error:error];
if (error) {
NSError *theError = *error;
Expand Down Expand Up @@ -291,14 +292,24 @@ + (NSPersistentStoreCoordinator *)coordinator:(NSPersistentStoreCoordinator *)co
return nil;
}

EncryptedStore *store = [coordinator addPersistentStoreWithType:EncryptedStoreType configuration:configuration URL:url options:options error:error];

if (store) {
store.configurationOptions = options;
}
[coordinator addPersistentStoreWithType:EncryptedStoreType configuration:configuration URL:url options:options error:error];

return coordinator;
}

+ (NSPersistentStoreDescription *)makeDescriptionWithOptions:(NSDictionary *)options configuration:(NSString *)configuration error:(NSError * __autoreleasing*)error {
NSPersistentStoreDescription *description = [NSPersistentStoreDescription new];
EncryptedStoreFileManager *fileManager = [options objectForKey:self.class.optionFileManager] ?: [EncryptedStoreFileManager defaultManager];
[fileManager setupDatabaseWithOptions:options error:error];
description.type = self.optionType;
description.URL = fileManager.databaseURL;
description.configuration = configuration;
for (NSString *key in options) {
[description setOption:options[key] forKey:key];
}
return description;
}

@end

@implementation EncryptedStore {
Expand Down
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -79,6 +79,27 @@ But you can configure file extension, file name and file url in `EncryptedStoreF
In general, this functionality is not needed.
It is a part of setup core data stack process.

## Configure persistentContainer
`NSPersistentContainer` uses NSPersistentStoreDescriptions to configure stores.

```
NSManagedObjectModel *model = [NSManagedObjectModel new];
NSPersistentContainer *container = [[NSPersistentContainer alloc] initWithName:@"abc" managedObjectModel:model];
NSDictionary *options = @{
self.optionPassphraseKey : @"123",
self.optionFileManager : [EncryptedStoreFileManager defaultManager]
};
NSPersistentStoreDescription *description = [self makeDescriptionWithOptions:options configuration:nil error:nil];
container.persistentStoreDescriptions = @[description];
[container loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *description, NSError * error) {
if (error) {
NSLog(@"error! %@", error);
}
}];
```

But if you wish:

```
Expand Down

0 comments on commit 324fb4b

Please sign in to comment.