IGFuture 0.2.2

IGFuture 0.2.2

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2014年12月

Francis Chong 维护。



IGFuture 0.2.2

  • 作者:
  • Francis Chong

Objective-C 中的未来模式。

为什么?

futuristic 启发,我想看看这是否可以在 Objective-C 中实现。结果发现之前已经实现了(即 MAFuture),但为什么不用重新发明轮子呢?

这是一个高度实验性的项目,因此请自行承担使用风险!

示例

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. 未来块立即在后台运行。当它需要时(在 [later timeIntervalSinceDate:now]),并且结果可用,它立即返回。如果它仍在运行,它会阻塞并等待完成。

  2. 如果您想要未来只在需要时计算结果,请使用 -initWithLazyBlock:

  3. 注意“稍后”是一个 IGFuture 对象,它可以被用作 NSDate(块的返回值)。

似是而非的例子

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];
    });
};

用法

在您的 Podfile

pod 'IGFuture', '~> 0.2.1'

版权

版权所有 © 2013 Francis Chong。此软件许可协议适用 MIT 许可证。请参阅 LICENSE 文件以获取详细信息。