Giter Club home page Giter Club logo

igfuture's Introduction

IGFuture

Futures pattern in Objective-C.

Why?

Inspired by futuristic, I want to see if it is possible to be done in Objective-C. Turns out it has been done previously (a.k.a. MAFuture), but why not reinvent it?

This is highly experimental, so use it at your own risk!

Example

NSDate* now = [NSDate date];
NSDate* later = (NSDate*) [[IGFuture alloc] initWithBlock:^id{
    // perform some long running task
    [NSThread sleepForTimeInterval:1];

    // return the value
    return [NSDate date];
}];

expect([later timeIntervalSinceDate:now]).to.beCloseToWithin(1, 0.01);
  1. The future block run immediately in background. When it is needed (at [later timeIntervalSinceDate:now]) and the result is available, it returns immediately. If it is still running, it block and wait for completion.

  2. If you want the future only calculate the results when it is needed, use -initWithLazyBlock:.

  3. Note "later" which is a IGFuture object can be used as a NSDate (the returned value of the block).

Real(-ish) example

IGFuture* future = [[IGFuture alloc] initWithBlock:^id{
    // perform some long running task
    [NSThread sleepForTimeInterval:3.0];
    return @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10"];
}];

future.completionBlock = ^(NSArray* data) {
    self.data = data;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
};

Usage

In your Podfile

pod 'IGFuture', '~> 0.2.2'

Copyright

Copyright (c) 2013 Francis Chong. This software is licensed under the MIT License. See LICENSE for details.

igfuture's People

Contributors

i386 avatar siuying avatar

Watchers

 avatar  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.