26 August 2015

Techniques after using Swift for a month

At work, I was put on an iOS/iPad app project a little while ago. I've never written anything in the iOS ecosystem so I spent about a week to research before digging in.The transition was smooth primarily because I'm using Swift--a much more succinct and modern language than Objective-C--and my somewhat dated Android experience and very dated Windows dev experience provided me with all of the patterns I needed to understand iOS. Also, coming off of Groovy development, Swift's syntactic sugar was immediately understandable.

I came up with two small techniques for the project. One helps with data persistence using Core Data with models and entity managers, a la JPA. The other technique simplifies updating multiple views using the Notification Center with type safe additions. They've helped to speed development and--the thought of every developer who thinks they've discovered something new--some aspects aren't commonly known (which could be good or other).

My Core Data code starts with the NSManagedObject model generation. In a separate file, I create an extension class so that the model can always be re-created without wiping out my custom methods. I also create a class derived from a DAO utility class to implement all CRUD methods using generics. The DAO util:

class DaoUtil {
    
    var managedObjectContext: NSManagedObjectContext
    
    var managedObjectModel: NSManagedObjectModel
    
    var entityName: String
    
    init(managedObjectContext: NSManagedObjectContext, managedObjectModel: NSManagedObjectModel, entityName: String) {
        self.managedObjectContext = managedObjectContext
        self.managedObjectModel = managedObjectModel
        self.entityName = entityName
    }

    // ... create, find, count, and delete methods

}

The find, count, and delete methods come in an *All() version and a *(fetchRequestName: String) version. The implementation for a User model would be as follows:

extension User {

    // ...

}

class UserDao: DaoUtil {
    
    let EntityName = "User"
    
    init(managedObjectContext: NSManagedObjectContext, managedObjectModel: NSManagedObjectModel) {
        super.init(managedObjectContext: managedObjectContext, managedObjectModel: managedObjectModel, entityName: EntityName)
    }
    
    // ... custom methods

}

This design favors the data mapper pattern over the active record pattern, which I think is more common in iOS. I prefer a separation of data object and persistence.

The other technique is a type-safe wrapper around the notification center. iOS's notification center calls reminded me of Windows SendMessage (and, with dispatch queues, PostMessage). One potential issue with these is the lack of type safety since you're effectively passing around a void pointer. Using Swift's generics again allows us to create small, typed versions based on a named string constant, an event (e,g, BeforeUpdate or AfterDelete), and an object type.

The core of the implementation:

    private func observe(id: String, event: Event?, notify: (observable: T?) -> Void) -> NSObjectProtocol {
        var observer = notificationCenter.addObserverForName(id, object: event, queue: NSOperationQueue.mainQueue()) { notification in
            if let userInfo = notification.userInfo, observable = userInfo[id] as? T {
                notify(observable: observable)
            } else {
                notify(observable: nil)
            }
        }
        return observer
    }
    
    private func notify(id: String, event: Event?, observable: T?) {
        if let observable = observable {
            notificationCenter.postNotificationName(id, object: event, userInfo: [id: observable])
        } else {
            notificationCenter.postNotificationName(id, object: event)
        }
    }
    
    func observeUser(event: Event?, notify: (equipment: User?) -> Void) -> NSObjectProtocol {
        return observe(NotificationService.Name.User, event: event, notify: notify)
    }
    
    func notifyUser(event: Event?, user: User?) {
        notify(NotificationService.Name.User, event: event, observable: user)
    }

With this, I can trace publishers and subscribers by type and event, but still take advantage of the built-in message queue. Still much to learn ...

posted by sstrader at 11:41 PM in Programming | comments (0) | permalink

23 August 2015

The children of Infinite Jest

Listening to dialog from the movie The End of the Tour (which we will probably go see tonight), spoken by David Foster Wallace:

So look, as the internet grows in the next 10, 15 years, and virtual reality pornography becomes a reality ... we're gonna have to develop some real machinery inside our guts to turn off pure, unalloyed pleasure or I don't know about you, I'm gonna have to leave the planet. ... 'Cause the technology is just gonna get better and better and it's gonna get easier and easier and more and more convenient and more and more pleasurable to sit alone with images on a screen given to us by people who do not love us but want our money and that's fine in low doses but if it's the basic main staple of your diet you're gonna die.

Taken from the On The Media interview that Brooke Gladstone had with David Lipsky 'We've Sort of Become Friends': The Original Tapes from David Foster Wallace's '96 Book Tour (transcript here).

This fear is examined and realized in Peter Watts' book Echopraxia in several forms: biological sex has become non-existent, relationships are created by altering each others' personalities to be more compatible, a large percentage of the human population lives only hooked in to a virtual world, etc. He suggests that our basic desire to eliminate pain will inevitably lead us to become not-human. Our toolmaking will destroy us. DFW worried in a similar manner concerning drug addiction and pleasure-seeking. The idea of a rat continually pressing a button for pleasure until it dies is unnerving. When I had written about Echopraxia, I recognized the connection to works by several other SF writers but hadn't considered how deeply these themes are encoded in Infinite Jest until I heard the quote above.

posted by sstrader at 3:48 PM in Language & Literature | tagged david foster wallace, posthuman | comments (0) | permalink

11 July 2015

Peachtree Road Race 2015

After a 40+ min delay from the rain, we swam our way to a late finish: 56:14. Down four minutes from last year.

Retired my now 3-year-old Vibrams (!) and got my 3rd pair. Went to Abadabba's in Buckhead again; excellent selection and staff.

I look horrible in the photos as I retired both my Vibrams and my 2009 Crescent City Classic t-shirt:

posted by sstrader at 11:45 AM in Where was I? | tagged jogging | comments (0) | permalink

5 July 2015

ATL Collective at Terminal West

ATL Collective is a group of local and regional musicians that get together to re-create classic rock albums live [ Web site | Facebook | Twitter | Tumblr (outdated) ]. I had heard about them many months ago from Michael H., a previous co-worker, but only decided to go see them when I got the notification about their concert on Sun 31 May for Stevie Wonder's Songs in the Key of Life. Flyer for the concert: front and back. It's a double album written after he reversed his decision to retire and go into charity work in Africa, and it contains several huge and hugely recognizable hits. ATL Collective had maybe 10 or 12 musicians total throughout the evening, with multiples for every skill: vocals, keyboards, guitars, drums, horns. Sometimes all would be on stage and at others just one or two. It was an amazing concert and perfect at that venue.

The next concert was on Thu 25 Jul for Jeff Buckley's Grace album. I knew him only from his posthumous album Sketches for My Sweetheart the Drunk. I picked that up after hearing tracks on an indie Internet station run by two guys in Texas. I listened to them frequently at around the time that album came out in 1998 (?!) and from their broadcasts also acquired Durian's Sometimes You Scare Me, Faraquet's The View from This Tower, and Don Caballero's What Burns Never Returns. Obv. heavy math rock fans. Anyway, Grace was an equally amazing concert with a guest vocalist from Nashville (the Google-friendly named Landon Pigg) who hit JB's vocal style perfectly.

Sadly, I missed their previous performances of This Year's Model and Led Zeppelin IV. On my radar for future gems.

posted by sstrader at 12:10 PM in Concerts | comments (0) | permalink

7 June 2015

Notes on We Don't Care About Music Anyway

Documentary about avant garde electronic music in Tokyo [ IMDB | Rotten Tomatoes ]. Very good. Structure worked with the music. Performances alternating with a round-table discussion by 8-or-so of the musicians represented. Worth watching for the natural, organic statements of intent that resulted. Music in the same vein as that from No Fun Fest back in 2009.

  • Is the avant garde a symptom/expression of a society's psychological disfunctions? More than mainstream forms, I see it as a working though of as-yet unformed or forming ideas.
  • Often works from existing instruments and extends them. Re orchestral writing expanding how sounds can be produced from stringed instruments (cf. even simple alterations like sul tasto and sul ponticello and then extend to other areas). Prepared piano. Records repurposed. Microphone feedback. Even when using modern devices (sequencer) it can be physically beat on to produce unintended sounds. "Hacking" your instrument.
  • Use of autonomic body processes to generate music. Think of all the personal health metrics that people are generating that could be converted to music. John Cage did similar things to remove the human hand from art. Both try to tap hidden forces, but the autonomic technique seems to have different intent.
  • Issues of authenticity. Object and location imbue meaning. Even the avant garde suffer the anxiety of influence.
  • "Contact mics allow you to amplify microscopic phenomena."
  • "To compose is to remember things that have entered us."
  • How does music reflect environment? Images of landfills and destroyed buildings shown as cultural memory. To be used by artists? Or triggered by artists?
  • All musicians were older (>30?) and, except one, male.
posted by sstrader at 1:06 PM in Cinema , Music | comments (0) | permalink

Amnesty International

Join EFF!

American Civil Liberties Union

Syndicate this site (XML)
Powered by
Movable Type 4.21-en

Atlanta People With Blogs Who Get Together To Drink

Valid XHTML 1.0!

Valid CSS!

Use OpenOffice.org

Opera Web Browser