0

I'm experimenting with ReactiveCocoa and trying to solve a simple problem.

I have a rest-client which fetches data in JSON like this

{
    "entries": [
        {
            "objectId": "123",
            "name": "EntryName"
        },
        ...
    ],
    "count": 100500
}

The model which describes this respond

@interface MYEntriesList : MYJSONSerializableModel

@property (nonatomic, copy, readonly) NSArray *entries;
@property (nonatomic, assign, readonly) NSUInteger count;

@end

The model for an entry

@interface MYEntry : MYJSONSerializableModel

@property (nonatomic, copy, readonly) NSString *objectId;
@property (nonatomic, copy, readonly) NSString *name;
@property (nonatomic, copy) NSString *itemId;

@end

So my task is to set an itemId for each entry after I fetch all entries.

I use a RACCommand to perform a fetch request and MYAPIClient returns a RACSignal in -fetchContent: which sends next: after deserializing from JSON to MYEntriesList instance.

My current implementation looks like this

@weakify(self);

self.fetchCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    @strongify(self);
    return [[MYAPIClient instance] fetchContent:self.item.objectId];
}];

RAC(self, entries) = [self.fetchCommand.executionSignals flattenMap:^RACStream *(RACSignal *executionSignal) {
    return [executionSignal map:^id(MYEntriesList *list) {
        @strongify(self);
        for (MYEntry *entry in list.entries) {
            entry.itemId = self.item.objectId;
        }

        return list.entries;
    }];
}];

I would like to know if this implementation corresponds to the "idea" of the ReactiveCocoa and are there any other ways to implement this?

Dmitry
  • 7,300
  • 6
  • 32
  • 55

1 Answers1

0

You could change setting the item Id's a bit more "functional" but thats up to you really. Inside your map block:

return [[list.entries.rac_sequence map:^id(MYEntriesList *entry) {
                                       @strongify(self);
                                       entry.itemId = self.item.objectId;
                                   }] array];
Menno
  • 1,232
  • 12
  • 23