Reputation: 5767
20140115 updated: with working code
I have a Singleton where I want to use the delegate pattern. When I call the methode with the delegate I get not notified.
Where is my error? How can I get the delegate pattern to work with didComposition
?
Below my debugger and code, the important parts:
2014-01-15 14:31:09.703 Foobar[5854:70b] -[WebApi sandbox] [Line 42] Sandbox call
2014-01-15 14:31:09.707 Foobar[5854:70b] -[WebApi getSurroundStream] [Line 67] Surround Stream call
#import "AFHTTPRequestOperationManager.h"
@class WebApi;
@protocol WebApiDelegate <NSObject>
-(void)didComposition;
@end
@interface WebApi : AFHTTPRequestOperationManager <SingletonDelegate>
@property (assign, nonatomic)id<WebApiDelegate> delegate;
+(WebApi*)sharedInstance;
-(void)sandbox;
-(void)doSurroundComposition;
@end
#import "WebApi.h"
#define kApiHost @"http://192.168.0.1"
@implementation WebApi
-(WebApi*)initWithBaseURL:url {
self = [super init];
if (self != nil) { }
return self;
}
#pragma mark - Singleton methods
+(WebApi*)sharedInstance
{
static WebApi *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:kApiHost]];
});
return sharedInstance;
}
-(void)sandbox {
DLog(@"Sandbox called");
// Do AFNetworking Stuff
}
-(void)doSurroundComposition {
[self sandbox];
DLog(@"do surround composition");
[self.delegate performSelector:@selector(didComposition)];
}
@end
#import <UIKit/UIKit.h>
#import "Lokation.h"
#import "WebApi.h"
@interface SurroundViewController : UICollectionViewController <LokationDelegate, WebApiDelegate>
@property (strong, nonatomic) Lokation *lokation;
@end
#import "SurroundViewController.h"
#import "SWRevealViewController.h"
@interface SurroundViewController ()
@end
@implementation SurroundViewController
-(id)init
{
self = [super init];
if (self) {
// Custom initialization
self.lokation = [[Lokation alloc] init];
self.lokation.delegate = self;
[self.lokation getLocation];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[WebApi sharedInstance].delegate = self;
[[WebApi sharedInstance] doSurroundComposition];
}
-(void)didComposition {
DLog(@"did load composition"); // will not be called!
}
@end
Upvotes: 1
Views: 1814
Reputation: 119031
When you call [WebApi sharedInstance]
for the first time, you then need to set the delegate to something. Currently (in the code you show) you aren't setting any delegate. So when the shared instance tries to call the delegate it is simply a no-op.
Upvotes: 6