iphone - is this a bad Core Data pattern for sharing functionality? -
i have app , there basic functionality needs shared across several controllers, 1 being adding , removing call bookmarkedobjects data store. created following funciton , seems working , apply pattern other function in code adding objects order, seeing if order exist ect..
here doing:
+(void) removebookmark: (nsstring *) aitemid withcustomernumber: (nsstring *) acustomernumber withmanufacturerid: (nsstring *) amanufacturerid withmanagedobjectcontext: (nsmanagedobjectcontext *) acontext { nsfetchrequest *request = [[nsfetchrequest alloc] init]; [request setentity:[nsentitydescription entityforname:@"bookmarkeditem" inmanagedobjectcontext:acontext]]; [request setpredicate:[nspredicate predicatewithformat:@"customernumber==%@ , manufacturerid==%@ , itemid==%@", acustomernumber, amanufacturerid, aitemid]]; [request setfetchlimit:1]; nserror *error = nil; nsarray *results = [acontext executefetchrequest:request error:&error]; [request release], request = nil; [acontext deleteobject:[results lastobject]]; [acontext save:&error]; }
i suggest make singleton class , add such type of function in it.the main benefit of class methods loaded on compile time if there 5 such functions app take unnecessary memory, i'll suggest define class named sharedfunctions
in .h
@interface sharedfunctions : nsobject { } +(sharedfunctions*)sharedinstance; -(void)yourfunction; ..... //make many instace methods want @end
in .m
@implementation sharedfunctions static sharedfunctions* singletoninstance = nil; -(id)init { if (self = [super init]) { } return self; } +(sharedfunctions*)sharedinstance { @synchronized(self) { if (singletoninstance == nil) { singletoninstance = [[sharedfunctions alloc] init]; } } return singletoninstance; } ///and define methods -(void)yourfunction { //somthing } @end
now use methods call
[[sharedfunctions sharedinstance] yourfunction];
by way such methods @ 1 place , there 1 class method no memory issue.
Comments
Post a Comment