Comments (4)
That exact example isn't implemented anywhere, but the included GHAPIDemo does similar things.
It's worth noting that some major renaming has just recently happened. RACSubscribable
is now known as RACSignal
, and selectMany:
is now known as flattenMap:
.
With that in mind, every method you see there – except for the very last one – returns a RACSignal. flattenMap:
and subscribeCompleted:
are both implemented by <RACSignal>
.
subscribeCompleted:
returns a RACDisposable, which is just an object used to encapsulate some resource cleanup.
Does that clarify anything at all?
from reactivecocoa.
Thanks for getting back to me so quickly Justin.. I think I'm still missing something fundamental. Ignoring for a minute the rename (which I haven't pulled yet):
-(RACAsyncSubject*) loginUser
{
RACAsyncSubject *subject = [RACAsyncSubject subject];
SomeAsyncNetworkRequest* request = [SomeAsyncNetworkRequest performRequestWithSuccess:^(...)
{
[subject sendNext:someResponseValue];
[subject sendComplete];
} andError:(NSError* e) {
[subject sendError:e];
}
return subject;
}
-(RACAsyncSubject*) loadCachedMessages
{
// similar to loginUser
}
-(RACAsyncSubject*) fetchMessages
{
// similar to loginUser
}
-(void)performLoginLoadCachedMessagesAndFetchMessagesSynchronously
{
[self
loginUser]
selectMany:^(id _) {
return [self loadCachedMessages];
}]
selectMany:^(id _) {
return [self fetchMessages];
}]
subscribeCompleted:^{
NSLog(@"Done.");
}]
}
Is this the correct pattern? I've tried to understand the difference between select:
and selectMany:
and it's not quite obvious...can you give me a hint?
from reactivecocoa.
That looks pretty correct.
To try to understand the difference between -select:
and -selectMany:
, let's first look at -select:
. It's used to transform a subscribable of one thing into a subscribable of another thing. For example:
RACSubscribable *uppercaseSubscribable = [lowercaseSubscribable select:^(NSString *string) {
return [string uppercaseString];
}];
As you can probably guess, that transforms a subscribable of lowercase strings to create a subscribable of uppercase strings. So -select:
is all about transforming subscribables.
Now let's suppose we want to chain a couple subscribable methods like our examples. What that's really doing is transforming one subscribable into a completely different subscribable. We could just use -select:
to do that:
[[[[[self loginUser]
select:^(id _) {
// We transformed the subscribable from -loginUser into a subscribable of subscribables
return [self loadCachedMessages];
}]
// But now we have a subscribable of subscribables and we really just want a flat subscribable
// -merge will do that flattening for us.
merge]
select:^(id _) {
return [self fetchMessages];
}]
merge]
// ...and so on...
The select + merge pattern is so common, it is its own method: -selectMany:
. In fact, that's exactly how it (was) implemented: https://github.com/github/ReactiveCocoa/blob/3ddedecf662fec9001524b0bce585c11ce780b25/ReactiveCocoaFramework/ReactiveCocoa/RACSubscribableProtocol.m#L605
from reactivecocoa.
Thanks again Josh, this absolutely helps. I think I dove in too deep too quick. I need to step back for a sec and understand the basics first. I've got what I needed working for now, but I'd like to gain a deeper understanding of how it's all wired together. Back to looking at the code.
Thanks!
from reactivecocoa.
Related Issues (20)
- [SwiftPM on Xcode] Package resolution failed HOT 2
- Unable to compile targeting macOS Catalyst using SwiftPM (fix exists)
- why RACObserve(self.scoreStepper,value) not available? HOT 1
- App rejected for HealthKit metadata HOT 4
- UISearchBar delegate proxy crash on Mac Catalyst HOT 1
- Build error when using ReactiveCocoa via Swift Package Manager HOT 3
- can not deinit HOT 2
- Xcode12 ReactiveObj archive error HOT 3
- How to implement PIN input with attempts HOT 1
- Dispose SignalProducer created via Action HOT 1
- UnsafeKVOProperty initializer crashes after updating to ReactiveSwift 6.5.0 HOT 1
- EXC_BAD_ACCESS Cash with NSURL HOT 1
- ReactiveCocoa 11.1.0 incompatible with ReactiveSwift 6.6.0 HOT 5
- Xcode 12.5 beta 3 can't build ReactiveCocoa with SwiftPM. HOT 2
- Using "<~" binding function with Signal.Observers causes memory leaks. HOT 1
- Upgrading from very old version (2.5) fails - can't find ReactiveCocoa.h HOT 1
- Cannot remove an observer <RACKVOProxy 0x280264940> for the key path "unit" from <HGConfigureModel 0x280d25050> because it is not registered as an observer.
- Current version can't be compiled with the latest ReactiveSwift version HOT 2
- Current version can't be compiled with the latest ReactiveSwift version HOT 6
- Add output values support for interception
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reactivecocoa.