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

Watchers

 avatar

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.