UIKonf App Architecture
&
Data Oriented Design
+------------+ +----------------+
| | | |
| Objects | | Data |
+------------+ +----------------+
| Talk | | Title |
| Spea...
Time slot
[
{
"t_id": "startDay1",
"startTime": "18-09-00",
"endTime": "18-10-00",
"description": "Check in first day",
"l...
Organizer
...
{
"name": "Maxim Zaks",
"twitter": "@iceX33",
"bio": "Software developer with a history in IDE development,
...
Speaker
...
{
"name": "Graham Lee",
"twitter": "@iwasleeg",
"bio": "Graham Lee works at Facebook, where he helps people ma...
Talk
...
{
"title": "World Modeling",
"speaker_name": "Mike Lee",
"t_id": "session1Day1",
"t_index": 1
},
...
Location
...
{
"name": "Heimathafen Neukölln",
"address": "Karl-Marx-Str. 141, 12043 Berlin",
"description": "Conference v...
Import Data from JSON Array
for item in jsonArray {
let entity = context.createEntity()
for pair in (item as! NSDictionary...
What is a context?
It's a managing data structure
public class Context {
public func createEntity() -> Entity
public func destroyEntity(entity : Entity)
public func entityG...
What's an entity?
Bag of components
Entity
public class Entity {
public func set(c:Component, overwrite:Bool = false)
public func get<C:Component>(ct:C.Type) ...
What's a component
It's just data (value object)
Components
struct NameComponent : Component, DebugPrintable {
let name : String
var debugDescription: String{
return "[(na...
What's a component
It also can be just a flag
struct OrganizerComponent : Component {}
Import Data from JSON Array
for item in jsonArray {
let entity = context.createEntity()
for pair in (item as! NSDictionary...
Converters dictionary
typealias Converter = (JsonValue) -> Component
let converters : [String : Converter] = [
"t_id" : {
...
How does it work with
UIKit
override func viewDidLoad() {
super.viewDidLoad()
groupOfEvents = context.entityGroup(
Matcher.Any(StartTimeComponent, End...
What's a group?
Subset of Entites
Entity
public class Group : SequenceType {
public var count : Int
public var sortedEntities: [Entity]
public func addObser...
override func viewDidLoad() {
super.viewDidLoad()
groupOfEvents = context.entityGroup(
Matcher.Any(StartTimeComponent, End...
extension TimeLineViewController : GroupObserver {
func entityAdded(entity : Entity) {
if entity.has(RatingComponent){
upd...
private lazy var reload :
dispatch_block_t = dispatch_debounce_block(0.1) {
...
}
override func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let sec...
protocol EntityCell {
func updateWithEntity(entity : Entity, context : Context)
}
class LocationCell: UITableViewCell, Ent...
struct PhotoComponent : Component, DebugPrintable {
let url : NSURL
let image : UIImage
let loaded : Bool
var debugDescrip...
Data:
{
...
"photo": "http://www.uikonf.com/static/images/maxim-zaks.png",
...
}
Converter:
"photo" : {
PhotoComponent(url...
func setPhoto() {
let photoComponent = entity!.get(PhotoComponent)!
imageView.image = photoComponent.image
if !photoCompon...
var detachedPerson = entity!.detach
cancelLoadingPhoto =
dispatch_after_cancellable(
0.5, dispatch_get_global_queue(QOS_CL...
What's a detached Entity?
It's an Entity implemented as a struct
with a sync method
var detachedPerson = entity!.detach
cancelLoadingPhoto =
dispatch_after_cancellable(
0.5, dispatch_get_global_queue(QOS_CL...
Recap
—Context is a managing Data Structure
—Entity is a bag of components
—Components are just value types
—Groups are su...
Tanks you
Maxim - @iceX33
Links
—UIKonf App on Github
—Blog: Think different about Data Model
—Blog: What is an entity framework
—Book: Data oriented...
UIKonf App & Data Driven Design @swift.berlin
UIKonf App & Data Driven Design @swift.berlin
UIKonf App & Data Driven Design @swift.berlin
UIKonf App & Data Driven Design @swift.berlin
UIKonf App & Data Driven Design @swift.berlin
UIKonf App & Data Driven Design @swift.berlin
UIKonf App & Data Driven Design @swift.berlin
UIKonf App & Data Driven Design @swift.berlin
UIKonf App & Data Driven Design @swift.berlin
Nächste SlideShare
Wird geladen in …5
×

UIKonf App & Data Driven Design @swift.berlin

602 Aufrufe

Veröffentlicht am

I explain how I build a conference app using a small entity system library written in swift.

Veröffentlicht in: Ingenieurwesen
0 Kommentare
0 Gefällt mir
Statistik
Notizen
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

Keine Downloads
Aufrufe
Aufrufe insgesamt
602
Auf SlideShare
0
Aus Einbettungen
0
Anzahl an Einbettungen
3
Aktionen
Geteilt
0
Downloads
2
Kommentare
0
Gefällt mir
0
Einbettungen 0
Keine Einbettungen

Keine Notizen für die Folie

UIKonf App & Data Driven Design @swift.berlin

  1. 1. UIKonf App Architecture & Data Oriented Design
  2. 2. +------------+ +----------------+ | | | | | Objects | | Data | +------------+ +----------------+ | Talk | | Title | | Speaker | | Description | | Organizer | | StartTime | | Volunteer | | EndTime | | Venue | | Name | | Break | | TwitterHandle | | Workshop | | PhotoURL | | ... | | ... | | | | | +------------+ +----------------+
  3. 3. Time slot [ { "t_id": "startDay1", "startTime": "18-09-00", "endTime": "18-10-00", "description": "Check in first day", "locations": [ "Heimathafen Neukölln" ] }, ...
  4. 4. Organizer ... { "name": "Maxim Zaks", "twitter": "@iceX33", "bio": "Software developer with a history in IDE development, Web development and even Enterprise Java development (He was young and under bad influence). Nowadays working as a game developer (preferably iOS). Regular visitor and occasional speaker at conferences.", "photo": "http://www.uikonf.com/static/images/maxim-zaks.png", "organizer": true }, ...
  5. 5. Speaker ... { "name": "Graham Lee", "twitter": "@iwasleeg", "bio": "Graham Lee works at Facebook, where he helps people make better tests so they can help people make better software. In the past he worked with some other people, and has written books and blogs so he can work with people he hasn't met too. His blog is at sicpers.info.", "photo": "http://www.uikonf.com/static/images/Graham-Lee.png" }, ...
  6. 6. Talk ... { "title": "World Modeling", "speaker_name": "Mike Lee", "t_id": "session1Day1", "t_index": 1 }, ...
  7. 7. Location ... { "name": "Heimathafen Neukölln", "address": "Karl-Marx-Str. 141, 12043 Berlin", "description": "Conference venue" }, ...
  8. 8. Import Data from JSON Array for item in jsonArray { let entity = context.createEntity() for pair in (item as! NSDictionary) { let (key,value) = (pair.key as! String, pair.value as! JsonValue) let component = converters[key]!(value) entity.set(component) } }
  9. 9. What is a context? It's a managing data structure
  10. 10. public class Context { public func createEntity() -> Entity public func destroyEntity(entity : Entity) public func entityGroup(matcher : Matcher) -> Group }
  11. 11. What's an entity? Bag of components
  12. 12. Entity public class Entity { public func set(c:Component, overwrite:Bool = false) public func get<C:Component>(ct:C.Type) -> C? public func has<C:Component>(ct:C.Type) -> Bool public func remove<C:Component>(ct:C.Type) }
  13. 13. What's a component It's just data (value object)
  14. 14. Components struct NameComponent : Component, DebugPrintable { let name : String var debugDescription: String{ return "[(name)]" } }
  15. 15. What's a component It also can be just a flag
  16. 16. struct OrganizerComponent : Component {}
  17. 17. Import Data from JSON Array for item in jsonArray { let entity = context.createEntity() for pair in (item as! NSDictionary) { let (key,value) = (pair.key as! String, pair.value as! JsonValue) let component = converters[key]!(value) entity.set(component) } }
  18. 18. Converters dictionary typealias Converter = (JsonValue) -> Component let converters : [String : Converter] = [ "t_id" : { TimeSlotIdComponent(id: $0 as! String) }, "t_index" : { TimeSlotIndexComponent(index: $0 as! Int) }, ...
  19. 19. How does it work with UIKit
  20. 20. override func viewDidLoad() { super.viewDidLoad() groupOfEvents = context.entityGroup( Matcher.Any(StartTimeComponent, EndTimeComponent)) setNavigationTitleFont() groupOfEvents.addObserver(self) context.entityGroup( Matcher.All(RatingComponent)).addObserver(self) readDataIntoContext(context) syncData(context) }
  21. 21. What's a group? Subset of Entites
  22. 22. Entity public class Group : SequenceType { public var count : Int public var sortedEntities: [Entity] public func addObserver(observer : GroupObserver) public func removeObserver(observer : GroupObserver) }
  23. 23. override func viewDidLoad() { super.viewDidLoad() groupOfEvents = context.entityGroup( Matcher.Any(StartTimeComponent, EndTimeComponent)) setNavigationTitleFont() groupOfEvents.addObserver(self) context.entityGroup( Matcher.All(RatingComponent)).addObserver(self) readDataIntoContext(context) syncData(context) }
  24. 24. extension TimeLineViewController : GroupObserver { func entityAdded(entity : Entity) { if entity.has(RatingComponent){ updateSendButton() } else { reload() } } func entityRemoved(entity : Entity, withRemovedComponent removedComponent : Component) { if removedComponent is RatingComponent { return } reload() } }
  25. 25. private lazy var reload : dispatch_block_t = dispatch_debounce_block(0.1) { ... }
  26. 26. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let sectionName = sectionNameTable[indexPath.section]()! let cellIdentifier = cellIdTable[sectionName]! let cell = tableView.dequeueReusableCellWithIdentifier( cellIdentifier, forIndexPath: indexPath) as! EntityCell let cellEntity = cellEntityTable[sectionName]!(indexPath.row) cell.updateWithEntity(cellEntity, context: context) return cell as! UITableViewCell }
  27. 27. protocol EntityCell { func updateWithEntity(entity : Entity, context : Context) } class LocationCell: UITableViewCell, EntityCell { @IBOutlet weak var nameLabel: UILabel! @IBOutlet weak var descriptionLabel: UITextView! func updateWithEntity(entity : Entity, context : Context){ nameLabel.text = entity.get(NameComponent)!.name let descriptionText = entity.get(DescriptionComponent)?.description let address = entity.get(AddressComponent)!.address descriptionLabel.text = descriptionText != nil ? descriptionText! + "n" + address : address } }
  28. 28. struct PhotoComponent : Component, DebugPrintable { let url : NSURL let image : UIImage let loaded : Bool var debugDescription: String{ return "[(url), loaded: (loaded)]" } }
  29. 29. Data: { ... "photo": "http://www.uikonf.com/static/images/maxim-zaks.png", ... } Converter: "photo" : { PhotoComponent(url: NSURL(string:$0 as! String)!, image : UIImage(named:"person-icon")!, loaded: false) },
  30. 30. func setPhoto() { let photoComponent = entity!.get(PhotoComponent)! imageView.image = photoComponent.image if !photoComponent.loaded { ... } }
  31. 31. var detachedPerson = entity!.detach cancelLoadingPhoto = dispatch_after_cancellable( 0.5, dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)) { if let data = NSData(contentsOfURL: photoComponent.url), let image = UIImage(data: data) { let photoComponent = detachedPerson.get(PhotoComponent)! detachedPerson.set( PhotoComponent(url: photoComponent.url, image:image, loaded:true), overwrite: true ) detachedPerson.sync() } }
  32. 32. What's a detached Entity? It's an Entity implemented as a struct with a sync method
  33. 33. var detachedPerson = entity!.detach cancelLoadingPhoto = dispatch_after_cancellable( 0.5, dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)) { if let data = NSData(contentsOfURL: photoComponent.url), let image = UIImage(data: data) { let photoComponent = detachedPerson.get(PhotoComponent)! detachedPerson.set( PhotoComponent(url: photoComponent.url, image:image, loaded:true), overwrite: true ) detachedPerson.sync() } }
  34. 34. Recap —Context is a managing Data Structure —Entity is a bag of components —Components are just value types —Groups are subsets on the components —You can observe groups -> KVO like behavior —Use detached entity if you want to go on another queue
  35. 35. Tanks you Maxim - @iceX33
  36. 36. Links —UIKonf App on Github —Blog: Think different about Data Model —Blog: What is an entity framework —Book: Data oriented Design (C++ heavy) —Talk: Data-Oriented Design and C++ (hardcore)

×