advanced realm in swift

34
Advanced Realm in Swift trippiece Inc. @kitasuke

Upload: yusuke-kita

Post on 05-Aug-2015

2.011 views

Category:

Engineering


11 download

TRANSCRIPT

Advanced Realm in Swifttrippiece 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

Usagelet viewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as! ViewControllerviewController.title = "Menu title"let viewControllers = [viewController]

let options = PagingMenuOptions()

let pagingMenuController = self.childViewControllers.first as! PagingMenuController // there is a containerView having a childViewController in storyboardpagingMenuController.setup(viewControllers: viewControllers, options: options)

Customizationpublic 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 }}

Welcome your feedback!https://github.com/kitasuke/PagingMenuController

Back to Realm

Realm Swift releasedA full-fledged, Swift-native API

Introducing Realm Swift

Available through Dynamic Framework, CocoaPods and

CarthageDocumentation for Realm Swift

What's the difference betweenSwift bridge from Objective-C

andpurely Swift?

Swift bridgeclass 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 Swiftclass 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}

Swift bridgelet company = Company() // Using Realm Objectscompany.name = "Realm Inc."

let realm = RLMRealm.defaultRealm() // Default Realmrealm.transactionWithBlock({ () -> Void in // Transactions realm.addObject(company)})

// Querieslet company = Company.allObjects().firstObject() as! Companycompany.name // => "Realm Inc." (property access is type-safe)let ftJacks = Employee.objectsWhere("name = 'Jack'").objectsWhere("fullTime = true") // RLMResults

purely Swiftlet company = Company() // Using Realm Objectscompany.name = "Realm Inc."

let realm = Realm() // Default Realmrealm.write { // Transactions realm.add(company)}

// Querieslet 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")

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: NSDate? = NSDate() dynamic var salary: double? = 0.0 dynamic var fullTime: Bool? = true}

Use rawValue for enum

class Employee: Object { dynamic var type: Int = Type.iOS.rawValue

enum Type: Int { case iOS = 1 case Android = 2 }}

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"] }}

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:

• createOrUpdateInDefaultRealmWithValue:

• createOrUpdateInRealm: withValue:

• create(type: T.Type, value: AnyObject = [:],

update: Bool = false) in Realm Swift

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:

• add(object: Object, update: Bool = false) in Realm Swift

• add(objects: S, update: Bool = false) in Realm Swift

Realm might support nil soon,so keep it mind that

this is just a workaround

Any questions?