Giter Site home page Giter Site logo

stdeferred's Introduction

STDeferred 0.1.0 Build Status

Objective-C simple implementation of deferred object

Installation

CocoaPods

  1. Add pod 'STDeferred' to your Podfile
  2. Run pod install

Manual

Add files STDeferred/STDeferred.h, STDeferred/STDeferred.m to your Project

Requirements

  • iOS5 and later
  • ARC

Usage

basic

- (STDeferred*)asynchronousRequest
{
  STDeferred *deferred = [STDeferred deferred];

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/data.json"]];
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if(error) {
      [deferred reject:error];
    } else {
      [deferred resolve:data];
    }
  });  

return deferred;
}

[self asynchronousRequest]
.then(^(NSData *data) {
  NSLog(@"response string = %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
})
.fail(^(NSError *error) {
  NSLog(@"error = %@", error.description);
})
.always(^(id ret) {
  NSLog(@"Always execute this block.");
});

when

- (STDeferred*)func1
{
  STDeferred *deferred = [STDeferred deferred];
  [STDeferred timeout:1.0f].then(^(id ret) {
    [deferred resolve:@"first"];
  });
  return deferred;  
}

- (STDeferred*)func2
{
  STDeferred *deferred = [STDeferred deferred];
  [STDeferred timeout:2.0f].then(^(id ret) {
    [deferred resolve:@"second"];
  });
  return deferred;  
}

[[STDeferred when:[self func1], [self func2], nil] then:^(id ret) {
  NSLog(@"%@", [ret objectAtIndex:0]); // "first"
  NSLog(@"%@", [ret objectAtIndex:1]); // "second"
}];

pipe

- (STDeferred*)func1
{
  STDeferred *deferred = [STDeferred deferred];
  [STDeferred timeout:1.0f].then(^(id ret) {
    [deferred resolve:@"first"];
  });
  return deferred;  
}

- (STDeferred*)func2
{
  STDeferred *deferred = [STDeferred deferred];
  [STDeferred timeout:2.0f].then(^(id ret) {
    [deferred resolve:@"second"];
  });
  return deferred;  
}

STDeferred *deferred = [self func1].pipe(^id(id ret) {
  NSLog(ret) // @"first"
  return [self func2];
}, nil);

deferred.then(^(id ret) {
  NSLog(ret) // @"second"
});

next

[STDeferred deferred].resolve([NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/data.json"]])
.next(^id(id request) {
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if(error) {
        @throw [NSException exceptionWithName:@"Request Error" reason:@"Request Error" userInfo:nil];
    }
    return data;
})
.next(^id(id responseData) {
    NSError *error = nil;
    id json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&error];
    if(error) {
        @throw [NSException exceptionWithName:@"Parse Error" reason:@"Parse Error" userInfo:nil];
    }
    return json;
})
.then(^(id json) {
    NSLog(@"name = %@", [json objectForKey:@"name"]);
})
.fail(^(id exception) {
    NSLog(@"error = %@", [exception reason]);
});

timeout

NSTimeInterval interval = 5.0f;
[STDeferred timeout:interval].then(^(id ret) {
  NSLog(@"This block is executed 5 seconds later.")
});

cancel

stdeferred's People

Contributors

readmecritic avatar saiten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

stdeferred's Issues

Xcode 7.3にて__weakが使用出来ない旨のエラーが発生する問題

以下の問題です:
http://stackoverflow.com/questions/36147625/xcode-7-3-cannot-create-weak-reference-in-file-using-manual-reference-counting/36148123#36148123

対応案1)
podspecファイルを作成し、CLANG_ENABLE_OBJC_WEAK = YES の設定をする

対応案2)
__weakを__unsafe_unretained に差し替える
https://github.com/ANNotunzdY/STDeferred/tree/unsafe_unretained

どちらの対応が良いか難しいところなので、Pull Reqではなく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.