swift - How to update SceneKit nodes to 'automatically' reflect underlying model? -


what sensible way of locating, updating, and/or adding or removing scenekit nodes correspond underlying model when model updated?

i'm not sure how best word question, it's easier have minimal example:

i have something, let's collection of coloured objects, might want either represent 2 dimensionally squares using say, quartz, or 3 dimensionally using scenekit. since underlying data same in both cases, seems more appropriate abstract away model , define this:

struct foo {     var uid: string     var color: uicolor     var position: [float] // array of 3 floats x, y, z } 

and have:

var collectionoffoo: [foo] 

and can construct scenekit scene looping on collectionoffoo , creating scnbox each 1 appropriate colour , position etc.

problem: user might add new foo collectionoffoo, or delete existing foo, or change 1 of existing foo's stored values, , scenekit scene has updated reflect that. destroying scene , rebuilding scratch seems wasteful , far far slow. might need called hundreds of times per second.

my current approach: give scenekit node corresponding given foo name matches foo's uid, , end manually searching node hierarchy using childnodewithname. however, doesn't scale (poor frame rates), , feels "wrong" if there's easier/more idiomatic way of doing this. i've been looking @ things reactivecocoa because seems solves sort of problem, i'm not sure if that's over-complicating things.

if using objective-c, might try creating foo class each instance holds pointer relevant scenekit node; wouldn't elegant @ least efficient , give desired results. i'm looking better way of doing , 1 works in swift.

suggestions welcome, , appreciated–i'm still working on first app , still consider myself beginner @ this.

i recommend having @ course stanford university , having @ mvc lectures: https://itunes.apple.com/gb/course/developing-ios-8-apps-swift/id961180099

they recommend using nsnotifications alert controller (which in case scnscene) of changes in model. controller goes , makes changes view (or scnnodes in case). here's example:

1. happens in model - new foo created! therefore, post nsnotification:

let notificationcenter = nsnotificationcenter.defaultcenter() notificationcenter.postnotificationname("foowascreatednotification",                                         object: nil,                                         userinfo: ["foo": fooinstance]) 

(to able pass instance of foo here, foo needs class because structs cannot added userinfo)

2. in scnscene (or whatever else you're using), receive nsnotification , update:

func didrevievenewfoonotification(notification: nsnotification!) {     if let info = notification.userinfo as? dictionary<string,anyobject>,        let foo  = info["foo"] as? foo {          //  create new node using foo's properties...         //  , add array:         collectionoffoo.append(foo)     } } 

using nsnotification makes model portable since nsnotification blind communication. therefore controller , nodes can whatever want without having change model.

hope helps.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -