OSDN Git Service

建造履歴などを保存するためマスタとは別のData ModelとData Storeを用意した
authormasakih <masakih@users.sourceforge.jp>
Sat, 22 Mar 2014 12:18:00 +0000 (21:18 +0900)
committermasakih <masakih@users.sourceforge.jp>
Sat, 22 Mar 2014 12:18:00 +0000 (21:18 +0900)
KCD.xcodeproj/project.pbxproj
KCD/HMKenzoHistory.h [new file with mode: 0644]
KCD/HMKenzoHistory.m [new file with mode: 0644]
KCD/HMLocalDataStore.h [new file with mode: 0644]
KCD/HMLocalDataStore.m [new file with mode: 0644]
KCD/LocalData.xcdatamodeld/LocalData.xcdatamodel/contents [new file with mode: 0644]

index 3e733f2..e5a3e85 100644 (file)
@@ -68,6 +68,9 @@
                F4D2CBC818B0555A000EBC59 /* HMKCManagedObject.m in Sources */ = {isa = PBXBuildFile; fileRef = F4D2CBC718B0555A000EBC59 /* HMKCManagedObject.m */; };
                F4D2CBCB18B05E58000EBC59 /* HMMaserShipCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4D2CBCA18B05E58000EBC59 /* HMMaserShipCommand.m */; };
                F4E33B5818BF5F67005B8C07 /* HMMemberSlotItemCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E33B5718BF5F67005B8C07 /* HMMemberSlotItemCommand.m */; };
+               F4E7802B18DC3A820011BC4C /* HMLocalDataStore.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E7802A18DC3A820011BC4C /* HMLocalDataStore.m */; };
+               F4E7802E18DC3AF00011BC4C /* LocalData.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = F4E7802C18DC3AF00011BC4C /* LocalData.xcdatamodeld */; };
+               F4E7803118DC42680011BC4C /* HMKenzoHistory.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E7803018DC42680011BC4C /* HMKenzoHistory.m */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
                F4D2CBCA18B05E58000EBC59 /* HMMaserShipCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMMaserShipCommand.m; sourceTree = "<group>"; };
                F4E33B5618BF5F67005B8C07 /* HMMemberSlotItemCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMMemberSlotItemCommand.h; sourceTree = "<group>"; };
                F4E33B5718BF5F67005B8C07 /* HMMemberSlotItemCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMMemberSlotItemCommand.m; sourceTree = "<group>"; };
+               F4E7802918DC3A820011BC4C /* HMLocalDataStore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMLocalDataStore.h; sourceTree = "<group>"; };
+               F4E7802A18DC3A820011BC4C /* HMLocalDataStore.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMLocalDataStore.m; sourceTree = "<group>"; };
+               F4E7802D18DC3AF00011BC4C /* LocalData.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = LocalData.xcdatamodel; sourceTree = "<group>"; };
+               F4E7802F18DC42680011BC4C /* HMKenzoHistory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMKenzoHistory.h; sourceTree = "<group>"; };
+               F4E7803018DC42680011BC4C /* HMKenzoHistory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMKenzoHistory.m; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
                        children = (
                                F4513EDB18B619B600073552 /* HMCoreDataManager.h */,
                                F4513EDC18B619B600073552 /* HMCoreDataManager.m */,
+                               F4E7802918DC3A820011BC4C /* HMLocalDataStore.h */,
+                               F4E7802A18DC3A820011BC4C /* HMLocalDataStore.m */,
                                F4D2CBC618B0555A000EBC59 /* HMKCManagedObject.h */,
                                F4D2CBC718B0555A000EBC59 /* HMKCManagedObject.m */,
                                F4513EFC18BB6B0C00073552 /* HMKCShipObject.h */,
                                F4513EFD18BB6B0C00073552 /* HMKCShipObject.m */,
                                F46F104718ABAC910063E503 /* KCD.xcdatamodeld */,
+                               F4E7802F18DC42680011BC4C /* HMKenzoHistory.h */,
+                               F4E7803018DC42680011BC4C /* HMKenzoHistory.m */,
+                               F4E7802C18DC3AF00011BC4C /* LocalData.xcdatamodeld */,
                        );
                        name = CoreData;
                        sourceTree = "<group>";
                                F46F103618A7CE310063E503 /* HMJSONViewCommand.m in Sources */,
                                F4B414FB18B1FB6C00468DAF /* HMMasterMapCellCommand.m in Sources */,
                                F4C118FA18A67B1C005D5B25 /* CacheStoragePolicy.m in Sources */,
+                               F4E7802E18DC3AF00011BC4C /* LocalData.xcdatamodeld in Sources */,
                                F4C1190518A71AEA005D5B25 /* HMJSONReciever.m in Sources */,
                                F4B414F818B1D35800468DAF /* HMMasterMissionCommand.m in Sources */,
                                F4E33B5818BF5F67005B8C07 /* HMMemberSlotItemCommand.m in Sources */,
                                F4C3957418CDDFE500A1E918 /* HMKenzoDockStatus.m in Sources */,
                                F4C118FB18A67B1C005D5B25 /* CanonicalRequest.m in Sources */,
                                F4BDEB5A187252F30069D0CE /* HMAppDelegate.m in Sources */,
+                               F4E7803118DC42680011BC4C /* HMKenzoHistory.m in Sources */,
+                               F4E7802B18DC3A820011BC4C /* HMLocalDataStore.m in Sources */,
                                F494DC6718C8BA6400AE97E4 /* HMPowerUpSupportViewController.m in Sources */,
                                F494DC6318C608C100AE97E4 /* HMShipViewController.m in Sources */,
                                F46F104918ABAC910063E503 /* KCD.xcdatamodeld in Sources */,
                        sourceTree = "<group>";
                        versionGroupType = wrapper.xcdatamodel;
                };
+               F4E7802C18DC3AF00011BC4C /* LocalData.xcdatamodeld */ = {
+                       isa = XCVersionGroup;
+                       children = (
+                               F4E7802D18DC3AF00011BC4C /* LocalData.xcdatamodel */,
+                       );
+                       currentVersion = F4E7802D18DC3AF00011BC4C /* LocalData.xcdatamodel */;
+                       path = LocalData.xcdatamodeld;
+                       sourceTree = "<group>";
+                       versionGroupType = wrapper.xcdatamodel;
+               };
 /* End XCVersionGroup section */
        };
        rootObject = F4BDEB3B187252F30069D0CE /* Project object */;
diff --git a/KCD/HMKenzoHistory.h b/KCD/HMKenzoHistory.h
new file mode 100644 (file)
index 0000000..0ce603d
--- /dev/null
@@ -0,0 +1,24 @@
+//
+//  HMKenzoHistory.h
+//  KCD
+//
+//  Created by Hori,Masaki on 2014/03/21.
+//  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import <CoreData/CoreData.h>
+
+
+@interface HMKenzoHistory : NSManagedObject
+
+@property (nonatomic, retain) NSString *name;
+@property (nonatomic, retain) NSNumber *fuel;
+@property (nonatomic, retain) NSNumber *bull;
+@property (nonatomic, retain) NSNumber *steel;
+@property (nonatomic, retain) NSNumber *bauxite;
+@property (nonatomic, retain) NSNumber *kaihatusizai;
+@property (nonatomic, retain) NSNumber *isLarge;
+@property (nonatomic, retain) NSDate *date;
+
+@end
diff --git a/KCD/HMKenzoHistory.m b/KCD/HMKenzoHistory.m
new file mode 100644 (file)
index 0000000..0d10efa
--- /dev/null
@@ -0,0 +1,23 @@
+//
+//  HMKenzoHistory.m
+//  KCD
+//
+//  Created by Hori,Masaki on 2014/03/21.
+//  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
+//
+
+#import "HMKenzoHistory.h"
+
+
+@implementation HMKenzoHistory
+
+@dynamic name;
+@dynamic fuel;
+@dynamic bull;
+@dynamic steel;
+@dynamic bauxite;
+@dynamic kaihatusizai;
+@dynamic isLarge;
+@dynamic date;
+
+@end
diff --git a/KCD/HMLocalDataStore.h b/KCD/HMLocalDataStore.h
new file mode 100644 (file)
index 0000000..2f4eb2d
--- /dev/null
@@ -0,0 +1,20 @@
+//
+//  HMLocalDataStore.h
+//  KCD
+//
+//  Created by Hori,Masaki on 2014/03/21.
+//  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface HMLocalDataStore : NSObject
+
+@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
+
++ (instancetype)defaultManager;
++ (instancetype)oneTimeEditor;
+
+- (IBAction)saveAction:(id)sender;
+
+@end
diff --git a/KCD/HMLocalDataStore.m b/KCD/HMLocalDataStore.m
new file mode 100644 (file)
index 0000000..4f91141
--- /dev/null
@@ -0,0 +1,205 @@
+//
+//  HMLocalDataStore.m
+//  KCD
+//
+//  Created by Hori,Masaki on 2014/03/21.
+//  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
+//
+
+#import "HMLocalDataStore.h"
+
+@implementation HMLocalDataStore
+
+@synthesize managedObjectContext = _managedObjectContext;
+
+static HMLocalDataStore *defaultManager = nil;
+
+static NSPersistentStoreCoordinator *_persistentStoreCoordinator = nil;
+static NSManagedObjectModel *_managedObjectModel = nil;
+
+
++ (void)load
+{
+       static dispatch_once_t onceToken;
+       dispatch_once(&onceToken, ^{
+               [self defaultManager];
+       });
+}
+
++ (instancetype)defaultManager
+{
+       if(defaultManager) return defaultManager;
+       
+       defaultManager = [self new];
+       [defaultManager.managedObjectContext setMergePolicy:NSRollbackMergePolicy];
+       
+       NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
+       [nc addObserver:defaultManager
+                  selector:@selector(applicationWillTerminate:)
+                          name:NSApplicationWillTerminateNotification
+                        object:NSApp];
+       [nc addObserver:defaultManager
+                  selector:@selector(anotherContextDidSave:)
+                          name:NSManagedObjectContextDidSaveNotification
+                        object:nil];
+       
+       return defaultManager;
+}
+
++ (instancetype)oneTimeEditor
+{
+       // we need default manager.
+       [self defaultManager];
+       
+       HMLocalDataStore *result = [self new];
+       [result.managedObjectContext setMergePolicy:NSOverwriteMergePolicy];
+       return result;
+}
+
+- (void)dealloc
+{
+       [self saveAction:nil];
+}
+
+// Returns the directory the application uses to store the Core Data store file. This code uses a directory named "com.masakih.KanColleLevelManager" in the user's Application Support directory.
+- (NSURL *)applicationFilesDirectory
+{
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    NSURL *appSupportURL = [[fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject];
+    return [appSupportURL URLByAppendingPathComponent:@"com.masakih.KCD"];
+}
+
+// Creates if necessary and returns the managed object model for the application.
+- (NSManagedObjectModel *)managedObjectModel
+{
+    if (_managedObjectModel) {
+        return _managedObjectModel;
+    }
+       
+    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"LocalData" withExtension:@"momd"];
+    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
+    return _managedObjectModel;
+}
+
+// Returns the persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. (The directory for the store is created, if necessary.)
+- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
+{
+    if (_persistentStoreCoordinator) {
+        return _persistentStoreCoordinator;
+    }
+    
+    NSManagedObjectModel *mom = [self managedObjectModel];
+    if (!mom) {
+        NSLog(@"%@:%@ No model to generate a store from", [self class], NSStringFromSelector(_cmd));
+        return nil;
+    }
+    
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    NSURL *applicationFilesDirectory = [self applicationFilesDirectory];
+    NSError *error = nil;
+    
+    NSDictionary *properties = [applicationFilesDirectory resourceValuesForKeys:@[NSURLIsDirectoryKey] error:&error];
+    
+    if (!properties) {
+        BOOL ok = NO;
+        if ([error code] == NSFileReadNoSuchFileError) {
+            ok = [fileManager createDirectoryAtPath:[applicationFilesDirectory path] withIntermediateDirectories:YES attributes:nil error:&error];
+        }
+        if (!ok) {
+            [[NSApplication sharedApplication] presentError:error];
+            return nil;
+        }
+    } else {
+        if (![properties[NSURLIsDirectoryKey] boolValue]) {
+            // Customize and localize this error.
+            NSString *failureDescription = [NSString stringWithFormat:@"Expected a folder to store application data, found a file (%@).", [applicationFilesDirectory path]];
+            
+            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
+            [dict setValue:failureDescription forKey:NSLocalizedDescriptionKey];
+            error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:101 userInfo:dict];
+            
+            [[NSApplication sharedApplication] presentError:error];
+            return nil;
+        }
+    }
+#if 0
+       NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"LocalData.storedata"];
+       NSString *storeType = NSSQLiteStoreType;
+#else
+       NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"LocalData.storedata.xml"];
+       NSString *storeType = NSXMLStoreType;
+#endif
+       NSDictionary *options = @{
+                                                         NSMigratePersistentStoresAutomaticallyOption : @YES,
+                                                         NSInferMappingModelAutomaticallyOption : @YES
+                                                         };
+    NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
+    if (![coordinator addPersistentStoreWithType:storeType configuration:nil URL:url options:options error:&error]) {
+               [[NSApplication sharedApplication] presentError:error];
+               return nil;
+    }
+       
+    _persistentStoreCoordinator = coordinator;
+    
+    return _persistentStoreCoordinator;
+}
+
+// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
+- (NSManagedObjectContext *)managedObjectContext
+{
+    if (_managedObjectContext) {
+        return _managedObjectContext;
+    }
+    
+    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
+    if (!coordinator) {
+        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
+        [dict setValue:@"Failed to initialize the store" forKey:NSLocalizedDescriptionKey];
+        [dict setValue:@"There was an error building up the data file." forKey:NSLocalizedFailureReasonErrorKey];
+        NSError *error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
+        [[NSApplication sharedApplication] presentError:error];
+        return nil;
+    }
+    _managedObjectContext = [[NSManagedObjectContext alloc] init];
+    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
+       
+       
+       
+    return _managedObjectContext;
+}
+
+// Performs the save action for the application, which is to send the save: message to the application's managed object context. Any encountered errors are presented to the user.
+- (IBAction)saveAction:(id)sender
+{
+    NSError *error = nil;
+    
+    if (![[self managedObjectContext] commitEditing]) {
+        NSLog(@"%@:%@ unable to commit editing before saving", [self class], NSStringFromSelector(_cmd));
+    }
+    
+    if (![[self managedObjectContext] save:&error]) {
+        [[NSApplication sharedApplication] presentError:error];
+    }
+}
+
+- (void)applicationWillTerminate:(NSNotification *)notification
+{
+       [self saveAction:nil];
+}
+
+- (void)anotherContextDidSave:(NSNotification *)notification
+{
+       NSManagedObjectContext *moc = [notification object];
+       NSPersistentStoreCoordinator *psc = [moc persistentStoreCoordinator];
+       if(![psc isEqual:_persistentStoreCoordinator]) return;
+       
+       if([NSThread isMainThread]) {
+               [self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
+       } else {
+               dispatch_sync(dispatch_get_main_queue(), ^{
+                       [self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
+               });
+       }
+}
+
+@end
diff --git a/KCD/LocalData.xcdatamodeld/LocalData.xcdatamodel/contents b/KCD/LocalData.xcdatamodeld/LocalData.xcdatamodel/contents
new file mode 100644 (file)
index 0000000..af2542f
--- /dev/null
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="5063" systemVersion="13C64" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
+    <entity name="KenzoHistory" representedClassName="HMKenzoHistory" syncable="YES">
+        <attribute name="bauxite" attributeType="Integer 32" minValueString="30" syncable="YES"/>
+        <attribute name="bull" attributeType="Integer 32" minValueString="30" syncable="YES"/>
+        <attribute name="date" attributeType="Date" indexed="YES" syncable="YES"/>
+        <attribute name="fuel" attributeType="Integer 32" minValueString="30" syncable="YES"/>
+        <attribute name="isLarge" attributeType="Boolean" syncable="YES"/>
+        <attribute name="kaihatusizai" attributeType="Integer 32" minValueString="1" syncable="YES"/>
+        <attribute name="name" attributeType="String" minValueString="1" indexed="YES" syncable="YES"/>
+        <attribute name="steel" attributeType="Integer 32" minValueString="30" syncable="YES"/>
+    </entity>
+    <elements>
+        <element name="KenzoHistory" positionX="0" positionY="0" width="128" height="165"/>
+    </elements>
+</model>
\ No newline at end of file