I read enough information about singleton and delegation. So, I think I understand what is singleton. About delegation I still confuse. I understand conception of delegation, but I need to create my protocol for understanding delegation.
Ok, I create singleton for work with my entities from CoreData. Maybe I wrong and it is not singleton, tell me please about it. 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 to work with CoreData now for me. I need only import FetchData and simply use methods for create/delete/change/add/sort…
SomeClass.m
#import "FetchData.h"
#define fetching [FetchData fetchData]
But I think that I can use for my aim delegation. Or maybe it is the best decesion as compared with singleton. So I want to remake singleton for delegation. And I need help with this question. What I must do?
If I understand correctly I need create protocol with all my methods from FetchData.h, FetchData.m I can leave without changes. And in SomeClass I need import FetchData and add my protocol. Like:
#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
The idea of a singleton is that your entire app can access this one class. Multiple view controllers may need data coming from your database. In your case, I would change your
fetchDatamethod (and maybe change its name as it doesn’t really follow convention now):Delegates are meant for one-on-one communication, meaning that one object has a delegate and sends any messages to that one particular delegate.
That means that a singleton and delegation don’t go well together. The singleton is made to send messages to multiple receivers, while the delegation pattern is meant for one-on-one communication. So you have two options: you could either not use a singleton and use the delegation pattern, or you could use a singleton, and use
NSNotificationCenterto notify observers of changes.