Singleton and Delegation

I have read enough information about Singleton and delegation. So I guess I understand what a singleton is. I am still confused about the delegation. I understand the concept of delegation, but I need to create my own protocol to understand delegation.

Ok, I am creating a singleton to work with my objects from CoreData. I may be wrong and this is not a singleton, please tell me about that. My singleton is FetchData.

Fetchdata.h

#import <Foundation/Foundation.h>

@interface FetchData : NSObject <UIApplicationDelegate>

+(FetchData*) fetchData;

-(NSArray*)fetchLogin:(NSString*)name;
-(BOOL)newGroup:(NSString*)group forLogin:(NSString*)login;
-(NSMutableArray*)contactsForGroup:(NSString*)group;
-(BOOL)newContact:(NSString*)name surname:(NSString*)surname withDatas:(NSArray*)array;
//other methods 

@end

      

Fetchdata.m

#import "FetchData.h"
#import "Contact.h"
#import "Login.h"
#import "Group.h"
#import "AppDelegate.h"

@interface FetchData ()
@property (nonatomic, strong) NSEntityDescription *loginEntity;
@property (nonatomic, strong) NSEntityDescription* groupEntity;
@property (nonatomic, strong) NSManagedObjectContext* context;
@property (nonatomic, strong) NSEntityDescription* contactEntity;
@property (nonatomic, strong) AppDelegate* appDelegate;
//other properties
@end

@implementation FetchData
@synthesize //my properties

+(FetchData*) fetchData
{
 static  FetchData* fetchData = nil;
 if (!fetchData) 
    fetchData = [[super allocWithZone:nil]init];
 return fetchData;
}

+(id)allocWithZone:(NSZone *)zone
{
 return [self fetchData];
}

//implementation my methods
@end

      

So it is very easy for me to work with CoreData. I only need to import FetchData and just use methods to create / remove / modify / add / sort ...

SomeClass.m

#import "FetchData.h"
#define fetching [FetchData fetchData]

      

But I think I can use for my target delegation. Or maybe this is a better discrepancy compared to the singleton. So I want to remake the singleton for delegation. And I need help with this issue. What should I do?

If I understand correctly, I need to create a protocol with all my methods from FetchData.h, FetchData.m. I can leave it unchanged. And in SomeClass I need to import FetchData and add my protocol. How:

#import <Foundation/Foundation.h>

@protocol FetchingDelegate

//all methods from FetchData.h

@end

@interface FetchData : NSObject
@property (nonatomic, strong) id <FetchingDelegate> delegate;
@end

      

FetchData.m

@interface FetchData()
//all properties without changing
@end

@implementation FetchData
@synthesize //all properties and delegate

//implementation of methods
@end

      

SomeClass

#import "FetchData.h"

@interface SomeClass : NSObject <FetchingDelegate>
@end

@implementation SomeClass

-(void)viewDidLoad
{
  FetchData* fetching = [FetchData new]
  fetching.delegate = self
}
//and now I can use any methods from protocol like [fetching anyMethod]
//such I used with singleton

      

+3


source to share


1 answer


The idea behind the singleton is that your entire application can access this class. Many controllers may need data coming from your database. In your case, I would change your method fetchData

(and possibly change its name as it is not standardized now):

+(FetchData*) fetchData
{
    static FetchData *fetchData;
    dispatch_once_t token;
    dispatch_once(&token, ^{
        if (!fetchData) 
            fetchData = [super init];
    }
 return fetchData;
}

      



Delegates are meant for one-to-one communication, which means that one object has a delegate and sends any messages to that particular delegate.

This means that the singleton and the delegation are not compatible with each other. The singleton is for sending messages to multiple receivers, and the delegation pattern is for one-to-one communication. So you have two options: you can either not use singleton mode, or use the delegation pattern, or use singleton, and use it NSNotificationCenter

to notify observers of changes.

+9


source







All Articles