Looking for the most elegant way to chain a dependent tree of network requests with reactive cocoa?

纵然是瞬间 提交于 2019-12-12 05:09:13

问题


This is the solution from my last question:

Since I am new to reactiveCocoa I am a little unsure if this is the right way to go?

Basicly I want to get my dependent network Requests sent serialized one after the other.

They form a tree so the parent is sent first and then any children and then the next parent:

After doing some testing the underneath code seems to do exactly what I want: Could somebody tell me if I am using reactiveCocoa the right way? Could I run into deadlocks?

#import "ViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>

@interface ViewController () {
    dispatch_queue_t backgroundQueue;
}
@end

@implementation ViewController
/**
 simulating network requests
 */

-(RACSignal*) executeRequestAsynch:(NSString*) ctx {
     return [RACSignal createSignal:^RACDisposable * (id<RACSubscriber> subscriber) {

         dispatch_async(backgroundQueue, ^(void) {
             NSLog(@"  saving depending %@ ",ctx);
             [subscriber sendCompleted];
         });

         return nil;
      }];
}
/**
 simulating network requests
 */
-(RACSignal*) executeRequestAsynchChildStep:(NSString*) ctx withParent:(NSString *) parent {
    return [RACSignal createSignal:^RACDisposable * (id<RACSubscriber> subscriber) {

        dispatch_async(backgroundQueue, ^(void) {
            NSLog(@"  saving depending ChildStep %@ of parent step:%@",ctx,parent);
            [subscriber sendCompleted];
        });

        return nil;
    }];

}

-(RACSignal*) executeRequestAsynch2:(NSString*) parent {
    RACSignal *contexts = [[@[ @"ChildStep 1", @"ChildStep 2",@"ChildStep 3", @"ChildStep 4"] rac_sequence] signalWithScheduler:RACScheduler.immediateScheduler];
    contexts = [contexts map:^(id ctx) {
        return [self executeRequestAsynchChildStep:ctx withParent:parent];
    }];
    return [contexts concat];

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    backgroundQueue = dispatch_queue_create("blah", NULL);
    RACSignal *contexts = [[@[ @"Step 1", @"Step 2",@"Step 3", @"Step 4",@"Step 5", @"Step 6"] rac_sequence] signalWithScheduler:RACScheduler.immediateScheduler];
    RACSignal *ne = [[contexts map:^(id ctx) {
        NSLog(@"iterating map  %@",ctx);
        return [[self executeRequestAsynch:ctx] concat: [self executeRequestAsynch2:ctx ]];
        }]concat] ;
    [ne subscribeCompleted:^{
        NSLog(@"done all");
    }];
}


@end

OUTPUT IS :

2014-03-11 15:06:23.361 test[2470:70b] iterating map  Step 1
2014-03-11 15:06:23.362 test[2470:70b] iterating map  Step 2
2014-03-11 15:06:23.362 test[2470:1303]   saving depending Step 1 
2014-03-11 15:06:23.363 test[2470:70b] iterating map  Step 3
2014-03-11 15:06:23.363 test[2470:1303]   saving depending ChildStep ChildStep 1 of parent step:Step 1
2014-03-11 15:06:23.363 test[2470:70b] iterating map  Step 4
2014-03-11 15:06:23.363 test[2470:70b] iterating map  Step 5
2014-03-11 15:06:23.363 test[2470:1303]   saving depending ChildStep ChildStep 2 of parent step:Step 1
2014-03-11 15:06:23.364 test[2470:70b] iterating map  Step 6
2014-03-11 15:06:23.364 test[2470:1303]   saving depending ChildStep ChildStep 3 of parent step:Step 1
2014-03-11 15:06:23.364 test[2470:1303]   saving depending ChildStep ChildStep 4 of parent step:Step 1
2014-03-11 15:06:23.365 test[2470:1303]   saving depending Step 2 
2014-03-11 15:06:23.365 test[2470:1303]   saving depending ChildStep ChildStep 1 of parent step:Step 2
2014-03-11 15:06:23.365 test[2470:1303]   saving depending ChildStep ChildStep 2 of parent step:Step 2
2014-03-11 15:06:23.370 test[2470:1303]   saving depending ChildStep ChildStep 3 of parent step:Step 2
2014-03-11 15:06:23.370 test[2470:1303]   saving depending ChildStep ChildStep 4 of parent step:Step 2
2014-03-11 15:06:23.370 test[2470:1303]   saving depending Step 3 
2014-03-11 15:06:23.371 test[2470:1303]   saving depending ChildStep ChildStep 1 of parent step:Step 3
2014-03-11 15:06:23.371 test[2470:1303]   saving depending ChildStep ChildStep 2 of parent step:Step 3
2014-03-11 15:06:23.372 test[2470:1303]   saving depending ChildStep ChildStep 3 of parent step:Step 3
2014-03-11 15:06:23.372 test[2470:1303]   saving depending ChildStep ChildStep 4 of parent step:Step 3
2014-03-11 15:06:23.372 test[2470:3803]   saving depending Step 4 
2014-03-11 15:06:23.373 test[2470:3503]   saving depending ChildStep ChildStep 1 of parent step:Step 4
2014-03-11 15:06:23.373 test[2470:3803]   saving depending ChildStep ChildStep 2 of parent step:Step 4
2014-03-11 15:06:23.373 test[2470:3503]   saving depending ChildStep ChildStep 3 of parent step:Step 4
2014-03-11 15:06:23.401 test[2470:3503]   saving depending ChildStep ChildStep 4 of parent step:Step 4
2014-03-11 15:06:23.402 test[2470:3503]   saving depending Step 5 
2014-03-11 15:06:23.402 test[2470:3503]   saving depending ChildStep ChildStep 1 of parent step:Step 5
2014-03-11 15:06:23.402 test[2470:3503]   saving depending ChildStep ChildStep 2 of parent step:Step 5
2014-03-11 15:06:23.403 test[2470:3503]   saving depending ChildStep ChildStep 3 of parent step:Step 5
2014-03-11 15:06:23.403 test[2470:3503]   saving depending ChildStep ChildStep 4 of parent step:Step 5
2014-03-11 15:06:23.404 test[2470:3503]   saving depending Step 6 
2014-03-11 15:06:23.404 test[2470:3503]   saving depending ChildStep ChildStep 1 of parent step:Step 6
2014-03-11 15:06:23.405 test[2470:3503]   saving depending ChildStep ChildStep 2 of parent step:Step 6
2014-03-11 15:06:23.405 test[2470:3503]   saving depending ChildStep ChildStep 3 of parent step:Step 6
2014-03-11 15:06:23.405 test[2470:3503]   saving depending ChildStep ChildStep 4 of parent step:Step 6
2014-03-11 15:06:23.406 test[2470:3503] done all

After considering the suggested changes I arrived at the following code :

    #import "ViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>

@interface ViewController () {
    dispatch_queue_t backgroundQueue;
}
@end

@implementation ViewController
/**
 simulating network requests
 */
-(RACSignal*) executeRequestAsynch:(NSString*) ctx {
     return [RACSignal createSignal:^RACDisposable * (id<RACSubscriber> subscriber) {

         dispatch_async(backgroundQueue, ^(void) {
             //simulating URLConnnection network requests with completion handler
             NSLog(@"  saving depending %@ ",ctx);
             [subscriber sendCompleted];
         });
         return nil;
      }];
}
/**
 simulating network requests
 */
-(RACSignal*) executeRequestAsynchChildStep:(NSString*) ctx withParent:(NSString *) parent {
    return [RACSignal createSignal:^RACDisposable * (id<RACSubscriber> subscriber) {

        dispatch_async(backgroundQueue, ^(void) {
            //simulating URLConnnection network requests with completion handler
            NSLog(@"  saving depending ChildStep %@ of parent step:%@",ctx,parent);
            [subscriber sendCompleted];
        });
        return nil;
    }];

}
-(RACSignal*) executeRequestAsynch2:(NSString*) parent {
    RACSequence *contexts = [@[ @"ChildStep 1", @"ChildStep 2",@"ChildStep 3", @"ChildStep 4"] rac_sequence];
    contexts = [contexts map:^(id ctx) {
        return [self executeRequestAsynchChildStep:ctx withParent:parent];
    }];
    return [RACSignal concat:contexts];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    backgroundQueue = dispatch_queue_create("blah", NULL);
    RACSignal *contexts = [[@[ @"Step 1", @"Step 2",@"Step 3", @"Step 4"] rac_sequence] signal];
    RACSignal *ne = [[contexts map:^(id ctx) {
        NSLog(@"iterating map  %@",ctx);
        return [[self executeRequestAsynch:ctx] concat: [self executeRequestAsynch2:ctx ]];
        }]concat] ;
    [ne subscribeCompleted:^{
        NSLog(@"done all");
    }];
}

回答1:


Overall, it looks as though the code would do what you want it to do. I don't see any cause for concern on deadlock. Was there a specific area you thought might deadlock?

I can give you a few suggestions on the code.

You've clearly seen that ReactiveCocoa has the concept of a scheduler (RACScheduler), and this class allows you to replace the imperative use of dispatch queues. This signal above:

return [RACSignal createSignal:^(id<RACSubscriber> subscriber) {
    dispatch_async(backgroundQueue, ^{
        NSLog(@"  saving depending ChildStep %@ of parent step:%@", ctx, parent);
        [subscriber sendCompleted];
    });
    return nil;
}];

Can be rewritten using -subscribeOn::

RACScheduler *backgroundScheduler = [[RACScheduler alloc] initWithName:nil targetQueue:backgroundQueue];

return [[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
    NSLog(@"  saving depending ChildStep %@ of parent step:%@", ctx, parent);
    [subscriber sendCompleted];
    return nil;
}] subscribeOn:backgroundScheduler];

Next, the way you're using sequences can be simplified. Instead of converting the sequences to signals, you could pass a sequence to +[RACSignal concat:]. For example, instead of:

RACSignal *contexts = [[@[@"ChildStep 1", … @"ChildStep N"] rac_sequence] signalWithScheduler:RACScheduler.immediateScheduler];
contexts = [contexts map:^(id ctx) {
    return [self executeRequestAsynchChildStep:ctx withParent:parent];
}];
return [contexts concat];

You can drop the use of -signalWithScheduler:, and apply +concat::

RACSequence *contexts = [@[@"ChildStep 1", … @"ChildStep N"] rac_sequence];
contexts = [contexts map:^(id ctx) {
    return [self executeRequestAsynchChildStep:ctx withParent:parent];
}];
return [RACSignal concat:contexts];

Hope that helps.

PS. You can learn a lot by following the ReactiveCocoa discussions.



来源:https://stackoverflow.com/questions/22328557/looking-for-the-most-elegant-way-to-chain-a-dependent-tree-of-network-requests-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!