Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift+RealmでMacアプリケーション

Econa77
July 24, 2015

 Swift+RealmでMacアプリケーション

Econa77

July 24, 2015
Tweet

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ ݹྛ ढ़༞ גࣜձࣾυϫϯΰ Github : http://github.com/econa77 Twitter : http://twitter.com/econa77

    աڈ࡞ͬͨ΋ͷ REVENTIVE, Inc. Modelor, Inc. ResuPress, Inc. Close Presser Coincheck
  2. RLMResult RLMResult͸ৗʹ࠷৽ͷঢ়ଶ͕อͨΕ͍ͯΔ FetchedResultsControllerͱಉ͡Α͏ͳ࢖͍ํ͕Ͱ͖ΔͷͰɺTableviewͷ DataSourceͳͲʹ࢖༻͢Δͱඇৗʹศར ɹˠ σʔλ͕มߋ͞Εͯ΋reloadData()ΛݺͿ͚ͩͰߋ৽͕͔͔Δ private var clips: RLMResults?

    self.clips = CPYClip.allObjects().sortedResultsUsingProperty("updateTime", ascending: false) self.reloadData() let clips = CPYClip.allObjects().sortedResultsUsingProperty("updateTime", ascending: false)
  3. RealmͷΦϒδΣΫτݕࡧ͸̎छྨ͋ΔʢSwift͔ΒObjC൛Λ࢖༻͢Δ৔߹ͷΈʣ objectsWhereͷ಺෦࣮૷ ฦΓ஋ͷRLMResults͕Φϓγϣφϧͳͷ͔ɺͦ͏Ͱͳ͍ͷ͔ͷҧ͍ ݕࡧ objectsWithPredicate:(NSPredicate *)predicate objectsWhere(predicateFormat: String, _ args:

    CVarArgType...) public class func objectsWhere(predicateFormat: String, _ args: CVarArgType...) -> RLMResults { return objectsWithPredicate(NSPredicate(format: predicateFormat, arguments: getVaList(args))) } objectsWithPredicate:(NSPredicate *)predicate ɹɹɹɹɹɹɹɹɹ -> RLMResult? objectsWhere(predicateFormat: String, _ args: CVarArgType…) ɹ-> RLMResult
  4. ϦϨʔγϣφϧ class CPYFolder: RLMObject { // MARK: - Properties dynamic

    var index = 0 dynamic var enable = true dynamic var title = "" dynamic var snippets = RLMArray(objectClassName: CPYSnippet.className()) } class CPYSnippet: RLMObject { // MARK: - Properties dynamic var index = 0 dynamic var enable = true dynamic var title = "" dynamic var content = "" dynamic var folder: CPYFolder? { return linkingObjectsOfClass("CPYFolder", forProperty: "snippets").first as? CPYFolder } }
  5. RLMResults͕ৗʹ࠷৽ͷ஋Λ͍࣋ͬͯΔͨΊforͰ࡟আ΍ߋ৽Λߦ͏ͱॱ൪͕ͣ ΕΔ ࡟আɾߋ৽ internal func removeFolders() { let realm =

    RLMRealm.defaultRealm() let folders = CPYFolder.allObjects() for folder in folders { realm.transactionWithBlock({ () -> Void in realm.deleteObjects((folder as! CPYFolder).snippets) realm.deleteObject(folder) }) } }
  6. RLMResults͕ৗʹ࠷৽ͷ஋Λ͍࣋ͬͯΔͨΊforͰ࡟আ΍ߋ৽Λߦ͏ͱॱ൪͕ͣ ΕΔ ࡟আɾߋ৽ internal func removeFolders() { let realm =

    RLMRealm.defaultRealm() let folders = CPYFolder.allObjects() for folder in folders { realm.transactionWithBlock({ () -> Void in realm.deleteObjects((folder as! CPYFolder).snippets) }) } realm.transactionWithBlock { () -> Void in realm.deleteObjects(folders) } }
  7. RealmͰͰ͖Δ͚ͩ៉ྷͳEnum Enum class CPYClip: RLMObject { // MARK: - Properties

    enum DataStatus: String { case Status1 = "status1" case Status2 = "status2" } dynamic var localStatus = DataStatus.Status1.rawValue internal var status: DataStatus { get { return DataStatus(rawValue: self.localStatus) ?? .Status1 } set { self.localStatus = newValue.rawValue } } }
  8. DBϑΝΠϧͷ҉߸ԽʹରԠ͍ͯ͠Δ iOS৔߹Ͱ΋DBϑΝΠϧ͕Ӿཡ͞ΕΔ৔߹͸͋ΔͷͰɺϢʔβͷ৘ใͳͲɺݟΒ Εͯ͸͍͚ͳ͍σʔλΛอଘ͢Δ৔߹͸҉߸ԽΛߦ͏ ҉߸Խ MacΞϓϦ͸୭Ͱ΋ӾཡՄೳͳҐஔʹDBϑΝΠϧ͕ஔ͔ΕΔͷͰͰ͖Ε͹҉߸Խ ͓ͯ͘͠ NSMutableData *key = [NSMutableData

    dataWithLength:64]; SecRandomCopyBytes(kSecRandomDefault, key.length, (uint8_t *)key.mutableBytes); [RLMRealm setEncryptionKey:key forRealmsAtPath:RLMRealm.defaultRealmPath]; RLMResults *dogs = [[Dog objectsWhere:@"name contains 'Fido'"]];
  9. NSOutlineViewͷDataSourceͱͯ͠ RealmObjectΛ࢖͑ͳ͍ NSOutlineView(ݕূத) func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) ->

    Int { if item == nil { return Int(self.folders!.count) } else if let folder = item as? CPYFolder { return Int(folder.snippets.count) } return 0 } NSOutlineViewॳΊͯ࢖͏ͷͰ࢖͍ํ͕ؒҧͬͯ ͍Δ͚͔ͩ΋͠Ε·ͤΜ
  10. ΦϒδΣΫτʹΧελϜInitializerͷఆٛ Custom Initializer override init() { super.init() } init(title: String)

    { self.title = title super.init() } // MARK: Primary Key override class func primaryKey() -> String { return "dataHash" } CPYClip(forPrimaryKey: "") forPrimaryKeyϝιου͕ݺ΂ͳ͍
  11. NSDate͸ϛϦඵؙ͕ΊΒΕΔ αʔόଆͱͷ௨৴΋ߟ͑Δͱ࢓༷Λ߹Θ͓͖͍ͤͯͨ NSDate let unixTime = NSDate().timeIntervalSince1970 let clip =

    CPYClip() clip.dataPath = path clip.title = title clip.dataHash = String(hash) clip.updateTime = unixTime clip.primaryType = clipData.primaryType ?? "" RealmͰ࣌ؒΛѻ͏ͱ͖͸શͯUNIXTimeͰѻ͍౎౓ม׵
  12. DeleteRule͕ઃఆͰ͖ͳ͍ͨΊɺ਌Λ࡟আ͢Δ࣌͸ɺࣗ෼Ͱ࡟আ͢Δɻ Delete Rule internal func removeFolders() { let realm =

    RLMRealm.defaultRealm() var folders = CPYFolder.allObjects() for folder in folders { realm.transactionWithBlock({ () -> Void in realm.deleteObjects((folder as! CPYFolder).snippets) }) } realm.transactionWithBlock { () -> Void in realm.deleteObjects(folders) } }