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

MOSA Software Meeting 2015 Realm

MOSA Software Meeting 2015 Realm

Kishikawa Katsumi

November 18, 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. 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) } ⥂㶷
  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 realm.write { () in realm.add(article) } ⥂㶷3FBMN؎ٝأةٝأ׾《䖤
  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 realm.write { () in realm.add(article) } ⥂㶷ؔـآؙؑز׾欰䧭
  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 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 realm.write { () in realm.add(article) } ⥂㶷ر؍أؙח剅ֹ鴥׬
  14. 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
  15. 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
  16. 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 嗚稊
  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 嗚稊6*׾刿倜
  20. let comment = Comment() ... try! realm.write { () ->

    Void in article.comments.append(comment) } 刿倜
  21. let comment = realm.objectForPrimaryKey(Comment.self, key: commentID) ... try! realm.write {

    () -> Void 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 ˖ ِ٦ؠ٦ؚٕ٦ف  IUUQTXXXGBDFCPPLDPNHSPVQTSFBMNKQ ˖ ؟ه٦زثٍحز

     IUUQTMBDLSFBMNJP ˖ 5XJUUFS  IUUQTUXJUUFSDPNSFBMN+BQBO ˖ ى٦ز،حف  IUUQSFBMNDPOOQBTTDPN ⿫罋项俱