Reputation:
I have two different NSOperation
subclasses. One that downloads the feed from server and the other one to parse.
@interface FeedDownloader:NSOperation
@property(nonatomic, strong) NSString *downloadedFeed;
@end
@interface FeedParser:NSOperation
@property(nonatomic, strong) NSString *feedToParse;
@end
Then, in my "view controller", I add these operations to my NSOperationQueue
and set dependency.
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
FeedDownloader *downloader = [[FeedDownloader alloc] init];
FeedParser *parser = [[FeedParser alloc] init];
[parser addDependency:downloader];
[downloader addObserver:self forKeypath: @"isFinished" context:kDownloaderContext];
[queue addOperation:downloader];
[queue addOperation:parser];
Now, I get the "KVO notification" from the downloader that it finished downloading the feed. How could I pass the downloaded feed to the parser once the download completes and before it starts parsing.
Upvotes: 1
Views: 427
Reputation: 90531
You could declare a protocol, maybe FeedProvider
, which has a method by which a FeedParser
can request a feed string. Make FeedDownloader
adopt FeedProvider
. Give FeedParser
a provider
property of type id <FeedProvider>
. At creation time, give the parser
object a reference to the downloader
object as its provider.
Upvotes: 0
Reputation: 38728
In your FeedParser
class you can ensure the the isReady
is not true until it has the required data is set - this way the operation won't prematurely start until it has it's dependencies.
There are lots of ways to do this - the first thing that would come to mind for me is to use the completion block of the download operation
__weak __typeof(parser) weakParser = parser;
downloader.completionBlock = ^{
weakParser.feedToParse = downloader.downloadedFeed;
};
Upvotes: 1