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

OSC Tokushima 2015 Realm

OSC Tokushima 2015 Realm

Kishikawa Katsumi

November 14, 2015
Tweet

More Decks by Kishikawa Katsumi

Other Decks in Programming

Transcript

  1. class Article: Object { dynamic var id: Int = 0

    dynamic var title: String = "" dynamic var contents: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var modificationDate: NSDate = NSDate() dynamic var deletionDate: NSDate? = nil dynamic var user: User? = nil let comments = List<Comment>() } وٕثفٓحزؿؓ٦ي J04
  2. public class Article extends RealmObject { @PrimaryKey private int id;

    private String title; private String contents; private Date creationDate; private Date modificationDate; private Date deletionDate; private User user; private RealmList<Comment> user; } وٕثفٓحزؿؓ٦ي "OESPJE
  3. let realm = try! Realm() let article = Article() article.title

    = titleTextfield.text article.contents = contentTextfield.text realm.write { () in realm.add(article) } وٕثفٓحزؿؓ٦ي J04
  4. Realm realm = Realm.getInstance(this); realm.beginTransaction(); Article article = realm.createObject(Article.class); article.setText(titleTextfield.getText());

    article.setContents(contentTextfield.getText()); realm.commitTransaction(); وٕثفٓحزؿؓ٦ي "OESPJE
  5. $ pod install Updating local specs repositories Analyzing dependencies Downloading

    dependencies Installing Realm (0.93.2) Installing RealmSwift (0.93.2) Generating Pods project Integrating client project [!] Please close any current Xcode sessions and use `SchooApp.xcworkspace` for this project from now on. Sending stats $ pod install Λ࣮ߦ͢Δ
  6. import RealmSwift class Article: Object { dynamic var id: Int

    = 0 dynamic var title: String = "" dynamic var contents: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var modificationDate: NSDate = NSDate() dynamic var deletionDate: NSDate? = nil dynamic var user: User? = nil let comments = List<Comment>() } class Comment: Object { dynamic var id: Int = 0 dynamic var contents: String = "" dynamic var user: User? = nil } class User: Object { dynamic var id: Int = 0 dynamic var name: String = "" } ٌرٕך㹀纏
  7. import RealmSwift class Article: Object { dynamic var id: Int

    = 0 dynamic var title: String = "" dynamic var contents: String = "" dynamic var creationDate: NSDate = NSDate() dynamic var modificationDate: NSDate = NSDate() dynamic var deletionDate: NSDate? = nil dynamic var user: User? = nil let comments = List<Comment>() } class Comment: Object { dynamic var id: Int = 0 dynamic var contents: String = "" dynamic var user: User? = nil } class User: Object { dynamic var id: Int = 0 dynamic var name: String = "" } ٌرٕך㹀纏
  8. import CoreData class Article: NSManagedObject { @NSManaged var id: NSNumber

    @NSManaged var title: String @NSManaged var contents: String @NSManaged var creationDate: NSDate @NSManaged var modificationDate: NSDate @NSManaged var deletionDate: NSDate? @NSManaged var user: User @NSManaged var comments: NSSet? } class User: NSManagedObject { @NSManaged var id: NSNumber @NSManaged var name: String } class Comment: NSManagedObject { @NSManaged var id: NSNumber @NSManaged var contents: String @NSManaged var user: User } ٌرٕך㹀纏$PSF%BUB
  9. CREATE TABLE IF NOT EXISTS article ( id INTEGER, title

    TEXT, contents TEXT, creationCate DATE, modificationCate DATE, deletionCate DATE ) CREATE TABLE IF NOT EXISTS user ( id INTEGER, name TEXT ) CREATE TABLE IF NOT EXISTS comment ( id INTEGER, contents TEXT, ) ٌرٕך㹀纏42-JUF
  10. let realm = try! Realm() let article = Article() article.title

    = titleTextfield.text article.contents = contentTextfield.text let currentUser = realm.objectForPrimaryKey(User.self, key: userID) article.user = currentUser try! realm.write { in realm.add(article) } ⥂㶷
  11. let realm = try! Realm() let article = Article() article.title

    = titleTextfield.text article.contents = contentTextfield.text let currentUser = realm.objectForPrimaryKey(User.self, key: userID) article.user = currentUser try! realm.write { in realm.add(article) } ⥂㶷3FBMN؎ٝأةٝأ׾《䖤
  12. let realm = try! Realm() let article = Article() article.title

    = titleTextfield.text article.contents = contentTextfield.text let currentUser = realm.objectForPrimaryKey(User.self, key: userID) article.user = currentUser try! realm.write { in realm.add(article) } ⥂㶷ؔـآؙؑز׾欰䧭
  13. let realm = try! Realm() let article = Article() article.title

    = titleTextfield.text article.contents = contentTextfield.text let currentUser = realm.objectForPrimaryKey(User.self, key: userID) article.user = currentUser try! realm.write { in realm.add(article) } ⥂㶷ꟼ鸬ؔـآؙؑز׾《䖤
  14. let realm = try! Realm() let article = Article() article.title

    = titleTextfield.text article.contents = contentTextfield.text let currentUser = realm.objectForPrimaryKey(User.self, key: userID) article.user = currentUser realm.write { () in realm.add(article) } ⥂㶷ر؍أؙח剅ֹ鴥׬
  15. let article = NSEntityDescription .insertNewObjectForEntityForName("Article", inManagedObjectContext: managedObjectContext) article.title = titleTextfield.text

    article.contents = contentTextfield.text let fetchRequest = NSFetchRequest(entityName: "User") fetchRequest.predicate = NSPredicate(format: "id = %@", userID) if let currentUser = managedObjectContext.executeFetchRequest(fetchRequest).first { article.user = currentUser } managedObjectContext.save() ⥂㶷$PSF%BUB
  16. lazy var managedObjectModel: NSManagedObjectModel = { let modelURL = NSBundle.mainBundle().URLForResource("Data",

    withExtension: "momd")! return NSManagedObjectModel(contentsOfURL: modelURL)! }() lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Data.sqlite") var failureReason = "There was an error creating or loading the application's saved data." do { try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil) } catch { abort() } return coordinator }() lazy var managedObjectContext: NSManagedObjectContext = { let coordinator = self.persistentStoreCoordinator var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) managedObjectContext.persistentStoreCoordinator = coordinator return managedObjectContext }() /4.BOBHFE0CKFDU$POUFYUך《䖤$PSF%BUB
  17. let realm = try! Realm() let currentUser = realm.objectForPrimaryKey(User.self, key:

    userID) let articles = realm.objects(Article).filter("user = %@", currentUser) let article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name 嗚稊
  18. let realm = try! Realm() let currentUser = realm.objectForPrimaryKey(User.self, key:

    userID) let articles = realm.objects(Article).filter("user = %@", currentUser) let article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name 嗚稊فٓ؎ؤؗ٦ד嗚稊
  19. let realm = try! Realm() let currentUser = realm.objectForPrimaryKey(User.self, key:

    userID) let articles = realm.objects(Article).filter("user = %@", currentUser) let article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name 嗚稊勴⟝ד嗚稊
  20. let realm = try! Realm() let currentUser = realm.objectForPrimaryKey(User.self, key:

    userID) let articles = realm.objects(Article).filter("user = %@", currentUser) let article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name 嗚稊6*׾刿倜
  21. let comment = realm.objectForPrimaryKey(Comment.self, key: commentID) ... try! realm.write {

    in realm.comments.removeAtIndex(realm.comments.indexOf(comment)) realm.delete(comment) } ⵴ꤐ
  22. let realm = try! Realm() let currentUser = realm.objectForPrimaryKey(User.self, key:

    userID) let articles = realm.objects(Article).filter("user = %@", currentUser) let article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name 䗡䎿׃׋鹼䒀ٗ٦س
  23. let realm = try! Realm() let currentUser = realm.objectForPrimaryKey(User.self, key:

    userID) let articles = realm.objects(Article) .filter("user = %@", currentUser) let article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name 䗡䎿׃׋鹼䒀ٗ٦س 㹋ꥷךر٦ةכ《䖤ׁ׸זְ 㹋ꥷךر٦ةכ《䖤ׁ׸זְ 㹋ꥷךر٦ةכ《䖤ׁ׸זְ UJUMFؕٓيךر٦ةչ׌ֽպ《䖤
  24. let realm = try! Realm() let currentUser = realm.objectForPrimaryKey(User.self, key:

    userID) let articles = realm.objects(Article) .filter("user = %@", currentUser) let article = articles[indexPath.row] cell.titleTextLabel.text = article.title cell.contentTextLabel.text = article.contents cell.userNameLabel.text = article.user.name ئٗ٥؝ؾ٦ 㹋ꥷךر٦ةכ《䖤ׁ׸זְ 㹋ꥷךر٦ةכ《䖤ׁ׸זְ 㹋ꥷךر٦ةכ《䖤ׁ׸זְ UJUMFؕٓيךر٦ةչ׌ֽպ《䖤
  25. $PSF%BUB J1IPOFD ˖ Ⰻ⟝《䖤 ˖ ا٦زז׃T ˖ ا٦ز֮׶T ˖ $0/5"*/4嗚稊

    ˖ ا٦زז׃T ˖ ا٦ز֮׶T ˖ ًٌٔ⢪欽ꆀ ˖ .#
  26. 3FBMN J1IPOFD ˖ Ⰻ⟝《䖤 ˖ ا٦زז׃T ˖ ا٦ز֮׶T ˖ $0/5"*/4嗚稊

    ˖ ا٦زז׃T ˖ ا٦ز֮׶T ˖ ًٌٔ⢪欽ꆀ ˖ .#
  27. Realm let cell = tableView .dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell let address

    = addresses[indexPath.row] postalCode.insertString("-", atIndex: 3) cell.textLabel.text = address.postalcode cell.detailTextLabel.text = address.prefecture ⦼ח،ؙإأ׃׋הֹח ⴱ׭ג㹋ꥷךر٦ة׾《䖤ׅ׷ ֿך儗挿ד׮ת׌ ر٦ةכ《䖤׃זְ
  28. ˖ Ⱅ䒭8FC؟؎ز"  IUUQTSFBMNJPKQ ˖ (JU)VC  IUUQTHJUIVCDPNSFBMNSFBMNDPDPB  IUUQTHJUIVCDPNSFBMNSFBMNKBWB

    ˖ ِ٦ؠ٦ؚٕ٦ف"  IUUQTXXXGBDFCPPLDPNHSPVQTSFBMNKQ ˖ ؟ه٦زثٍحز"  IUUQTMBDLSFBMNJP ˖ 5XJUUFS"  IUUQTUXJUUFSDPNSFBMN+BQBO ˖ ى٦ز،حف"  IUUQSFBMNDPOOQBTTDPN ⿫罋项俱