diff --git a/Examples/coobjcBaseExample/coobjcBaseExampleTests/coobjcSequenceTests.m b/Examples/coobjcBaseExample/coobjcBaseExampleTests/coobjcSequenceTests.m index 0861e27..ef02a3c 100644 --- a/Examples/coobjcBaseExample/coobjcBaseExampleTests/coobjcSequenceTests.m +++ b/Examples/coobjcBaseExample/coobjcBaseExampleTests/coobjcSequenceTests.m @@ -24,6 +24,30 @@ #import #import "coobjcCommon.h" +static COPromise *co_downloadWithURL(NSString *url) { + + return [COPromise promise:^(COPromiseFullfill _Nonnull fullfill, COPromiseReject _Nonnull reject) { + + [NSURLSession sharedSession].configuration.requestCachePolicy = NSURLRequestReloadIgnoringCacheData; + NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithURL:[NSURL URLWithString:url] completionHandler: + ^(NSURL *location, NSURLResponse *response, NSError *error) { + if (error) { + reject(error); + return; + } + else{ + NSData *data = [[NSData alloc] initWithContentsOfURL:location]; + + fullfill(data); + return; + } + }]; + + [task resume]; + + }]; +} + SpecBegin(coSequence) describe(@"test sequence on same queue", ^{ @@ -90,7 +114,7 @@ COCoroutine *co1 = co_sequence(^{ int index = 0; while(co_isActive()){ - yield([NSData co_downloadWithURL:@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa"]); + yield(co_downloadWithURL(@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa")); index++; } }); @@ -117,7 +141,7 @@ COCoroutine *co2 = co_sequence(^{ int index = 0; while(co_isActive()){ - yield([NSData co_downloadWithURL:@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa"]); + yield(co_downloadWithURL(@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa")); index++; } }); @@ -217,7 +241,7 @@ COCoroutine *co1 = co_sequence_onqueue(q1, ^{ int index = 0; while(co_isActive()){ - yield([NSData co_downloadWithURL:@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa"]); + yield(co_downloadWithURL(@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa")); index++; } }); @@ -246,7 +270,7 @@ COCoroutine *co2 = co_sequence_onqueue(q1, ^{ int index = 0; while(co_isActive()){ - yield([NSData co_downloadWithURL:@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa"]); + yield(co_downloadWithURL(@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa")); index++; } }); diff --git a/coobjc/api/coobjc.h b/coobjc/api/coobjc.h index 44080d9..5d5b916 100644 --- a/coobjc/api/coobjc.h +++ b/coobjc/api/coobjc.h @@ -160,12 +160,18 @@ NS_INLINE NSArray *_Nullable batch_await(NSArray * _Nonnull _promiseOrCh } /** - yield with a COPromise or COChan + yield with a COPromise - @param _promiseOrChan the COPromise object or COChan object. + @discussion `yield` means pause the expression execution, + until Generator(coroutine) call `next`. + + So, you should passing a `COPromise` object with constructor, + To make the expression lazy loading. + + @param _promise the COPromise object. */ -NS_INLINE void yield(id _Nonnull _promiseOrChan) { - co_generator_yield(_promiseOrChan); +NS_INLINE void yield(id _Nonnull _promise) { + co_generator_yield(_promise); } /**