Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Advanced Realm in Swift
trippiece Inc.
@kitasuke
Table of contents
• Introducing Realm Swift
• How to deal with JSON
Before starting a presentation,
I would like to introduce my new
library
Flexible menu width mode
PagingEnabled mode
Fixed menu width mode
ScrollingEnabled mode
centerItem option
SegmentedControl mode
Usage
let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
v...
Customization
public class PagingMenuOptions {
public var defaultPage = 0
public var backgroundColor = UIColor.whiteColor(...
Welcome your feedback!
https://github.com/kitasuke/PagingMenuController
Back to Realm
Realm Swift released
A full-fledged, Swift-native API
Introducing Realm Swift
Available through Dynamic
Framework, CocoaPods and
Carthage
Documentation for Realm Swift
What's the difference between
Swift bridge from Objective-C
and
purely Swift?
Swift bridge
class Employee: RLMObject {
dynamic var name = "" // you can specify defaults
dynamic var startDate = NSDate(...
Swift bridge
let company = Company() // Using Realm Objects
company.name = "Realm Inc."
let realm = RLMRealm.defaultRealm(...
Cannot use full Swift features like
enum yet, but it's definitely
better than before
Dealing with JSON in Swift
Not sure exactly what kind of
values server respond
Might be Array, String, Int or nil
However, Realm doesn't accept nil
Make it Optional and Set default values
class Employee: Object {
dynamic var name: String? = ""
dynamic var startDate: NSD...
Use rawValue for enum
class Employee: Object {
dynamic var type: Int = Type.iOS.rawValue
enum Type: Int {
case iOS = 1
cas...
ignoredProperty could be useful
class Employee: Object {
var number: Int = 0
var name: String { // computed properties are...
That's for object mapping so far
Next one is the biggest problem
nil cannot be saved in Realm
If you save nil in Realm,
an exception will be thrown
Ideally, values should be saved,
but nil should be skipped
Use following methods with external data
• createInDefaultRealmWithValue:
• createInRealm: withValue:
• createOrUpdateInDe...
if not exists in Realm yet,
default value is used for key,
otherwise it's skipped
Use following methods with realm object
• addObject:
• addObjects:
• addOrUpdateObject:
• addOrUpdateObjectsFromArray:
• a...
Realm might support nil soon,
so keep it mind that
this is just a workaround
Any questions?
Advanced realm in swift
Upcoming SlideShare
Loading in …5
×

Advanced realm in swift

9,445 views

Published on

2015/05/16 #yidev

Published in: Engineering

Advanced realm in swift

  1. 1. Advanced Realm in Swift trippiece Inc. @kitasuke
  2. 2. Table of contents • Introducing Realm Swift • How to deal with JSON
  3. 3. Before starting a presentation, I would like to introduce my new library
  4. 4. Flexible menu width mode PagingEnabled mode
  5. 5. Fixed menu width mode ScrollingEnabled mode centerItem option
  6. 6. SegmentedControl mode
  7. 7. Usage let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as! ViewController viewController.title = "Menu title" let viewControllers = [viewController] let options = PagingMenuOptions() let pagingMenuController = self.childViewControllers.first as! PagingMenuController // there is a containerView having a childViewController in storyboard pagingMenuController.setup(viewControllers: viewControllers, options: options)
  8. 8. Customization public class PagingMenuOptions { public var defaultPage = 0 public var backgroundColor = UIColor.whiteColor() public var selectedBackgroundColor = UIColor.whiteColor() public var textColor = UIColor.lightGrayColor() public var selectedTextColor = UIColor.blackColor() public var font = UIFont.systemFontOfSize(16) public var menuHeight: CGFloat = 50 public var menuItemMargin: CGFloat = 20 public var animationDuration: NSTimeInterval = 0.3 public var menuDisplayMode = MenuDisplayMode.FlexibleItemWidth(centerItem: true, scrollingMode: MenuScrollingMode.PagingEnabled) public var menuItemMode = MenuItemMode.Underline(height: 3.0, color: UIColor.whiteColor(), selectedColor: UIColor.blueColor()) public enum MenuScrollingMode { case ScrollEnabled case ScrollEnabledAndBouces case PagingEnabled } public enum MenuDisplayMode { case FlexibleItemWidth(centerItem: Bool, scrollingMode: MenuScrollingMode) case FixedItemWidth(width: CGFloat, centerItem: Bool, scrollingMode: MenuScrollingMode) case SegmentedControl } public enum MenuItemMode { case None case Underline(height: CGFloat, color: UIColor, selectedColor: UIColor) case RoundRect } }
  9. 9. Welcome your feedback! https://github.com/kitasuke/PagingMenuController
  10. 10. Back to Realm
  11. 11. Realm Swift released A full-fledged, Swift-native API Introducing Realm Swift
  12. 12. Available through Dynamic Framework, CocoaPods and Carthage Documentation for Realm Swift
  13. 13. What's the difference between Swift bridge from Objective-C and purely Swift?
  14. 14. Swift bridge class Employee: RLMObject { dynamic var name = "" // you can specify defaults dynamic var startDate = NSDate() dynamic var salary = 0.0 dynamic var fullTime = true } class Company: RLMObject { dynamic var name = "" dynamic var ceo: Employee? // optional to-one relationship let employees = RLMArray(objectClassName: Employee.className()) // to-many relationship } purely Swift class Employee: Object { dynamic var name = "" // you can specify defaults dynamic var startDate = NSDate() dynamic var salary = 0.0 dynamic var fullTime = true } class Company: Object { dynamic var name = "" dynamic var ceo: Employee? // optional to-one relationship let employees = List<Employee>() // generic to-many relationship }
  15. 15. Swift bridge let company = Company() // Using Realm Objects company.name = "Realm Inc." let realm = RLMRealm.defaultRealm() // Default Realm realm.transactionWithBlock({ () -> Void in // Transactions realm.addObject(company) }) // Queries let company = Company.allObjects().firstObject() as! Company company.name // => "Realm Inc." (property access is type-safe) let ftJacks = Employee.objectsWhere("name = 'Jack'").objectsWhere("fullTime = true") // RLMResults purely Swift let company = Company() // Using Realm Objects company.name = "Realm Inc." let realm = Realm() // Default Realm realm.write { // Transactions realm.add(company) } // Queries let companies = realm.objects(Company) // => Results<Company> companies[0].name // => "Realm Inc." (property access is type-safe) // "Jack"s who work full time (lazily loaded & chainable) let ftJacks = realm.objects(Employee).filter("name = 'Jack'") .filter("fullTime = true")
  16. 16. Cannot use full Swift features like enum yet, but it's definitely better than before
  17. 17. Dealing with JSON in Swift
  18. 18. Not sure exactly what kind of values server respond
  19. 19. Might be Array, String, Int or nil
  20. 20. However, Realm doesn't accept nil
  21. 21. Make it Optional and Set default values class Employee: Object { dynamic var name: String? = "" dynamic var startDate: NSDate? = NSDate() dynamic var salary: double? = 0.0 dynamic var fullTime: Bool? = true }
  22. 22. Use rawValue for enum class Employee: Object { dynamic var type: Int = Type.iOS.rawValue enum Type: Int { case iOS = 1 case Android = 2 } }
  23. 23. ignoredProperty could be useful class Employee: Object { var number: Int = 0 var name: String { // computed properties are automatically ignored return "String" } override static func ignoredProperties() -> [String] { return ["number"] } }
  24. 24. That's for object mapping so far
  25. 25. Next one is the biggest problem
  26. 26. nil cannot be saved in Realm
  27. 27. If you save nil in Realm, an exception will be thrown
  28. 28. Ideally, values should be saved, but nil should be skipped
  29. 29. Use following methods with external data • createInDefaultRealmWithValue: • createInRealm: withValue: • createOrUpdateInDefaultRealmWithValue: • createOrUpdateInRealm: withValue: • create(type: T.Type, value: AnyObject = [:], update: Bool = false) in Realm Swift
  30. 30. if not exists in Realm yet, default value is used for key, otherwise it's skipped
  31. 31. Use following methods with realm object • addObject: • addObjects: • addOrUpdateObject: • addOrUpdateObjectsFromArray: • add(object: Object, update: Bool = false) in Realm Swift • add(objects: S, update: Bool = false) in Realm Swift
  32. 32. Realm might support nil soon, so keep it mind that this is just a workaround
  33. 33. Any questions?

×