ç§ã®æã¯ããã§combããŠããŠããã®ãããªã¹ãŠã£ãããã©ããªçš®é¡ã®ç£ã§ãå®éã«äœãé£ã¹ãŠããã®ãã調ã¹ãŸããã äºæ³ã©ããããããŸã§å€ãã®åé¡ãšèœãšãç©ŽããããŸããããŸãããã®Swiftã調çããæ¹æ³ããŸã£ããããããŸããã CoreDataã§ãã®Swiftã®åéãäœãããšãããšãã«ãæ倧ã®åé¡ã¯ç§ãåŸ ã£ãŠããŸãã-ç©äºã¯åºæ¬çã«åäœãæåŠããŸããã è±å¯ãªã°ãŒã°ã«ã¯ãå°ãªããšãè¯ãçµæã«ã€ãªãããŸããã§ãã-æ å ±ã¯éåžžã«æççã§ããããæŸèæã§å©ãããŸããã ãããã£ãŠãèŠãã¿ã®æåã®å€ã«ãç§ã¯å€ãæ¹æ³ã§CoreDataãæäœããéã«æãæããªãœãªã¥ãŒã·ã§ã³ã䜿çšããããšãæ念ãã決å®ããŸãã-ãã¹ãŠã®ã³ãŒããå€ãè¯ãObjective-Cã«ä¿åããSwiftãããã§ã«ã¢ã¯ã»ã¹ããŸãïŒããšãã°ãã€ã³ã¿ãŒãã§ãŒã¹ã§ïŒã ããããéã®å®å šäž»çŸ©ã¯äŒæ¯ãäžãããæŸèæãªãã§ã¯ãªããèªããŠãããã®ã®ãå®éã«è¡ãããšãã§ããçŽç²ãªåäžèšèªã®æ±ºå®ãå®è£ ããå¿ èŠããããŸããã 誰ããã®ããã»ã¹ã«èå³ãããã®ãââãç«ããé¡ãããŸãã ãŸããéäžã§ãã°ãåéããããšããå§ãããŸãããæ°ããèšèªã«ä»å±ãããã®ã¯ç§ã®æèŠã§ã¯æã䟿å©ã§ã¯ãããŸããã ããããç§ã¯äœãããããªããšããã-ã³ã¡ã³ããä¿®æ£ããããŠãã¹ããã©ã¯ãã£ã¹ã®è°è«ã«æè¬ããã
æéãæ°ã«ãã人
èšäºhttps://github.com/KoNEW/CoreDataTest.gitãèªãããšãªãããããããµã³ãã«ãããã«ããŠã³ããŒãããŠããã¹ãŠèªåã§åžãããšãã§ããŸãã
åæäŸ
äœãéžã³ãŸãã
以äžã§ã¯ããã¹ãŠã®åé¡ãšäŸã®åæã®ããã«ãåæãããžã§ã¯ãã䜿çšããŸããå€å žçãªãšã³ãã£ãã£ãDepartmentããšãEmployeeãã衚瀺ããã³ç®¡çããããã®ã¢ããªã±ãŒã·ã§ã³ãäœæããŸãã
ããã«ã次ã®ãããªãã£ãŒã«ãã§éšéãç¹åŸŽä»ããŸãã
- ååïŒæåå-å¿ é ïŒ
- å éšçªå·ïŒçªå·-å¿ é ïŒ
- é»è©±çªå·ïŒè¡-ãªãã·ã§ã³ãçªå·ã®æ£ç¢ºæ§ã®ãã§ãã¯ã¯è¡ããŸããïŒ
ãããŠåŸæ¥å¡ãããããïŒ
- ååïŒæåå-å¿ é ïŒ
- å§ïŒæåå-å¿ é ïŒ
- æ§å¥ïŒæ°å€-å¿ é ïŒ
- 幎霢ïŒæ°å€-ãªãã·ã§ã³ïŒ
ããŒã¿ç®¡çè
å®éãæåã®ã¹ãããã¯XcodeãéããCoreDataãšSwiftèšèªã»ããã䜿çšããŠç°¡åãªãããžã§ã¯ããäœæããããšã§ãã ãã®æ®µéã§è¡ãå¯äžã®ç·šéã¯ãã¢ããªã±ãŒã·ã§ã³ããªã²ãŒãããCoreDataã®ãã¹ãŠã®äœæ¥ãåãåãããããå¥ã®ã¯ã©ã¹ã«è»¢éããããšã§ããããã¯ãã·ã³ã°ã«ãã³ãšããŠæ©èœããŸãã ç§ã¯ã³ãŒããæžãã®ã«æ £ããŠãããšãã«ããããã£ãŠããã®ã§ãããããã§ç¹°ãè¿ããŸã-åæã«ãSwiftã§ã·ã³ã°ã«ãã³ãäœæããæ¹æ³ãèŠãããšãã§ããŸãã ãã¹ãŠã®ã¯ã©ã¹ã®ãã¬ãã£ãã¯ã¹ã¯ã以éCSïŒCoreData + SwiftïŒã«ãã£ãŠäœ¿çšãããŸãã
ãžã£ã çªå·1
ãããXcode 6 Betaã®ãã°ãªã®ãæ©èœãªã®ãã¯ããããŸããããæ¯åæžã蟌ãŸãªãããã«ãç¬èªã®ã¯ã©ã¹ã®ãã¬ãã£ãã¯ã¹ã¯ãæåã§èšå®ããå¿
èŠããããŸãã ãããžã§ã¯ããã¡ã€ã«ãéžæãããšã[ãã¡ã€ã«ã€ã³ã¹ãã¯ã¿ãŒ]ã¿ãã§ãããå®è¡ã§ããŸãã
ããã§ãç§ãã¡ã¯äœãããŸããïŒ
- AppDelegateããCoreDataã®ãã¹ãŠã®äœæ¥ãåãåããŸã
- CSDataManagerã¯ã©ã¹ãäœæãã
- ãã®äžã§ãã¢ãã«ãã³ã³ããã¹ããããã³ãã€ã¬ã¯ãããŒã¿ãŠã§ã¢ããŠã¹ãæäœããããã®ããããã£ãäœæããŸãã
- ã·ã³ã°ã«ãã³ã®åœ¢ã§äœæ¥ããããã®ã¡ãœãããäœæããŸã
ãã®çµæãAppDelegate.swiftãã¡ã€ã«ã¯æ¬¡ã®ããã«ãªããŸãã
import UIKit import CoreData @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { // Override point for customization after application launch. return true } }
ãŸããCSDataManager.swftãã¡ã€ã«ã¯æ¬¡ã®ãšããã§ãã
import UIKit import Foundation import CoreData let kCSErrorDomain = "ru.novilab-mobile.cstest" let kCSErrorLocalStorageCode = -1000 @objc(CSDataManager) class CSDataManager:NSObject { //Managed Model var _managedModel: NSManagedObjectModel? var managedModel: NSManagedObjectModel{ if !_managedModel{ _managedModel = NSManagedObjectModel.mergedModelFromBundles(nil) } return _managedModel! } //Store coordinator var _storeCoordinator: NSPersistentStoreCoordinator? var storeCoordinator: NSPersistentStoreCoordinator{ if !_storeCoordinator{ let _storeURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("CSDataStorage.sqlite") _storeCoordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedModel) func addStore() -> NSError?{ var result: NSError? = nil if _storeCoordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: _storeURL, options: nil, error: &result) == nil{ println("Create persistent store error occurred: \(result?.userInfo)") } return result } var error = addStore() if error != nil{ println("Store scheme error. Will remove store and try again. TODO: add scheme migration.") NSFileManager.defaultManager().removeItemAtURL(_storeURL, error: nil) error = addStore() if error{ println("Unresolved critical error with persistent store: \(error?.userInfo)") abort() } } } return _storeCoordinator! } //Managed Context var _managedContext: NSManagedObjectContext? = nil var managedContext: NSManagedObjectContext { if !_managedContext { let coordinator = self.storeCoordinator if coordinator != nil { _managedContext = NSManagedObjectContext() _managedContext!.persistentStoreCoordinator = coordinator } } return _managedContext! } //Init init() { super.init() NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil) } @objc(appDidEnterBackground) func appDidEnterBackground(){ var (result:Bool, error:NSError?) = self.saveContext() if error != nil{ println("Application did not save data with reason: \(error?.userInfo)") } } // Returns the URL to the application's Documents directory. var applicationDocumentsDirectory: NSURL { let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) return urls[urls.endIndex-1] as NSURL } //Save context func saveContext() -> (Bool, NSError?){ println("Will save") var error: NSError? = nil var result: Bool = false let context = self.managedContext if context != nil{ if context.hasChanges && !context.save(&error){ println("Save context error occurred: \(error?.userInfo)") }else{ result = true } }else{ let errorCode = kCSErrorLocalStorageCode let userInfo = [NSLocalizedDescriptionKey : "Managed context is nil"] error = NSError.errorWithDomain(kCSErrorDomain, code: errorCode, userInfo: userInfo) } return (result, error) } //Singleton Instance class func sharedInstance() -> CSDataManager{ struct wrapper{ static var shared_instance: CSDataManager? = nil static var token: dispatch_once_t = 0 } dispatch_once(&wrapper.token, {wrapper.shared_instance = CSDataManager()}) return wrapper.shared_instance! } }
åºç€ã¯ãXcodeãçæããã³ãŒãã«ãã£ãŠèªåçã«æ¡çšãããŸãããã€ãŸããããçšåºŠã¯åç §ãšèŠãªãããšãã§ããŸãã èšèªãã¬ãŒãã³ã°ã®é¢ã§èå³æ·±ãããšããããã®ãã¡ã€ã«ã§ã¯ç§ãèªåã§éžã³ãŸãã
- ã°ããŒãã«å®æ°ã䜿çšãã
- ã¯ã©ã¹ããããã£ãæäœãã
- éçã¯ã©ã¹ã¡ãœããïŒå€ãè¯ãã¯ã©ã¹ãªããžã§ã¯ãã¡ãœããïŒ
- ã¹ãã¢ãããã·ãŒãžã£ïŒstoreCoordinatorããããã£ãæåºããŸãããããã«ãããå°æ¥çã«ããŒã¿ã¢ãã«ã絶ããå€æŽãããå¿ èŠã«å¿ããŠããŒã¿ããŒã¹ãã¡ã€ã«ãèªåçã«äžæžããããããã«ãªããŸããïŒ
- ã³ã³ããã¹ããä¿åããããã®å€æŽãããã¡ãœããã®äŸã䜿çšããŠã¿ãã«ãæäœãã
- NSNotificationCenterãšé£æºãã
ãžã£ã çªå·2
ããããã£ãæäœãã-ç§ã¯æ¬åœã«å¥œãã§ã¯ãããŸããã ãã®äŸã¯ãXcodeèªäœãããã©ã«ãã§æäŸãããã®ã«åºã¥ããŠããŸã-ãããã£ãŠãããã¯æ¢åã®ãœãªã¥ãŒã·ã§ã³ã®äžã§æé«ã®ãã®ã§ãããšçµè«ä»ããŸãã ç§ã¯ãããç¹ã«å¥œãã§ã¯ãããŸãã-ã¹ãã¬ãŒãžçšã®å
éšå€æ°ãçŽæ¥å®£èšããå¿
èŠããããŸãïŒä»¥åã¯å
éšã§æ©èœããŠããŸããïŒã ããã«ãå
é ã«äžç·ãä»ããŠããã«ãããããããå€æ°èªäœã¯å€éšããèŠãããŸãŸã§ããå®éããã¡ã€ã«ã€ã³ã¹ãã¯ã¿ãŒã§ã¯ãã¿ã¹ã¯ããšã«2ã€ã®ããããã£ã衚瀺ãããŠããŸãã ç§ã奜ãã§ã¯ãªãåèšïŒ
- ãã®ãããªåé¡ã解決ããããã«ããããã£ãæ瀺çã«è€è£œããå¿ èŠ
- å éšå€æ°ã®ã¿ãäœæã§ããªã
- å éšããããã£ãäœæã§ããªãããš-調éãã¡ã€ã«ã§ã¯ãªããå®è£ ãã¡ã€ã«å ã§ããããã£ãå®çŸ©ããããšã決å®ããå
ãžã£ã çªå·3
ã·ã³ã°ã«ãã³ãã¿ãŒã³ã¯ãå
éšæ§é ã䜿çšããå
ãæŸèæã«ãã£ãŠå®è£
ãããŸãã çè«çã«ã¯ãããã¯èšèªä»æ§ã§å®£èšãããŠããã¯ã©ã¹å€æ°ïŒã¯ã©ã¹varïŒã䜿çšããŠç°¡åãªæ¹æ³ã§è§£æ±ºããå¿
èŠããããŸãããå®éã«ã¯ã³ã³ãã€ã©ã¯ãŸã ãµããŒããããŠããŸããã æ²ãã¿ãæ²ãã¿-ç§ãã¡ã¯ä¿®æ£ãåŸ
ã£ãŠããŸãã ãŸãã以åã®ïŒObjective-Cãšæ¯èŒããŠïŒèšèªã®çŸåšã®ããŒãžã§ã³ã§ã¯ãã¯ã©ã¹åæååããã©ã€ããŒãã¡ãœãããšããŠæå®ããããšã¯ã§ããŸããããã®çµæãçŽç²ãªãã«ã«åŒ·ãã·ã³ã°ã«ãã³ãäœæããããšã¯ãŸã ã§ããŸããã
Jambçªå·4ãŸãã¯æ©èœãããããŸãã
NSNotificationCenterã®åŒã³åºããã©ã®ããã«æ©èœãããã«æ³šæãæã䟡å€ããããŸãã 1ã€ã®ç°¡åãªãã€ã³ãããããŸãã Appleã¯ããã¹ãŠã®ã·ã¹ãã ã©ã€ãã©ãªïŒUIKitãFoundationãCoreDataãªã©ïŒã¯æ¢ã«Swiftãšå®å
šã«å奜é¢ä¿ã«ãããšæžããŠããŸãã ããããå®éã«ã¯ããã¯ãŸã£ããçå®ã§ã¯ãããŸããããããã§ã¯ãããŸããããå®å
šã§ã¯ãããŸããã ã€ãŸããå
éšçã«NSNotificationCenterã¯çŽç²ãªObjective-Cã§å®è¡ãããã»ãšãã©ã®å Žåãã³ãŒãå
ã®ä»ã®ãã¹ãŠãšäºææ§ããããŸãã ãã®ããããã®ã¢ããªã±ãŒã·ã§ã³ã«ã¯ããã€ãã®ãã¥ã¢ã³ã¹ãšå¶éããããŸãã
ã³ãŒããObjective-CåŒã³åºãã§é©åã«æ©èœããããã«ã¯ãäºææ§ãæãããå¿ èŠããããŸããããã§ã¯ãäžè¬ã«ããã¹ãŠãæ瀺ã«åŸã£ãŠããŸãã
ããžãã¯å±æ§ãã¯ã©ã¹åãšå¿ èŠãªã¡ãœããã«è¿œå ããŸã-ããšãã°ãããã¯äžéšã§ãïŒ
éç¥ã»ã³ã¿ãŒããã®åŒã³åºããsaveContextã¡ãœããèªäœã«çµã³ä»ããããšã¯è«ççã§ãããTupleãè¿ãããããããããè¡ãããšã¯ã§ããŸããããã®ãããªæ§é ã¯Objective-Cã§å®çŸ©ãããŠããŸããã ãã®ãããåçŽãª
ã¡ãœããåŒã³åºãã§æŸèæã䜿çšããŸãã ååãšããŠããã¹ãŠãçŠ ã§ãã ãããã補åãèšèšãããšãã®é ã®äžã§ã¯ããã®ãããªããšãå¿ã«çããŠãã䟡å€ããããŸãã
ã¢ãŒã¡ã³ãã¯ã³
ã³ãŒããObjective-CåŒã³åºãã§é©åã«æ©èœããããã«ã¯ãäºææ§ãæãããå¿ èŠããããŸããããã§ã¯ãäžè¬ã«ããã¹ãŠãæ瀺ã«åŸã£ãŠããŸãã
@objc()
ããžãã¯å±æ§ãã¯ã©ã¹åãšå¿ èŠãªã¡ãœããã«è¿œå ããŸã-ããšãã°ãããã¯äžéšã§ãïŒ
@objc(CSDataManager) class CSDataManager:NSObject { ... @objc(appDidEnterBackground) func appDidEnterBackground(){ ...
ã¢ãŒã¡ã³ã2
éç¥ã»ã³ã¿ãŒããã®åŒã³åºããsaveContextã¡ãœããèªäœã«çµã³ä»ããããšã¯è«ççã§ãããTupleãè¿ãããããããããè¡ãããšã¯ã§ããŸããããã®ãããªæ§é ã¯Objective-Cã§å®çŸ©ãããŠããŸããã ãã®ãããåçŽãª
void
ã¡ãœããåŒã³åºãã§æŸèæã䜿çšããŸãã ååãšããŠããã¹ãŠãçŠ ã§ãã ãããã補åãèšèšãããšãã®é ã®äžã§ã¯ããã®ãããªããšãå¿ã«çããŠãã䟡å€ããããŸãã
ããŒã¿ã¢ãã«ãäœæãã
ããã§ã¯ããã¹ãŠãéåžžã«æšæºçãªXcodeããŒã«ã§ãããããŒã¿ã¢ãã«ãäœæããŸã-æçµçã«ã¯ãã®ãããªãã®ãåŸãããŸãã
å®éã«åé¡
ãããŠãäœãåé¡ãªã®ãã NSManagedObjectããã®ç¶æ¿ã¯ã©ã¹ã®ã³ãŒãçæã¯ãXcode 6-Betaã§ã¯å£ããŠããŸãã ããæ£ç¢ºã«ã¯ãã³ãŒãã¯Swiftã§ã¯ãªãObjective-Cã§çæãããŸãããããã¯äžè¬ã«äžè¬çãªãã®ã§ã¯ãããŸããã
ããã§ãç°¡æœã«ãããã§ã®è§£æ±ºçã¯äœã§ããïŒ
- ãªãã·ã§ã³A.éåžžã®ã³ãŒãçæã䜿çšããéåžžã®æå¹ãªObjective-Cãã¡ã€ã«ãååŸããBridgingãã¡ã€ã«ãéããŠã¡ã€ã³ã®Swiftã³ãŒãã§äœ¿çšããŸãã Swiftã§ã«ã¹ã¿ã Objective-Cã¯ã©ã¹ã䜿çšããæ¹æ³ã«ã€ããŠã¯ãåããããžã§ã¯ãã®SwiftãšObjective-Cãåç §ããŠãã ããã
- ãªãã·ã§ã³Bãå察ã®ããšãããããšããŠããŸããObjective-Cã¯ã©ã¹ã®ããã«åäœããåæã«å€è²šã®CoreDataã§åäœããSwiftã¯ã©ã¹ãäœæããŸãã ãããè¡ãã«ã¯ããã¡ã€ã«ãå³æã«ãAppleã®éåžžã«çãã¬ã€ããå·Šã«ãObjective-Cã®åäœã§Swiftã¯ã©ã¹ãæžã
æ瀺ã«åŸã
æåã«ãéšéãã®1ã€ã®ãšãã»ã³ã¹ã ãã§äœæ¥ããããšãèããŠã¿ãŸããããå°ãåŸã§é¢ä¿ã«æ»ããŸãã ãã®ãããAppleã®æ瀺ã«åŸã£ãŠãCSDepartmentã¯ã©ã¹ã説æããããã«ãã®ãã¡ã€ã«ã«ã¢ã¯ã»ã¹ããŸãã
import Foundation import CoreData import UIKit class CSDepartment : NSManagedObject{ @NSManaged var title: NSString @NSManaged var internalID: NSNumber @NSManaged var phone: NSString? }
ãããŠãç°¡åã«ããããã«AppDelegateã«æ®ããã³ãŒãã䜿çšããŠãã¹ãŠã®äœæ¥ã確èªããŸãïŒåŸã§ãBolã§æ£ããããŒãžã§ã³ãå€æŽãããŸãïŒã
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { //Get manager let manager = CSDataManager.sharedInstance() //Create new department let newDepartment : AnyObject! = NSEntityDescription.insertNewObjectForEntityForName("CSDepartment", inManagedObjectContext: manager.managedContext) //Save context manager.saveContext() //Get and print all departments let request = NSFetchRequest(entityName: "CSDepartment") let departments = manager.managedContext.executeFetchRequest(request, error: nil) println("Departments: \(departments)") return true }
å§ããŠããã°ãèŠãŠãæ²ããã§ãã éèŠãªãã€ã³ãïŒ
- ãŸããããã€ãã®ãã£ãŒã«ããå¿ é ïŒã¿ã€ãã«ãšinternalIDïŒãšããŠèšå®ãããŠãããããCoreDataã§ããŒã¿ãä¿åããéã«ãšã©ãŒãçºçããŸãããããããæå®ããŸããã§ããã ã¢ãã«ãã¡ã€ã«ã«ããã©ã«ãå€ãèšå®ããããawakeFromInsertã¡ãœããã䜿çšããäžè¬çãªãœãªã¥ãŒã·ã§ã³ã次ã«ç€ºããŸãã æåã®è§£æ±ºçã¯æ©èœããŠããŸãããã¹ããŒãã§ã¯ãããŸããã2çªç®ã®è§£æ±ºçã¯ãã®åœ¢åŒã§ã¯æ©èœããŠããŸãã-ã¡ãœãããåŒã³åºãããªããããç§ã¯èœèããŸããã
- 2çªç®ã«éèŠãªç¹ã¯ããã°ã«Departmentsã®ãããªãã®ãããããšã§ãã
[<NSManagedObject: 0xb264030> (entity: CSDepartment; id: 0xb264090 <x-coredata:///CSDepartment/t3403D9E7-F910-4E2D-989E-95D9C984C1762> ; data: {employees = (); internalID = nil;phone = nil;title = nil;})]
ãã®ãã°ããéèŠãªçµè«ãåŸãããŸã-å€æ°ã¯ãCSDepartmentãšããŠã§ã¯ãªããNSManagedObjectã®ã€ã³ã¹ã¿ã³ã¹ãšããŠã€ã³ã¹ã¿ã³ã¹åãããŸããã ãã®çµæããã£ãŒã«ãã®å€ãæå®ããããšãã§ããŸããã
let newDepartment : CSDepartment = NSEntityDescription.insertNewObjectForEntityForName("CSDepartment", inManagedObjectContext: manager.managedContext) as CSDepartment
åäœãããã¢ããªã±ãŒã·ã§ã³ãã¯ã©ãã·ã¥ããã ãã§ãã
ãã¡ã€ã«ã«å ¥ããŸã
ãããã¯ãŒã¯ãããŸãããã³ãŒããæ©èœãããäžé£ã®ãžã§ã¹ãã£ãŒãèŠã€ããŸããã ããã§ãããªããããå¿ èŠãããããšïŒ
ã¹ããã1. Objective-Cãšäºææ§ããããŸãã
å éšã®ã©ããã§ãã¯ãªãŒã³ãªObjective-CåŒã³åºããå床å®è¡ããŠãããããæ°ããã¯ã©ã¹ãObjective-CåŒã³åºããšäºææ§ãæãããå¿ èŠããããŸãã
@objc()
ãã£ã¬ã¯ãã£ãã«ããããã§ã«éåžžã®æ¹æ³ã§å®è¡ããŠããŸã
ã¹ããã2.ã¢ãã«ãã¡ã€ã«ãç 磚ããŸãã
ããã¯æãããªã¹ãããã§ã¯ãããŸãããã¢ãã«ã®ãã¡ã€ã«ãå床éžæãããšã³ãã£ãã£ã®è¡šç€ºã«äœ¿çšããã¯ã©ã¹ãã¢ãã«ã®æ§æã§æäœæ¥ã§ç»é²ããå¿ èŠããããŸãã
ã¹ããã3.å粧åã
åã®2ã€ã®ã¹ãããã®åŸããã¹ãŠãæ©èœããã¯ãã§ãããawakeFromInsertã¡ãœãããè¿œå ããŸãããããããæ©èœããããã«ãªããŸããã ãŸããããçŸããç解ããããããŒã¿è¡ããã°ã«è¡šç€ºãããããã«ã説æã¡ãœãããè¿œå ããŸããã
ãã®çµæãã¯ã©ã¹ã®ã³ãŒãã¯æ¬¡ã®ããã«ãªãå§ããŸããã
import Foundation import CoreData import UIKit @objc(CSDepartment) class CSDepartment : NSManagedObject{ @NSManaged var title: NSString @NSManaged var internalID: NSNumber @NSManaged var phone: NSString? override func awakeFromInsert() { self.title = "New department" self.internalID = 0 } func description() -> NSString{ return "Department: className=\(self.dynamicType.description()), title=\(self.title), id=[\(self.internalID)] and phone=\(self.phone)" } }
ãã¹ããå床å®è¡ããŸã-ãã¹ãŠãæ©èœããåãã§ããã ããŸãã
é¢ä¿ãæ±ã
ãããã£ãŠãäºçŽ°ãªãšã³ãã£ãã£ãæŽçãããŠããŸãã åæ§ã«ã
CSEmployee
ãšã³ãã£ãã£ã®èª¬æãäœæã§ããŸãã ã·ã¹ãã ã«ãšã³ãã£ãã£ãæ£ããæ©èœãããããã«-æ¥ç¶ãè¿œå ããã³åé€ã§ããããã«ããããã«ãããš1ã€ã ããæ®ã£ãŠããŸãã éšéãšåŸæ¥å¡ãšã®é¢ä¿ã¯1察å€ã§ãã ããã§ãæ°ããèšèªãšXcodeã¯2ã€ã®æ¹æ³ã§åäœããŸããã
åŸæ¥å¡ããéšéãžã®éä¿¡ãå®è£ ããããã«ããã¹ãŠãç°¡åã§ããããšãå€æããŸãã-éšéèªäœãæãããããã£ã®ãªã¹ãã«å¥ã®ãã®ãè¿œå ããã ãã§ãã åèšãããšãåŸæ¥å¡ã¯ã©ã¹ã¯æ¬¡ã®ããã«ãªãå§ããŸããïŒèªåã®ã°ããŒãã«é åããå§ãšåã®ã©ã³ãã çæãè¿œå ããŸããïŒã
import Foundation import CoreData let st_fNames = ["John", "David", "Michael", "Bob"] let st_lNames = ["Lim", "Jobs", "Kyler"] @objc(CSEmployee) class CSEmployee:NSManagedObject{ @NSManaged var firstName: NSString @NSManaged var lastName: NSString @NSManaged var age: NSNumber? @NSManaged var department: CSDepartment override func awakeFromInsert() { super.awakeFromInsert() self.firstName = st_fNames[Int(arc4random_uniform(UInt32(st_fNames.count)))] self.lastName = st_lNames[Int(arc4random_uniform(UInt32(st_lNames.count)))] } func description() -> NSString{ return "Employee: name= \(self.firstName) \(self.lastName), age=\(self.age) years" } }
ããããéšéã®åŽã§ã¡ã«ããºã ã®ãµããŒããå®è£ ããããã«ãç§ã¯ãã¡ã€ã«ããã匷ãæã«å ¥ããªããã°ãªããŸããã§ãã-åã³ãå£ããã³ãŒãçæã®ããã«ãåãè¿œå ããããã®éæ³ã®ã¡ãœããã¯äœæãããŸããã§ããã åèšã§æ¬¡ã®ããšãè¡ããŸãã
- NSSetã¯ã©ã¹ã§åŸæ¥å¡ãæ ŒçŽããããããã£ãè¿œå ããŸã
- åŸæ¥å¡ãè¿œå ããã³åé€ããã«ã¹ã¿ã ã¡ãœãããè¿œå -Objective-CããŒãžã§ã³ã®Xcodeã¹ããããã«åºã¥ããŠå®è¡
ãã®çµæãã¯ã©ã¹ã¯æ¬¡ã®ããã«ãªãå§ããŸããã
import Foundation import CoreData @objc(CSDepartment) class CSDepartment : NSManagedObject{ @NSManaged var title: NSString @NSManaged var internalID: NSNumber @NSManaged var phone: NSString? @NSManaged var employees: NSSet override func awakeFromInsert() { self.title = "New department" self.internalID = 0 } func description() -> NSString{ let employeesDescription = self.employees.allObjects.map({employee in employee.description()}) return "Department: title=\(self.title), id=[\(self.internalID)], phone=\(self.phone) and employees = \(employeesDescription)" } //Working with Employees func addEmployeesObject(employee: CSEmployee?){ let set:NSSet = NSSet(object: employee) self.addEmployees(set) } func removeEmployeesObject(employee: CSEmployee?){ let set:NSSet = NSSet(object: employee) self.removeEmployees(set) } func addEmployees(employees: NSSet?){ self.willChangeValueForKey("employees", withSetMutation: NSKeyValueSetMutationKind.UnionSetMutation, usingObjects: employees) self.primitiveValueForKey("employees").unionSet(employees) self.didChangeValueForKey("employees", withSetMutation: NSKeyValueSetMutationKind.UnionSetMutation, usingObjects: employees) } func removeEmployees(employees: NSSet?){ self.willChangeValueForKey("employess", withSetMutation: NSKeyValueSetMutationKind.MinusSetMutation, usingObjects: employees) self.primitiveValueForKey("employees").minusSet(employees) self.didChangeValueForKey("employees", withSetMutation: NSKeyValueSetMutationKind.MinusSetMutation, usingObjects: employees) } }
æçµç¢ºèªã³ãŒããšæ®ãã®åé¡
æçµçã«ã次ã®èª¿æŽãè¡ãããŸããã
- éšéãšåŸæ¥å¡ã®å®å šãªãªã¹ããååŸããããã«ãCSDataManagerã¯ã©ã¹ã«2ã€ã®ã¡ãœãããè¿œå ãããŸãã
- ãµã³ãã«ã³ãŒãã®å€æŽ-ãªããžã§ã¯ãã®è¿œå ãåé€ãã«ã¹ã±ãŒãåé€ã®æ©èœã確èªããŸãã
ãããã£ãŠãçµæã®AppDelegateã³ãŒãïŒ
import UIKit import CoreData @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { //Get manager let manager = CSDataManager.sharedInstance() //Testing insert new objects let newDepartment : CSDepartment = NSEntityDescription.insertNewObjectForEntityForName("CSDepartment", inManagedObjectContext: manager.managedContext) as CSDepartment let newEmployee: CSEmployee = NSEntityDescription.insertNewObjectForEntityForName("CSEmployee", inManagedObjectContext: manager.managedContext) as CSEmployee let newEmployee2: CSEmployee = NSEntityDescription.insertNewObjectForEntityForName("CSEmployee", inManagedObjectContext: manager.managedContext) as CSEmployee newEmployee.department = newDepartment newDepartment.addEmployeesObject(newEmployee2) manager.saveContext() //Get and print all departments println("Have add oen department and two employees") println("Departments: \(manager.departments())") println("Employees: \(manager.employees())") //Testing remove child object newDepartment.removeEmployeesObject(newEmployee2) manager.saveContext() println("Have delete one employee") println("Departments: \(manager.departments())") //Testing cascade remove manager.managedContext.deleteObject(newDepartment) manager.saveContext() println("\nHave delete department") println("Departments: \(manager.departments())") println("Employees: \(manager.employees())") //Uncomment to remove all records // let departments = manager.departments() // for i in 0..departments.count{ // let dep = departments[i] as CSDepartment // manager.managedContext.deleteObject(dep) // } // let employees = manager.employees() // for i in 0..employees.count{ // let emp = employees[i] as CSEmployee // manager.managedContext.deleteObject(emp) // } // manager.saveContext() // println("\nHave delete all data") // println("Departments: \(manager.departments())") // println("Employees: \(manager.employees())") return true } }
é倧ãªãã°ãèŠã€ãããŸãã-ãªããžã§ã¯ãã®ã«ã¹ã±ãŒãåé€ã¯
removeEmployeesObject removeEmployees
çºçããŸãã
removeEmployeesObject removeEmployees
ã¡ãœãã
removeEmployeesObject removeEmployees
ã¡ãœããã䜿çšããŠéšéããåŸæ¥å¡ãåé€ããå Žåãéšéãžã®ãã€ã³ã¿ãŒã¯åãªããžã§ã¯ãã§ãªã»ãããããããããã£ãŠããªããžã§ã¯ãã¯ã¹ãã¬ãŒãžå ã®ã·ã¹ãã ã«ãã£ãŠãŸã çãŸããŠããŸãã
æŽæ°ïŒããã¯ãã°ã§ã¯ãããŸãã-Objective-Cã®ã³ãŒãã¯åãããã«åäœããäœããæ··ä¹±ãããŠããããã§ãã ã©ãããããããã®åé¡ã¯ã¢ãŒããã¯ãã£ã®ã¢ãããŒãã«ãã£ãŠè§£æ±ºããå¿ èŠããããŸãã
ãããã«
äžè¬çã«ãããªãã¯åãããšãã§ããŸãããéã®çã¿ãªãã§ã¯ã§ããŸããã ãã¡ã€ã«ã¯åžžã«æå ã«çœ®ããŠããå¿ èŠããããŸãã ãµã ã©ã€ã®æ¬åœã®éãæ¢ããŠãã³ã¡ã³ããä¿®æ£ãèªç±ãªè°è«ãã§ããã°å¬ããã§ãã