Skip to content

Instantly share code, notes, and snippets.

@hsavit1
hsavit1 / RAC_DynamicSignal.m
Last active August 29, 2015 14:23
Asynchronous dynamic signal in RAC
//example of a dynamic signal created with RAC
//called 'dynamic' because it's user created. You can turn any asynchronus action into a signal
//heres a typical example
RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
id operation = asynchronusAction(^(id asyncActionResult, NSError *error) {
if (error) {
[subscriber sendError:error];
/*
RACImmediateScheduler
Private scheduler used in ReactiveCocoa internally and and supports only synchronous scheduling. It simply execute the block right away. Delayed scheduling works by blocking current thread with -[NSThread sleepUntilDate:]. Obviously, there's no way to control cancellation of such scheduling, so disposables retuned by this scheduler will do nothing (in fact, scheduling methods of this scheduler return nil).
RACQueueScheduler
This scheduler uses GCD queues for scheduling blocks. It's functionality is pretty straightforward if you know what GCD does. It's actually a very thin layer over dispatching blocks in GCD queues.
RACSubscriptionScheduler
Another private scheduler used by framework internally. It forwards scheduling to current thread's scheduler (scheduler can be associated with the thread) if it exists or to default background queue scheduler.
*/
- (RACSignal *)topQuestions{
RACSignal *signal = [[RSOWebServices sharedServices] fetchQuestionsWithTag:nil];
return [self questionsForSignal:signal];
}
- (RACSignal *)fetchQuestionsWithTag:(NSString *)tag{
NSURL *fetchQuestionURL = [self createRelativeURLWithTag:tag];
@weakify(self);
RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
@hsavit1
hsavit1 / KVO_imp.m
Last active August 29, 2015 14:23
RAC vs KVO
@implementation ViewController
static MyContext = &MyContext;
- (void)configure
[myPerson addObserver:self
forKeyPath:NSStringFromSelector(@selector(firstName))
options:NSKeyValueObservingOptionNew
context:MyContext];
}
@hsavit1
hsavit1 / RACSignal.h
Last active August 29, 2015 14:23
RACSignal Header file shows you everything
//
// RACSignal.h
// ReactiveCocoa
//
// Created by Josh Abernathy on 3/1/12.
// Copyright (c) 2012 GitHub, Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "RACStream.h"
- (RACSignal *)fetchJSONFromURL:(NSURL *)url {
NSLog(@"Fetching: %@",url.absoluteString);
// 1
return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// 2
NSURLSessionDataTask *dataTask = [self.session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// TODO: Handle retrieved data
}];
@hsavit1
hsavit1 / RAC_TableViewController.m
Created June 20, 2015 09:23
Easy TableView Design with RAC
@interface RAC_TableViewController ()
@property (nonatomic) SRGViewModel *viewModel;
@end
@implementation RAC_TableViewController
- (void)awakeFromNib {
@weakify(self)
self.viewModel = [[SRGViewModel alloc] init];
@hsavit1
hsavit1 / RAC_Async_Impl.m
Created June 20, 2015 09:30
Asynchronous operation implementation with ReactiveCocoa
// KVO based observing
RACSignal *kvoPropertySignal = RACObserve(object, property);
// dynamic signal with async action with block-based callback
RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
id operation = asynchronusAction(^(id asyncActionResult, NSError *error) {
if (error) {
[subscriber sendError:error];
}
else {
@hsavit1
hsavit1 / RAC_vs_NSOperationQueue.m
Created June 20, 2015 20:24
RAC vs NSOperationQueue for Parallelizing work
__block NSArray *databaseObjects;
__block NSArray *fileContents;
NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init];
NSBlockOperation *databaseOperation = [NSBlockOperation blockOperationWithBlock:^{
databaseObjects = [databaseClient fetchObjectsMatchingPredicate:predicate];
}];
NSBlockOperation *filesOperation = [NSBlockOperation blockOperationWithBlock:^{
NSMutableArray *filesInProgress = [NSMutableArray array];
@hsavit1
hsavit1 / RAC_Do.m
Created June 20, 2015 20:27
Do methods of RAC
__block unsigned subscriptions = 0;
RACSignal *loggingSignal = [RACSignal createSignal:^ RACDisposable * (id<RACSubscriber> subscriber) {
subscriptions++;
[subscriber sendCompleted];
return nil;
}];
// Does not output anything yet
loggingSignal = [loggingSignal doCompleted:^{