开发者

refreshing singleton object data in object c

here is code

+ (SalesCollection*)sharedCollection {
@synchronized(self) {
    if (sharedInstance == nil) {
        [[self alloc] init]; // assignment not d开发者_StackOverflow中文版one here
    }
}
return sharedInstance;

}

+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
    if (sharedInstance == nil) {
        sharedInstance = [super allocWithZone:zone];
        return sharedInstance;  // assignment and return on first allocation
    }
}
   return nil; //on subsequent allocation attempts return nil
}
 - (id)copyWithZone:(NSZone *)zone {
     return self;
}
 - (id)retain {
  return self;
 }

- (unsigned)retainCount {
    return UINT_MAX;  //denotes an object that cannot be released
 }

- (void)release {
 /* Problem in Here */
    [myDict release];
sharedInstance = nil;
[sharedInstance release];
 }

 - (id)autorelease {
  return self;
 }

 // setup the data collection
  - init {
if (self = [super init]) {
    [self setupData];
}
return self;
  }

and here my .h file

@interface MyCollection : NSObject {
NSMutableDictionary *myDict;
}

@property (nonatomic,retain) NSMutableDictionary * myDict;
+ (MyCollection*)sharedInstance ;
- (void)setupData;

and i have one NSMutableDictionary (myDict) which contains array of object. now my problem is i want to refresh this data on button click. so i am releasing this instance in - (void)release method then try to Init again but that creates lots of leaks because may it does not release array of objects form the myDict so how to achieve this. i follow same example "TheElement" from apple to create singleton.

Thanks


You should come up with some other way to refresh the object, rather than releasing it. The whole point of a singleton is that there can be only one, and no more will ever be created. It's no clear what myDict is, but if it's an instance variable, perhaps you could add a method such as this:

- (void) refresh {
    [myDict release];
    myDict = nil;
    [self setupData];
}


If you want to get rid of the code analyzer warnings then do this:

static SalesCollection gSharedSalesCollection = NULL;

+ (id) sharedCollection {
    @synchronized(self) {
        if (gSharedSalesCollection == nil) {
            gSharedSalesCollection = [[self alloc] init];
        }
    }
    return gSharedSalesCollection;
}

And have regular init and dealloc methods. That way you can use the class as a singleton (by accessing it with sharedCollection) or use it as a non-singleton in for example unit tests with regular alloc/init/release style.


i found a way...here is what i was doing

[self performSelectorInBackground:@selector(refreshData:) withObject:overlayView];

then i wrap up refreshData within autoreleasepool like this

NSAutoreleasePool *arPool = [[NSAutoreleasePool alloc] init];
[[MyCollection sharedCollection] refresh];
[arPool release];

and suddenly all leaks are gonna..it work like charm .....( :)))) )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜