Coder Social home page Coder Social logo

thin's Introduction

THIn

© 2013 James Montgomerie
[email protected], http://www.blog.montgomerie.net/
[email protected], http://th.ingsmadeoutofotherthin.gs/

What it is

  • Three easy ways to do things later in Cocoa/Cocoa Touch.
  • Simpler than dispatch_after, -performSelector:withObject:afterDelay: and NSTimer

How it works

Three ways! All call back on the main thread/runloop/dispatch queue.

  1. A Category on NSObject defines a -thIn: method that takes an NSTimeInterval and returns a proxy object. Methods sent to it will be queued and sent to the original object after the interval elapses. The recently new instancetype return type makes this type-safe too!

    [[self thIn:3] doYourThingWithThisArray:@[ @"Everybody", @"to", @"the", @"limit!" ]];
  2. A Category on NSObject defines a -thIn:do: method that takes an NSTimeInterval and a block, and invokes the block after the interval elapses.

    [self thIn:3 do:^(id obj) { 
        NSLog(@"The passed in object is the same as self. It's weakly held: %@", obj);
    }];
  3. THInWeakTimer is a lightweight timer. It has two main methods. -invalidate specifically cancels the timer. The timer is also implicitly cancelled if the THInWeakTimer is deallocated.

    @interface THInWeakTimer : NSObject
    - (id)initWithDelay:(NSTimeInterval)delay do:(void (^)(void))block;
    - (void)invalidate;
    @end

In the first two cases, the target object is weakly held. This means that if it is released before the interval is up the queued message/block will never be sent/invoked. Why? This is what made sense for my initial use case, and I still haven't found a case where I don't like the behaviour.

How to use it

I've packaged this as a static library, you should be able to use it as detailed in this blog post. It's only a couple of files though, so I won't tell anyone if you just copy them into your project instead.

thin's People

Contributors

th-in-gs avatar

Stargazers

thorfei avatar Kai Li avatar  avatar Pasquale Barilla avatar Angus H. avatar  avatar JT5D avatar  avatar  avatar Mike Rotondo avatar  avatar  avatar  avatar Bernhard Loibl avatar  avatar Thomas Heß avatar Marcus Kida avatar Ryan Nystrom avatar Javi Lorbada avatar Michael Yudin avatar Karsten avatar  avatar Blake Schwendiman avatar Min Kim avatar Gustavo Barbosa avatar Marcel Klein avatar Kingiol avatar Mikhail Merkulov avatar  avatar Tianyong Tang avatar Chris Eidhof avatar Vinh Nguyen avatar pravin reddy avatar Kasper Munck avatar Makoto Miyagawa avatar Aron Cedercrantz avatar Victor Baro avatar Luis Ascorbe avatar  avatar  avatar Sadat Rahman avatar Josh Fuggle avatar Aled Brown avatar  avatar Brian Quinn avatar Jeffrey Sambells avatar Tom Hutchinson avatar Sendhil Panchadsaram avatar Jorge (Hezi) Cohen avatar Popa Stefan avatar Alastair Tse avatar Matthew Stromberg avatar Pedro Jorge Romero avatar Tomek Kuźma avatar Mohammad Hanif avatar  avatar  avatar Matt Thomas avatar ArronPJ avatar Jaanus Siim avatar Emil avatar Yang Meyer avatar Damien Debin avatar Antonin Hildebrand avatar Abraham Vegh avatar Berklee avatar Paul Melnikow avatar chriscoffee avatar Horace Ho avatar Bruno Koga avatar  avatar Jonathan Crooke avatar Rico S avatar M Emrich avatar Kevin Cao avatar yezehui2002 avatar Reefaq Kader avatar Zheng-Yu Chen avatar feipinghuang avatar Steven Huey avatar Phillip Reichelt avatar Chris Miles avatar Martin Man avatar Tony Amoyal avatar David Barker avatar Nikita Korchagin avatar  avatar Joshua Suggs avatar Pavel Dusatko avatar Cody Krieger avatar Ke avatar Patrick Burleson avatar aleksejlinkov avatar Wesley Dyson avatar Elango avatar Carlos Ricardo Santos avatar Adam Byram avatar Marc Boquet avatar  avatar  avatar

Watchers

 avatar  avatar  avatar Jeffrey Sambells avatar mayulu avatar Pedro Jorge Romero avatar Alex Bar-Guy avatar James Cloos avatar fangjun avatar Wesley Dyson avatar

thin's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.